ozzy-proto 0.3.0

Sans-I/O application protocol and message types for Ozzy.
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
//! Bounds-checked big-endian fields for wire and file layouts.
//!
//! [`Reader`] and [`Writer`] walk counted layouts and fail with a
//! [`LayoutError`] where the bytes run out; each codec converts that into its
//! own error. [`Fixed`] reads and writes the fields of a fixed-size record at
//! offsets checked when the code is built, so a record whose length was checked
//! once needs no further checks.

/// A layout that does not fit its bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum LayoutError {
    /// The input ends before a field.
    #[error("layout is truncated: need {needed} bytes, have {available}")]
    Truncated {
        /// Input length the field needs.
        needed: usize,
        /// Input length.
        available: usize,
    },
    /// Bytes remain after the last field.
    #[error("layout has {0} trailing bytes")]
    Trailing(usize),
    /// The output cannot hold a field.
    #[error("layout overflows its output: need {needed} bytes, have {capacity}")]
    Overflow {
        /// Output length the field needs.
        needed: usize,
        /// Output length.
        capacity: usize,
    },
}

/// Sequential reader over borrowed bytes.
#[derive(Debug, Clone, Copy)]
pub struct Reader<'a> {
    rest: &'a [u8],
    len: usize,
}

impl<'a> Reader<'a> {
    /// Read `input` from its start.
    #[must_use]
    pub const fn new(input: &'a [u8]) -> Self {
        Self {
            rest: input,
            len: input.len(),
        }
    }

    /// Bytes consumed so far.
    #[must_use]
    #[expect(
        clippy::arithmetic_side_effects,
        reason = "rest is a suffix of the original input"
    )]
    pub const fn offset(&self) -> usize {
        self.len - self.rest.len()
    }

    /// Bytes not yet consumed.
    #[must_use]
    pub const fn rest(&self) -> &'a [u8] {
        self.rest
    }

    /// The next `count` bytes.
    pub fn take(&mut self, count: usize) -> Result<&'a [u8], LayoutError> {
        let (bytes, rest) = self
            .rest
            .split_at_checked(count)
            .ok_or_else(|| self.truncated(count))?;
        self.rest = rest;
        Ok(bytes)
    }

    /// The next `N` bytes, borrowed.
    pub fn chunk<const N: usize>(&mut self) -> Result<&'a [u8; N], LayoutError> {
        let (bytes, rest) = self
            .rest
            .split_first_chunk()
            .ok_or_else(|| self.truncated(N))?;
        self.rest = rest;
        Ok(bytes)
    }

    /// The next `N` bytes, copied.
    pub fn array<const N: usize>(&mut self) -> Result<[u8; N], LayoutError> {
        self.chunk().copied()
    }

    /// The next byte.
    pub fn u8(&mut self) -> Result<u8, LayoutError> {
        let [byte] = self.array()?;
        Ok(byte)
    }

    /// The next big-endian `u16`.
    pub fn u16(&mut self) -> Result<u16, LayoutError> {
        self.array().map(u16::from_be_bytes)
    }

    /// The next big-endian `u32`.
    pub fn u32(&mut self) -> Result<u32, LayoutError> {
        self.array().map(u32::from_be_bytes)
    }

    /// The next big-endian `u64`.
    pub fn u64(&mut self) -> Result<u64, LayoutError> {
        self.array().map(u64::from_be_bytes)
    }

    /// Require that every byte was consumed.
    pub const fn finish(self) -> Result<(), LayoutError> {
        if self.rest.is_empty() {
            Ok(())
        } else {
            Err(LayoutError::Trailing(self.rest.len()))
        }
    }

    fn truncated(&self, count: usize) -> LayoutError {
        LayoutError::Truncated {
            needed: self.offset().saturating_add(count),
            available: self.len,
        }
    }
}

/// Sequential writer into a caller-sized buffer.
#[derive(Debug)]
pub struct Writer<'a> {
    output: &'a mut [u8],
    offset: usize,
}

impl<'a> Writer<'a> {
    /// Write `output` from its start.
    pub const fn new(output: &'a mut [u8]) -> Self {
        Self { output, offset: 0 }
    }

    /// Write the first `len` bytes of `output`, or fail when it is shorter.
    pub fn with_len(output: &'a mut [u8], len: usize) -> Result<Self, LayoutError> {
        let capacity = output.len();
        let output = output.get_mut(..len).ok_or(LayoutError::Overflow {
            needed: len,
            capacity,
        })?;
        Ok(Self::new(output))
    }

    /// Bytes written so far.
    #[must_use]
    pub const fn offset(&self) -> usize {
        self.offset
    }

    /// Write `bytes` next, or nothing when they do not fit.
    pub fn bytes(&mut self, bytes: &[u8]) -> Result<(), LayoutError> {
        let overflow = LayoutError::Overflow {
            needed: self.offset.saturating_add(bytes.len()),
            capacity: self.output.len(),
        };
        let end = self.offset.checked_add(bytes.len()).ok_or(overflow)?;
        let field = self.output.get_mut(self.offset..end).ok_or(overflow)?;
        field.copy_from_slice(bytes);
        self.offset = end;
        Ok(())
    }

    /// Write one byte.
    pub fn u8(&mut self, value: u8) -> Result<(), LayoutError> {
        self.bytes(&[value])
    }

    /// Write a big-endian `u16`.
    pub fn u16(&mut self, value: u16) -> Result<(), LayoutError> {
        self.bytes(&value.to_be_bytes())
    }

    /// Write a big-endian `u32`.
    pub fn u32(&mut self, value: u32) -> Result<(), LayoutError> {
        self.bytes(&value.to_be_bytes())
    }

    /// Write a big-endian `u64`.
    pub fn u64(&mut self, value: u64) -> Result<(), LayoutError> {
        self.bytes(&value.to_be_bytes())
    }
}

/// The `N`-byte record at `at`.
pub fn record<const N: usize>(input: &[u8], at: usize) -> Result<&[u8; N], LayoutError> {
    input
        .get(at..)
        .and_then(<[u8]>::first_chunk)
        .ok_or(LayoutError::Truncated {
            needed: at.saturating_add(N),
            available: input.len(),
        })
}

/// The mutable `N`-byte record at `at`.
pub fn record_mut<const N: usize>(
    output: &mut [u8],
    at: usize,
) -> Result<&mut [u8; N], LayoutError> {
    let capacity = output.len();
    output
        .get_mut(at..)
        .and_then(<[u8]>::first_chunk_mut)
        .ok_or(LayoutError::Overflow {
            needed: at.saturating_add(N),
            capacity,
        })
}

/// Overwrite the `N` bytes at `at`, as when filling a placeholder.
pub fn patch<const N: usize>(
    output: &mut [u8],
    at: usize,
    bytes: [u8; N],
) -> Result<(), LayoutError> {
    *record_mut(output, at)? = bytes;
    Ok(())
}

/// Fields of a fixed-size record at offsets checked when the code is built.
///
/// A field that does not fit the record fails the build, so the accessors
/// cannot fail. Get the record from a slice with `first_chunk`,
/// `split_first_chunk`, or `as_chunks`.
pub trait Fixed {
    /// The `M` bytes at `AT`, copied.
    fn array<const AT: usize, const M: usize>(&self) -> [u8; M];

    /// The `M` bytes at `AT`, borrowed.
    fn slice<const AT: usize, const M: usize>(&self) -> &[u8];

    /// Overwrite the `M` bytes at `AT`.
    fn put<const AT: usize, const M: usize>(&mut self, bytes: [u8; M]);

    /// The byte at `AT`.
    fn u8_at<const AT: usize>(&self) -> u8 {
        let [byte] = self.array::<AT, 1>();
        byte
    }

    /// The big-endian `u16` at `AT`.
    fn u16_at<const AT: usize>(&self) -> u16 {
        u16::from_be_bytes(self.array::<AT, 2>())
    }

    /// The big-endian `u32` at `AT`.
    fn u32_at<const AT: usize>(&self) -> u32 {
        u32::from_be_bytes(self.array::<AT, 4>())
    }

    /// The big-endian `u64` at `AT`.
    fn u64_at<const AT: usize>(&self) -> u64 {
        u64::from_be_bytes(self.array::<AT, 8>())
    }

    /// Write the byte at `AT`.
    fn put_u8<const AT: usize>(&mut self, value: u8) {
        self.put::<AT, 1>([value]);
    }

    /// Write a big-endian `u16` at `AT`.
    fn put_u16<const AT: usize>(&mut self, value: u16) {
        self.put::<AT, 2>(value.to_be_bytes());
    }

    /// Write a big-endian `u32` at `AT`.
    fn put_u32<const AT: usize>(&mut self, value: u32) {
        self.put::<AT, 4>(value.to_be_bytes());
    }

    /// Write a big-endian `u64` at `AT`.
    fn put_u64<const AT: usize>(&mut self, value: u64) {
        self.put::<AT, 8>(value.to_be_bytes());
    }
}

// Each body asserts its bounds at build time; the `None` arms are dead.
impl<const N: usize> Fixed for [u8; N] {
    fn array<const AT: usize, const M: usize>(&self) -> [u8; M] {
        const { assert!(AT + M <= N, "field exceeds the record") };
        self.get(AT..)
            .and_then(<[u8]>::first_chunk)
            .copied()
            .unwrap_or([0; M])
    }

    fn slice<const AT: usize, const M: usize>(&self) -> &[u8] {
        const { assert!(AT + M <= N, "field exceeds the record") };
        self.get(AT..)
            .and_then(|tail| tail.get(..M))
            .unwrap_or_default()
    }

    fn put<const AT: usize, const M: usize>(&mut self, bytes: [u8; M]) {
        const { assert!(AT + M <= N, "field exceeds the record") };
        if let Some(field) = self.get_mut(AT..).and_then(<[u8]>::first_chunk_mut) {
            *field = bytes;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reader_takes_fields_in_order_and_stops_at_the_end() {
        let input = [1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6];
        let short = |needed| LayoutError::Truncated {
            needed,
            available: 17,
        };
        let mut reader = Reader::new(&input);
        assert_eq!(reader.u8(), Ok(1));
        assert_eq!(reader.u16(), Ok(2));
        assert_eq!(reader.u32(), Ok(3));
        assert_eq!(reader.u64(), Ok(4));
        assert_eq!(reader.offset(), 15);
        assert_eq!(reader.finish(), Err(LayoutError::Trailing(2)));
        assert_eq!(reader.array::<3>(), Err(short(18)));
        assert_eq!(reader.rest(), &[5, 6]);
        assert_eq!(reader.take(2), Ok(&[5, 6][..]));
        assert_eq!(reader.u8(), Err(short(18)));
        assert_eq!(reader.take(usize::MAX), Err(short(usize::MAX)));
        assert_eq!(reader.finish(), Ok(()));
    }

    #[test]
    fn writer_fills_its_buffer_and_refuses_overflow() {
        let mut output = [0; 7];
        let mut writer = Writer::new(&mut output);
        assert_eq!(writer.u8(1), Ok(()));
        assert_eq!(writer.u16(2), Ok(()));
        assert_eq!(
            writer.u64(3),
            Err(LayoutError::Overflow {
                needed: 11,
                capacity: 7
            })
        );
        assert_eq!(writer.u32(4), Ok(()));
        assert_eq!(writer.offset(), 7);
        assert!(writer.u8(5).is_err());
        assert_eq!(output, [1, 0, 2, 0, 0, 0, 4]);
        assert!(Writer::with_len(&mut output, 8).is_err());
        let mut writer = Writer::with_len(&mut output, 1).unwrap();
        assert!(writer.u16(9).is_err());
    }

    #[test]
    fn fixed_fields_round_trip_at_their_offsets() {
        let mut record = [0; 16];
        record.put_u8::<0>(1);
        record.put_u16::<1>(2);
        record.put_u32::<3>(3);
        record.put_u64::<7>(4);
        record.put::<15, 1>([9]);
        assert_eq!(record, [1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 9]);
        assert_eq!(record.u8_at::<0>(), 1);
        assert_eq!(record.u16_at::<1>(), 2);
        assert_eq!(record.u32_at::<3>(), 3);
        assert_eq!(record.u64_at::<7>(), 4);
        assert_eq!(record.array::<14, 2>(), [4, 9]);
        assert_eq!(record.slice::<13, 3>(), &[0, 4, 9]);
    }

    #[test]
    fn records_are_bounded_by_their_input() {
        let mut bytes = [1, 2, 3, 4, 5];
        assert_eq!(record::<2>(&bytes, 3), Ok(&[4, 5]));
        assert_eq!(
            record::<2>(&bytes, 4),
            Err(LayoutError::Truncated {
                needed: 6,
                available: 5
            })
        );
        assert!(record::<1>(&bytes, usize::MAX).is_err());
        record_mut::<2>(&mut bytes, 0).unwrap().put_u16::<0>(0x0909);
        assert_eq!(bytes, [9, 9, 3, 4, 5]);
        assert!(record_mut::<6>(&mut bytes, 0).is_err());
    }

    #[test]
    fn patch_fills_a_placeholder_inside_its_output() {
        let mut output = [0; 6];
        assert_eq!(patch(&mut output, 2, [1, 2, 3, 4]), Ok(()));
        assert_eq!(output, [0, 0, 1, 2, 3, 4]);
        assert_eq!(
            patch(&mut output, 3, [5, 6, 7, 8]),
            Err(LayoutError::Overflow {
                needed: 7,
                capacity: 6
            })
        );
        assert!(patch(&mut output, usize::MAX, [9]).is_err());
        assert_eq!(output, [0, 0, 1, 2, 3, 4]);
    }
}