protoflow_blocks/types/
encoding.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::prelude::{fmt, FromStr, String};
4
5/// The encoding to use when (de)serializing messages from/to bytes.
6#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub enum Encoding {
9    #[default]
10    ProtobufWithLengthPrefix,
11    ProtobufWithoutLengthPrefix,
12    TextWithNewlineSuffix,
13}
14
15impl FromStr for Encoding {
16    type Err = String;
17
18    fn from_str(input: &str) -> Result<Self, Self::Err> {
19        use Encoding::*;
20        Ok(match input {
21            "protobuf-with-length-prefix" | "protobuf" => ProtobufWithLengthPrefix,
22            "protobuf-without-length-prefix" => ProtobufWithoutLengthPrefix,
23            "text-with-newline-suffix" | "text" => TextWithNewlineSuffix,
24            _ => return Err(String::from(input)),
25        })
26    }
27}
28
29impl fmt::Display for Encoding {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        use Encoding::*;
32        match self {
33            ProtobufWithLengthPrefix => write!(f, "protobuf-with-length-prefix"),
34            ProtobufWithoutLengthPrefix => write!(f, "protobuf-without-length-prefix"),
35            TextWithNewlineSuffix => write!(f, "text-with-newline-suffix"),
36        }
37    }
38}