Skip to main content

act_types/
cbor.rs

1// CBOR <-> JSON/serde conversion utilities.
2//
3// Note: ciborium produces standard CBOR, not strict dCBOR (RFC 8949 §4.2).
4// For JSON-originating data this is practically deterministic, but does not
5// guarantee shortest integer encoding, sorted map keys, or preferred floats.
6
7/// Encode a serializable value as CBOR bytes.
8pub fn to_cbor<T: serde::Serialize>(value: &T) -> Vec<u8> {
9    let mut buf = Vec::new();
10    ciborium::into_writer(value, &mut buf).expect("CBOR serialization should not fail");
11    buf
12}
13
14/// Decode CBOR bytes into a deserializable value.
15pub fn from_cbor<T: serde::de::DeserializeOwned>(bytes: &[u8]) -> Result<T, CborError> {
16    ciborium::from_reader(bytes).map_err(|e| CborError(format!("CBOR decode failed: {e}")))
17}
18
19/// Convert a JSON value to CBOR bytes.
20pub fn json_to_cbor(value: &serde_json::Value) -> Result<Vec<u8>, CborError> {
21    let mut buf = Vec::new();
22    ciborium::into_writer(value, &mut buf)
23        .map_err(|e| CborError(format!("JSON→CBOR encode failed: {e}")))?;
24    Ok(buf)
25}
26
27/// Convert CBOR bytes to a JSON value.
28pub fn cbor_to_json(bytes: &[u8]) -> Result<serde_json::Value, CborError> {
29    ciborium::from_reader(bytes).map_err(|e| CborError(format!("CBOR→JSON decode failed: {e}")))
30}
31
32/// Decode content-part data based on MIME type.
33///
34/// - `text/*` — raw UTF-8 bytes → JSON string
35/// - everything else — CBOR-decoded to JSON, with base64 fallback for invalid CBOR
36pub fn decode_content_data(data: &[u8], mime_type: Option<&str>) -> serde_json::Value {
37    let mime = mime_type.unwrap_or("application/cbor");
38    if mime.starts_with("text/") {
39        serde_json::Value::String(String::from_utf8_lossy(data).into_owned())
40    } else {
41        cbor_to_json(data).unwrap_or_else(|_| {
42            use base64::Engine as _;
43            serde_json::Value::String(base64::engine::general_purpose::STANDARD.encode(data))
44        })
45    }
46}
47
48/// CBOR conversion error.
49#[derive(Debug, Clone)]
50pub struct CborError(pub String);
51
52impl std::fmt::Display for CborError {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        f.write_str(&self.0)
55    }
56}
57
58impl std::error::Error for CborError {}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use serde_json::json;
64
65    #[test]
66    fn roundtrip_object() {
67        let input = json!({"a": 2, "b": 3});
68        let cbor = json_to_cbor(&input).unwrap();
69        let output = cbor_to_json(&cbor).unwrap();
70        assert_eq!(input, output);
71    }
72
73    #[test]
74    fn roundtrip_nested() {
75        let input = json!({"config": {"api_key": "abc123"}, "values": [1, 2, 3]});
76        let cbor = json_to_cbor(&input).unwrap();
77        let output = cbor_to_json(&cbor).unwrap();
78        assert_eq!(input, output);
79    }
80
81    #[test]
82    fn roundtrip_null() {
83        let input = json!(null);
84        let cbor = json_to_cbor(&input).unwrap();
85        let output = cbor_to_json(&cbor).unwrap();
86        assert_eq!(input, output);
87    }
88
89    #[test]
90    fn empty_bytes_is_error() {
91        assert!(cbor_to_json(&[]).is_err());
92    }
93
94    #[test]
95    fn generic_roundtrip() {
96        let input = 42u64;
97        let bytes = to_cbor(&input);
98        let output: u64 = from_cbor(&bytes).unwrap();
99        assert_eq!(input, output);
100    }
101
102    #[test]
103    fn decode_text_content() {
104        let data = b"hello world";
105        let result = decode_content_data(data, Some("text/plain"));
106        assert_eq!(result, json!("hello world"));
107    }
108
109    #[test]
110    fn decode_cbor_content() {
111        let data = to_cbor(&json!({"key": "value"}));
112        let result = decode_content_data(&data, None);
113        assert_eq!(result, json!({"key": "value"}));
114    }
115
116    #[test]
117    fn decode_invalid_cbor_falls_back_to_base64() {
118        let data = b"\xff\xfe";
119        let result = decode_content_data(data, Some("application/octet-stream"));
120        // Should be base64 string
121        assert!(result.is_string());
122    }
123}