1use serde::{Deserialize, Serialize};
2
3#[derive(
5 Clone,
6 Copy,
7 Debug,
8 Default,
9 PartialEq,
10 Eq,
11 Serialize,
12 Deserialize,
13 strum::Display,
14 strum::EnumString,
15 strum::VariantArray,
16)]
17#[strum(serialize_all = "snake_case")]
18#[serde(rename_all = "snake_case")]
19#[non_exhaustive]
20pub enum ContentType {
21 Any,
25 #[default]
26 Raw,
27 Json,
28 Avro,
29 Protobuf,
30 Msgpack,
31 Cbor,
32 Bson,
33 Arrow,
34 Ref,
39}
40
41impl ContentType {
42 pub const fn is_raw(&self) -> bool {
45 matches!(self, ContentType::Raw)
46 }
47
48 pub const fn code(self) -> u8 {
54 match self {
55 ContentType::Raw => 0,
56 ContentType::Json => 1,
57 ContentType::Msgpack => 2,
58 ContentType::Cbor => 3,
59 ContentType::Bson => 4,
60 ContentType::Avro => 5,
61 ContentType::Protobuf => 6,
62 ContentType::Arrow => 7,
63 ContentType::Ref => 8,
64 ContentType::Any => 255,
65 }
66 }
67
68 pub const fn from_code(code: u8) -> Option<Self> {
74 match code {
75 0 => Some(ContentType::Raw),
76 1 => Some(ContentType::Json),
77 2 => Some(ContentType::Msgpack),
78 3 => Some(ContentType::Cbor),
79 4 => Some(ContentType::Bson),
80 5 => Some(ContentType::Avro),
81 6 => Some(ContentType::Protobuf),
82 7 => Some(ContentType::Arrow),
83 8 => Some(ContentType::Ref),
84 255 => Some(ContentType::Any),
85 _ => None,
86 }
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn given_content_type_codes_when_mapped_then_should_match_the_fixed_dictionary() {
96 let expected = [
99 (ContentType::Raw, 0u8),
100 (ContentType::Json, 1),
101 (ContentType::Msgpack, 2),
102 (ContentType::Cbor, 3),
103 (ContentType::Bson, 4),
104 (ContentType::Avro, 5),
105 (ContentType::Protobuf, 6),
106 (ContentType::Arrow, 7),
107 (ContentType::Ref, 8),
108 (ContentType::Any, 255),
109 ];
110 for (content_type, code) in expected {
111 assert_eq!(content_type.code(), code);
112 assert_eq!(ContentType::from_code(code), Some(content_type));
113 }
114 assert_eq!(ContentType::from_code(9), None);
115 }
116
117 #[test]
118 fn given_content_types_when_displayed_then_should_match_the_wire_names() {
119 assert_eq!(ContentType::Raw.to_string(), "raw");
120 assert_eq!(ContentType::Json.to_string(), "json");
121 assert_eq!(ContentType::Msgpack.to_string(), "msgpack");
122 assert_eq!(
123 "protobuf".parse::<ContentType>().expect("protobuf parses"),
124 ContentType::Protobuf
125 );
126 assert_eq!(ContentType::default(), ContentType::Raw);
127 }
128}