Skip to main content

mz_pgrepr_consts/
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 std::io;
11
12/// The encoding format for a `mz_pgrepr::Value`.
13///
14/// See the ["Formats and Format Codes"][pgdocs] section of the PostgreSQL
15/// protocol documentation for details on the available formats.
16///
17/// [pgdocs]:
18/// https://www.postgresql.org/docs/current/protocol-overview.html#PROTOCOL-FORMAT-CODES
19#[derive(Copy, Clone, Debug)]
20pub enum Format {
21    /// Text encoding.
22    Text,
23    /// Binary encoding.
24    Binary,
25}
26
27impl TryFrom<u16> for Format {
28    type Error = io::Error;
29
30    fn try_from(value: u16) -> Result<Self, Self::Error> {
31        match value {
32            0 => Ok(Format::Text),
33            1 => Ok(Format::Binary),
34            n => Err(io::Error::new(
35                io::ErrorKind::InvalidInput,
36                format!("unknown format code: {}", n),
37            )),
38        }
39    }
40}
41
42impl From<Format> for i8 {
43    fn from(val: Format) -> Self {
44        match val {
45            Format::Text => 0,
46            Format::Binary => 1,
47        }
48    }
49}