shrike 0.1.1

AT Protocol library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Bump-allocated DRISL decoding.
//!
//! When the `bumpalo` feature is enabled, this module provides [`BumpValue`]
//! and [`Decoder::decode_bump`] for zero-heap-allocation decoding. All
//! collections (arrays, maps) are allocated as slices in the bump arena,
//! avoiding per-Vec malloc/free and Drop overhead.
//!
//! This is the optimal path for hot loops like firehose decode, where you
//! process one frame at a time and can reset the arena between frames.
//!
//! ```no_run
//! # #[cfg(feature = "bumpalo")]
//! # {
//! use bumpalo::Bump;
//! use crate::cbor::Decoder;
//!
//! let bump = Bump::new();
//! let data: &[u8] = &[0xa1, 0x61, 0x61, 0x01]; // {"a": 1}
//! let mut dec = Decoder::new(data);
//! let val = dec.decode_bump(&bump).unwrap();
//! // val borrows from both `data` (text/bytes) and `bump` (collections)
//! // reset the arena to free everything at once:
//! // bump.reset();
//! # }
//! ```

use bumpalo::Bump;

use crate::cbor::CborError;
use crate::cbor::cid::Cid;
use crate::cbor::decode::Decoder;

/// Maximum nesting depth — mirrors the limit in the standard decoder.
const MAX_DEPTH: usize = 64;

/// Maximum collection length — mirrors the limit in the standard decoder.
const MAX_COLLECTION_LEN: usize = 500_000;

/// Decoded DRISL value with bump-allocated collections.
///
/// Text and bytes borrow from the input buffer (`'data`), while arrays
/// and maps are allocated as slices in the bump arena (`'bump`).
/// This eliminates all heap allocation during decode.
#[derive(Debug, Clone, PartialEq)]
pub enum BumpValue<'bump, 'data> {
    Unsigned(u64),
    Signed(i64),
    Float(f64),
    Bool(bool),
    Null,
    Text(&'data str),
    Bytes(&'data [u8]),
    Cid(Cid),
    Array(&'bump [BumpValue<'bump, 'data>]),
    Map(&'bump [(&'data str, BumpValue<'bump, 'data>)]),
}

impl<'data> Decoder<'data> {
    /// Decode one DRISL value, allocating collections into the bump arena.
    ///
    /// This is functionally identical to [`Decoder::decode`] but avoids all
    /// heap allocation — arrays and maps are bump-allocated slices instead
    /// of `Vec`s. For hot loops, reset the arena between iterations to
    /// reclaim memory without per-element Drop overhead.
    #[inline]
    pub fn decode_bump<'bump>(
        &mut self,
        bump: &'bump Bump,
    ) -> Result<BumpValue<'bump, 'data>, CborError> {
        self.decode_bump_inner(bump, 0)
    }

    fn decode_bump_inner<'bump>(
        &mut self,
        bump: &'bump Bump,
        depth: usize,
    ) -> Result<BumpValue<'bump, 'data>, CborError> {
        if depth >= MAX_DEPTH {
            return Err(CborError::InvalidCbor(
                "maximum nesting depth exceeded".into(),
            ));
        }
        let initial_byte = self.read_byte()?;
        let major = initial_byte >> 5;
        let additional = initial_byte & 0x1f;

        match major {
            0 => {
                let n = self.read_argument(additional)?;
                Ok(BumpValue::Unsigned(n))
            }
            1 => {
                let n = self.read_argument(additional)?;
                let val = if n <= i64::MAX as u64 {
                    -1 - (n as i64)
                } else {
                    return Err(CborError::InvalidCbor(
                        "negative integer too large for i64".into(),
                    ));
                };
                Ok(BumpValue::Signed(val))
            }
            2 => {
                let len = self.read_argument(additional)?;
                let len_usize = usize::try_from(len)
                    .map_err(|_| CborError::InvalidCbor("length exceeds platform limits".into()))?;
                let bytes = self.read_slice(len_usize)?;
                Ok(BumpValue::Bytes(bytes))
            }
            3 => {
                let len = self.read_argument(additional)?;
                let len_usize = usize::try_from(len)
                    .map_err(|_| CborError::InvalidCbor("length exceeds platform limits".into()))?;
                let bytes = self.read_slice(len_usize)?;
                let text = match simdutf8::basic::from_utf8(bytes) {
                    Ok(s) => s,
                    Err(_) => {
                        return Err(CborError::InvalidCbor(
                            "invalid UTF-8 in text string".into(),
                        ));
                    }
                };
                Ok(BumpValue::Text(text))
            }
            4 => {
                let len = self.read_argument(additional)?;
                let len_usize = usize::try_from(len)
                    .map_err(|_| CborError::InvalidCbor("length exceeds platform limits".into()))?;
                if len_usize > MAX_COLLECTION_LEN {
                    return Err(CborError::InvalidCbor(
                        "array length exceeds maximum".into(),
                    ));
                }
                let capacity = len_usize.min(self.remaining());
                let items = bump.alloc_slice_fill_with(capacity, |_| BumpValue::Null);
                for item in items.iter_mut() {
                    *item = self.decode_bump_inner(bump, depth + 1)?;
                }
                Ok(BumpValue::Array(items))
            }
            5 => {
                let len = self.read_argument(additional)?;
                let len_usize = usize::try_from(len)
                    .map_err(|_| CborError::InvalidCbor("length exceeds platform limits".into()))?;
                if len_usize > MAX_COLLECTION_LEN {
                    return Err(CborError::InvalidCbor("map length exceeds maximum".into()));
                }
                let capacity = len_usize.min(self.remaining());
                let entries = bump.alloc_slice_fill_with(capacity, |_| ("", BumpValue::Null));
                let mut prev_key_bytes: &[u8] = &[];
                for entry in entries.iter_mut() {
                    // Decode text key inline (same as standard decoder)
                    let key_start = self.position();
                    let key_byte = self.read_byte()?;
                    let key_major = key_byte >> 5;
                    if key_major != 3 {
                        return Err(CborError::InvalidCbor(
                            "map keys must be text strings".into(),
                        ));
                    }
                    let key_additional = key_byte & 0x1f;
                    let key_len = self.read_argument(key_additional)?;
                    let key_len_usize = usize::try_from(key_len).map_err(|_| {
                        CborError::InvalidCbor("length exceeds platform limits".into())
                    })?;
                    let key_bytes = self.read_slice(key_len_usize)?;
                    let key = match simdutf8::basic::from_utf8(key_bytes) {
                        Ok(s) => s,
                        Err(_) => {
                            return Err(CborError::InvalidCbor(
                                "invalid UTF-8 in text string".into(),
                            ));
                        }
                    };
                    let key_encoded = &self.raw_input()[key_start..self.position()];

                    // Check canonical order
                    if !prev_key_bytes.is_empty() {
                        match prev_key_bytes.cmp(key_encoded) {
                            std::cmp::Ordering::Greater => {
                                return Err(CborError::InvalidCbor(
                                    "map keys not in canonical sort order".into(),
                                ));
                            }
                            std::cmp::Ordering::Equal => {
                                return Err(CborError::InvalidCbor("duplicate map key".into()));
                            }
                            std::cmp::Ordering::Less => {}
                        }
                    }
                    prev_key_bytes = key_encoded;

                    let value = self.decode_bump_inner(bump, depth + 1)?;
                    *entry = (key, value);
                }
                Ok(BumpValue::Map(entries))
            }
            6 => {
                let tag_num = self.read_argument(additional)?;
                if tag_num != 42 {
                    return Err(CborError::InvalidCbor(format!(
                        "unsupported CBOR tag: {tag_num} (only tag 42 is allowed)"
                    )));
                }
                let inner = self.decode_bump_inner(bump, depth)?;
                let bytes = match inner {
                    BumpValue::Bytes(b) => b,
                    _ => {
                        return Err(CborError::InvalidCbor(
                            "tag 42 must wrap a bytestring".into(),
                        ));
                    }
                };
                let cid = Cid::from_tag42_bytes(bytes)?;
                Ok(BumpValue::Cid(cid))
            }
            7 => match additional {
                20 => Ok(BumpValue::Bool(false)),
                21 => Ok(BumpValue::Bool(true)),
                22 => Ok(BumpValue::Null),
                24 => Err(CborError::InvalidCbor("unsupported simple value".into())),
                25 => Err(CborError::InvalidCbor(
                    "half-precision floats not allowed in DRISL".into(),
                )),
                26 => Err(CborError::InvalidCbor(
                    "single-precision floats not allowed in DRISL".into(),
                )),
                27 => {
                    let bytes = self.read_fixed::<8>()?;
                    let val = f64::from_bits(u64::from_be_bytes(bytes));
                    if val.is_nan() {
                        return Err(CborError::InvalidCbor("NaN not allowed in DRISL".into()));
                    }
                    if val.is_infinite() {
                        return Err(CborError::InvalidCbor(
                            "Infinity not allowed in DRISL".into(),
                        ));
                    }
                    Ok(BumpValue::Float(val))
                }
                31 => Err(CborError::InvalidCbor(
                    "indefinite length not allowed in DRISL".into(),
                )),
                _ => Err(CborError::InvalidCbor(format!(
                    "unsupported simple value: {additional}"
                ))),
            },
            _ => Err(CborError::InvalidCbor("invalid major type".into())),
        }
    }
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::unreachable
)]
mod tests {
    use super::*;
    use crate::cbor::{Codec, encode_value, value::Value};

    fn decode_bump_one<'bump, 'data>(
        data: &'data [u8],
        bump: &'bump Bump,
    ) -> BumpValue<'bump, 'data> {
        let mut dec = Decoder::new(data);
        let val = dec.decode_bump(bump).unwrap();
        assert!(dec.is_empty(), "trailing data");
        val
    }

    #[test]
    fn bump_decode_unsigned() {
        let bump = Bump::new();
        assert_eq!(decode_bump_one(&[0x00], &bump), BumpValue::Unsigned(0));
        assert_eq!(decode_bump_one(&[0x17], &bump), BumpValue::Unsigned(23));
    }

    #[test]
    fn bump_decode_negative() {
        let bump = Bump::new();
        assert_eq!(decode_bump_one(&[0x20], &bump), BumpValue::Signed(-1));
    }

    #[test]
    fn bump_decode_text() {
        let bump = Bump::new();
        match decode_bump_one(b"\x65hello", &bump) {
            BumpValue::Text(s) => assert_eq!(s, "hello"),
            other => panic!("expected text, got {other:?}"),
        }
    }

    #[test]
    fn bump_decode_bytes() {
        let bump = Bump::new();
        match decode_bump_one(&[0x42, 0xDE, 0xAD], &bump) {
            BumpValue::Bytes(b) => assert_eq!(b, &[0xDE, 0xAD]),
            other => panic!("expected bytes, got {other:?}"),
        }
    }

    #[test]
    fn bump_decode_bool_and_null() {
        let bump = Bump::new();
        assert_eq!(decode_bump_one(&[0xf5], &bump), BumpValue::Bool(true));
        assert_eq!(decode_bump_one(&[0xf4], &bump), BumpValue::Bool(false));
        assert_eq!(decode_bump_one(&[0xf6], &bump), BumpValue::Null);
    }

    #[test]
    fn bump_decode_array() {
        let bump = Bump::new();
        let buf = [0x83, 0x01, 0x02, 0x03];
        match decode_bump_one(&buf, &bump) {
            BumpValue::Array(items) => {
                assert_eq!(items.len(), 3);
                assert_eq!(items[0], BumpValue::Unsigned(1));
                assert_eq!(items[1], BumpValue::Unsigned(2));
                assert_eq!(items[2], BumpValue::Unsigned(3));
            }
            other => panic!("expected array, got {other:?}"),
        }
    }

    #[test]
    fn bump_decode_map() {
        let bump = Bump::new();
        // {"a": 1}
        let buf = [0xa1, 0x61, 0x61, 0x01];
        match decode_bump_one(&buf, &bump) {
            BumpValue::Map(entries) => {
                assert_eq!(entries.len(), 1);
                assert_eq!(entries[0].0, "a");
                assert_eq!(entries[0].1, BumpValue::Unsigned(1));
            }
            other => panic!("expected map, got {other:?}"),
        }
    }

    #[test]
    fn bump_decode_cid() {
        let bump = Bump::new();
        let cid = Cid::compute(Codec::Drisl, b"test");
        let val = Value::Cid(cid);
        let encoded = encode_value(&val).unwrap();
        match decode_bump_one(&encoded, &bump) {
            BumpValue::Cid(decoded_cid) => assert_eq!(decoded_cid, cid),
            other => panic!("expected CID, got {other:?}"),
        }
    }

    #[test]
    fn bump_reject_unsorted_map_keys() {
        let bump = Bump::new();
        // {"b": 1, "a": 2}
        let buf = [0xa2, 0x61, 0x62, 0x01, 0x61, 0x61, 0x02];
        let mut dec = Decoder::new(&buf);
        assert!(dec.decode_bump(&bump).is_err());
    }

    #[test]
    fn bump_reject_indefinite_length() {
        let bump = Bump::new();
        let mut dec = Decoder::new(&[0x9f]); // indefinite array
        assert!(dec.decode_bump(&bump).is_err());
    }

    #[test]
    fn bump_complex_roundtrip_matches_standard() {
        let bump = Bump::new();
        let cid = Cid::compute(Codec::Drisl, b"test");
        let original = Value::Map(vec![
            ("age", Value::Unsigned(30)),
            ("cid", Value::Cid(cid)),
            ("name", Value::Text("alice")),
            (
                "tags",
                Value::Array(vec![Value::Text("rust"), Value::Bool(true), Value::Null]),
            ),
        ]);
        let encoded = encode_value(&original).unwrap();

        // Standard decode
        let std_val = crate::cbor::decode(&encoded).unwrap();

        // Bump decode
        let mut dec = Decoder::new(&encoded);
        let bump_val = dec.decode_bump(&bump).unwrap();
        assert!(dec.is_empty());

        // Verify same structure
        match (&std_val, &bump_val) {
            (Value::Map(std_entries), BumpValue::Map(bump_entries)) => {
                assert_eq!(std_entries.len(), bump_entries.len());
                for (s, b) in std_entries.iter().zip(bump_entries.iter()) {
                    assert_eq!(s.0, b.0, "key mismatch");
                }
            }
            _ => panic!("expected maps"),
        }

        // Re-encode standard and verify bytes match
        let re_encoded = encode_value(&std_val).unwrap();
        assert_eq!(encoded, re_encoded);
    }

    #[test]
    fn bump_deeply_nested_rejects() {
        let bump = Bump::new();
        let mut buf = vec![0x81; 65]; // 65 x "array of 1"
        buf.push(0x00);
        let mut dec = Decoder::new(&buf);
        assert!(dec.decode_bump(&bump).is_err());
    }
}