Skip to main content

gbp_core/
codec.rs

1//! Payload codec discriminant.
2
3use core::fmt;
4
5/// Codec used to encode the sub-protocol payload inside a `GbpFrame`.
6///
7/// The `pf` (payload_format) field in the frame header carries this value.
8/// `Cbor` (0) is the default and is backward-compatible with all existing
9/// implementations; frames that omit the `pf` field are treated as CBOR.
10#[repr(u8)]
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
12pub enum PayloadCodec {
13    /// CBOR (RFC 7049) — default and backward-compatible.
14    #[default]
15    Cbor = 0,
16    /// Protocol Buffers (proto3).
17    Protobuf = 1,
18    /// FlatBuffers.
19    FlatBuffers = 2,
20}
21
22impl PayloadCodec {
23    /// Returns the on-wire byte discriminant.
24    pub const fn as_u8(self) -> u8 {
25        self as u8
26    }
27
28    /// Converts from the on-wire discriminant. Returns `None` for unknown values.
29    pub fn from_u8(v: u8) -> Option<Self> {
30        match v {
31            0 => Some(Self::Cbor),
32            1 => Some(Self::Protobuf),
33            2 => Some(Self::FlatBuffers),
34            _ => None,
35        }
36    }
37}
38
39impl fmt::Display for PayloadCodec {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.write_str(match self {
42            Self::Cbor => "cbor",
43            Self::Protobuf => "protobuf",
44            Self::FlatBuffers => "flatbuffers",
45        })
46    }
47}