Skip to main content

laser_wire/
content.rs

1use serde::{Deserialize, Serialize};
2
3/// The wire codec tag stamped on `agdx.ct`.
4#[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    /// "Best-effort decode": the projector tries JSON first, else
22    /// raw. Useful when a projection is content-agnostic. Lands on the wire
23    /// as the `any` variant so consumers can distinguish "unknown" from "raw".
24    Any,
25    #[default]
26    Raw,
27    Json,
28    Avro,
29    Protobuf,
30    Msgpack,
31    Cbor,
32    Bson,
33    Arrow,
34    /// The body is a CBOR [`BodyRef`](crate::agent::BodyRef) capsule
35    /// pointing at content stored elsewhere (object storage, KV, another
36    /// topic), not the content itself: the claim-check form for payloads too
37    /// large or sensitive to inline.
38    Ref,
39}
40
41impl ContentType {
42    /// Whether this is the default `Raw` codec (omitted on the wire when a
43    /// field defaults to it).
44    pub const fn is_raw(&self) -> bool {
45        matches!(self, ContentType::Raw)
46    }
47
48    /// The compact `u8` wire code stamped as the `agdx.ct` header value. A fixed
49    /// dictionary shared with LaserData Cloud and its display layers (and the
50    /// shape planned for Iggy's native reserved content-type field): raw=0,
51    /// json=1, msgpack=2, cbor=3, bson=4, avro=5, protobuf=6, arrow=7,
52    /// ref=8 (claim-check body reference), any=255 (best-effort sentinel).
53    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    /// Decode a compact `agdx.ct` code, or `None` for a code this build does
69    /// not name. The codes are a growable dictionary, so a server MUST treat an
70    /// unknown code as opaque (pass it through, decode the body best-effort)
71    /// and never reject the record on it. A newer peer may stamp a code this
72    /// build has not learned yet.
73    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        // The compact `agdx.ct` dictionary is a fixed wire contract: the
97        // `ContentType::code` mapping must not drift.
98        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}