Skip to main content

mcap2arrow_core/
message_encoding.rs

1use std::fmt;
2
3/// Message encodings defined in the mcap spec registry.
4/// <https://mcap.dev/spec/registry>
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub enum MessageEncoding {
7    /// ROS 1 (`ros1`)
8    Ros1,
9    /// CDR - used by ROS 2 (`cdr`)
10    Cdr,
11    /// Protocol Buffers (`protobuf`)
12    Protobuf,
13    /// FlatBuffers (`flatbuffer`)
14    FlatBuffer,
15    /// CBOR (`cbor`)
16    Cbor,
17    /// MessagePack (`msgpack`)
18    MsgPack,
19    /// JSON (`json`)
20    Json,
21    /// Unknown/custom encoding
22    Unknown(String),
23}
24
25impl MessageEncoding {
26    pub fn as_str(&self) -> &str {
27        match self {
28            Self::Ros1 => "ros1",
29            Self::Cdr => "cdr",
30            Self::Protobuf => "protobuf",
31            Self::FlatBuffer => "flatbuffer",
32            Self::Cbor => "cbor",
33            Self::MsgPack => "msgpack",
34            Self::Json => "json",
35            Self::Unknown(s) => s,
36        }
37    }
38}
39
40impl From<&str> for MessageEncoding {
41    fn from(s: &str) -> Self {
42        match s {
43            "ros1" => Self::Ros1,
44            "cdr" => Self::Cdr,
45            "protobuf" => Self::Protobuf,
46            "flatbuffer" => Self::FlatBuffer,
47            "cbor" => Self::Cbor,
48            "msgpack" => Self::MsgPack,
49            "json" => Self::Json,
50            other => Self::Unknown(other.to_string()),
51        }
52    }
53}
54
55impl fmt::Display for MessageEncoding {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.write_str(self.as_str())
58    }
59}