nodedb_array/codec/
tag.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[repr(u8)]
12pub enum CodecTag {
13 Raw = 0,
16 Structural = 1,
18}
19
20impl CodecTag {
21 pub fn from_byte(b: u8) -> Option<Self> {
22 match b {
23 0 => Some(CodecTag::Raw),
24 1 => Some(CodecTag::Structural),
25 _ => None,
26 }
27 }
28
29 pub fn as_byte(self) -> u8 {
30 self as u8
31 }
32}
33
34pub fn peek_tag(payload: &[u8]) -> Option<CodecTag> {
41 let first = *payload.first()?;
42 CodecTag::from_byte(first)
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn raw_tag_roundtrips() {
51 assert_eq!(CodecTag::Raw.as_byte(), 0);
52 assert_eq!(CodecTag::from_byte(0), Some(CodecTag::Raw));
53 }
54
55 #[test]
56 fn structural_tag_roundtrips() {
57 assert_eq!(CodecTag::Structural.as_byte(), 1);
58 assert_eq!(CodecTag::from_byte(1), Some(CodecTag::Structural));
59 }
60
61 #[test]
62 fn unknown_byte_returns_none() {
63 assert_eq!(CodecTag::from_byte(42), None);
64 assert_eq!(CodecTag::from_byte(255), None);
65 }
66
67 #[test]
68 fn peek_tag_former_msgpack_bytes_return_none() {
69 for b in 0x80u8..=0x8fu8 {
72 let payload = [b, 0x00];
73 assert_eq!(peek_tag(&payload), None, "byte {b:#04x} should be None");
74 }
75 assert_eq!(peek_tag(&[0xde, 0x00]), None);
76 assert_eq!(peek_tag(&[0xdf, 0x00]), None);
77 }
78
79 #[test]
80 fn peek_tag_detects_raw_tag() {
81 assert_eq!(peek_tag(&[0x00, 0x01, 0x02]), Some(CodecTag::Raw));
82 }
83
84 #[test]
85 fn peek_tag_detects_structural_tag() {
86 assert_eq!(peek_tag(&[0x01, 0x00]), Some(CodecTag::Structural));
87 }
88
89 #[test]
90 fn peek_tag_empty_returns_none() {
91 assert_eq!(peek_tag(&[]), None);
92 }
93
94 #[test]
95 fn peek_tag_unknown_byte_returns_none() {
96 assert_eq!(peek_tag(&[42]), None);
98 }
99}