mz_pgwire_common/
format.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use tokio::io;
11
12use crate::codec::input_err;
13
14/// The encoding format for a `mz_pgrepr::Value`.
15///
16/// See the ["Formats and Format Codes"][pgdocs] section of the PostgreSQL
17/// protocol documentation for details on the available formats.
18///
19/// [pgdocs]:
20/// https://www.postgresql.org/docs/current/protocol-overview.html#PROTOCOL-FORMAT-CODES
21#[derive(Copy, Clone, Debug)]
22pub enum Format {
23    /// Text encoding.
24    Text,
25    /// Binary encoding.
26    Binary,
27}
28
29impl TryFrom<i16> for Format {
30    type Error = io::Error;
31
32    fn try_from(value: i16) -> Result<Self, Self::Error> {
33        match value {
34            0 => Ok(Format::Text),
35            1 => Ok(Format::Binary),
36            n => Err(input_err(format!("unknown format code: {}", n))),
37        }
38    }
39}
40
41impl From<Format> for i8 {
42    fn from(val: Format) -> Self {
43        match val {
44            Format::Text => 0,
45            Format::Binary => 1,
46        }
47    }
48}