1pub const TAG: u8 = 0xFC;
25
26pub const HEADER_LEN: usize = 1 + 8 + 8 + 4 + 4;
28
29pub const MAX_FIELD_LEN: usize = 64 * 1024;
31
32#[derive(Debug, Clone, PartialEq)]
34pub struct MetadataUpdate {
35 pub seq: u64,
40 pub timestamp: f64,
42 pub key: String,
44 pub value: String,
46}
47
48pub fn write_record(out: &mut Vec<u8>, update: &MetadataUpdate) {
50 let key = update.key.as_bytes();
51 let val = update.value.as_bytes();
52 let key_len = key.len().min(MAX_FIELD_LEN);
53 let val_len = val.len().min(MAX_FIELD_LEN);
54 out.reserve(HEADER_LEN + key_len + val_len);
55 out.push(TAG);
56 out.extend_from_slice(&update.seq.to_le_bytes());
57 out.extend_from_slice(&update.timestamp.to_le_bytes());
58 out.extend_from_slice(&(key_len as u32).to_le_bytes());
59 out.extend_from_slice(&(val_len as u32).to_le_bytes());
60 out.extend_from_slice(&key[..key_len]);
61 out.extend_from_slice(&val[..val_len]);
62}
63
64#[derive(Debug, Clone, PartialEq)]
66pub enum Next {
67 Record(MetadataUpdate, usize),
69 Incomplete,
71 Other,
73 Invalid,
75}
76
77pub fn peek(buf: &[u8]) -> Next {
79 match buf.first() {
80 Some(&TAG) => {}
81 _ => return Next::Other,
82 }
83 if buf.len() < HEADER_LEN {
84 return Next::Incomplete;
85 }
86 let mut seq_b = [0u8; 8];
87 seq_b.copy_from_slice(&buf[1..9]);
88 let seq = u64::from_le_bytes(seq_b);
89
90 let mut ts_b = [0u8; 8];
91 ts_b.copy_from_slice(&buf[9..17]);
92 let timestamp = f64::from_le_bytes(ts_b);
93
94 let mut kl_b = [0u8; 4];
95 kl_b.copy_from_slice(&buf[17..21]);
96 let key_len = u32::from_le_bytes(kl_b) as usize;
97
98 let mut vl_b = [0u8; 4];
99 vl_b.copy_from_slice(&buf[21..25]);
100 let val_len = u32::from_le_bytes(vl_b) as usize;
101
102 if key_len > MAX_FIELD_LEN || val_len > MAX_FIELD_LEN {
103 return Next::Invalid;
104 }
105 let total = match HEADER_LEN
106 .checked_add(key_len)
107 .and_then(|n| n.checked_add(val_len))
108 {
109 Some(n) => n,
110 None => return Next::Invalid,
111 };
112 if buf.len() < total {
113 return Next::Incomplete;
114 }
115 let key = match core::str::from_utf8(&buf[HEADER_LEN..HEADER_LEN + key_len]) {
116 Ok(s) => s.to_string(),
117 Err(_) => return Next::Invalid,
118 };
119 let value = match core::str::from_utf8(&buf[HEADER_LEN + key_len..total]) {
120 Ok(s) => s.to_string(),
121 Err(_) => return Next::Invalid,
122 };
123 Next::Record(
124 MetadataUpdate {
125 seq,
126 timestamp,
127 key,
128 value,
129 },
130 total,
131 )
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 #[test]
139 fn round_trip_preserves_fields_and_exact_timestamp_bits() {
140 let orig = MetadataUpdate {
141 seq: 42_000,
142 timestamp: 12345.6789012345,
143 key: "impedance/Fp1".to_string(),
144 value: "4.2 kOhm".to_string(),
145 };
146 let mut buf = Vec::new();
147 write_record(&mut buf, &orig);
148 assert_eq!(buf[0], TAG);
149 match peek(&buf) {
150 Next::Record(got, used) => {
151 assert_eq!(used, buf.len());
152 assert_eq!(got.seq, orig.seq);
153 assert_eq!(got.timestamp.to_bits(), orig.timestamp.to_bits());
154 assert_eq!(got.key, orig.key);
155 assert_eq!(got.value, orig.value);
156 }
157 other => panic!("expected Record, got {other:?}"),
158 }
159 }
160
161 #[test]
162 fn every_truncation_returns_incomplete() {
163 let orig = MetadataUpdate {
164 seq: 7,
165 timestamp: 1.5,
166 key: "ch/0/bad".to_string(),
167 value: "1".to_string(),
168 };
169 let mut buf = Vec::new();
170 write_record(&mut buf, &orig);
171 for cut in 1..buf.len() {
172 assert_eq!(peek(&buf[..cut]), Next::Incomplete, "cut at {cut}");
173 }
174 }
175
176 #[test]
177 fn other_tags_and_empty_return_other() {
178 assert_eq!(peek(&[]), Next::Other);
179 assert_eq!(peek(&[0x01, 0x00]), Next::Other);
180 assert_eq!(peek(&[0x02, 0x00]), Next::Other);
181 assert_eq!(peek(&[0xFE, 0x00]), Next::Other);
182 }
183
184 #[test]
185 fn oversized_and_bad_utf8_return_invalid() {
186 let mut bad_len = vec![TAG];
187 bad_len.extend_from_slice(&0u64.to_le_bytes());
188 bad_len.extend_from_slice(&0.0f64.to_le_bytes());
189 bad_len.extend_from_slice(&((MAX_FIELD_LEN as u32) + 1).to_le_bytes());
190 bad_len.extend_from_slice(&0u32.to_le_bytes());
191 assert_eq!(peek(&bad_len), Next::Invalid);
192
193 let mut bad_utf8 = vec![TAG];
194 bad_utf8.extend_from_slice(&0u64.to_le_bytes());
195 bad_utf8.extend_from_slice(&0.0f64.to_le_bytes());
196 bad_utf8.extend_from_slice(&2u32.to_le_bytes());
197 bad_utf8.extend_from_slice(&0u32.to_le_bytes());
198 bad_utf8.extend_from_slice(&[0xFF, 0xFE]);
199 assert_eq!(peek(&bad_utf8), Next::Invalid);
200 }
201}