Skip to main content

seyd_wire/
lib.rs

1// Copyright 2026 Anton Gravestam
2// SPDX-License-Identifier: Apache-2.0
3//! Seyd wire protocol — see `docs/adr/0001-wire-protocol-v2.md`.
4//!
5//! This crate is pure data layout: no I/O, no FEC, no policy. Both ends of a
6//! link (Rust agent, TypeScript pilot) derive their parsers from the same ADR,
7//! and the version nibble makes every format change a hard cutover: unknown
8//! versions are counted and dropped, never guessed at.
9
10pub mod v1;
11pub mod v2;
12
13/// Wrap-aware signed difference of two 16-bit sequence numbers (`a - b`).
14///
15/// Every frame-id comparison must go through this. The prototype had a real
16/// bug from unsigned arithmetic: one reordered chunk from an older frame made
17/// the eviction sweep compute `(old - current) & 0xFFFF ≈ 65500` for the frame
18/// being assembled and delete it.
19#[inline]
20pub fn seq_delta(a: u16, b: u16) -> i16 {
21    a.wrapping_sub(b) as i16
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn seq_delta_wraps() {
30        assert_eq!(seq_delta(5, 3), 2);
31        assert_eq!(seq_delta(3, 5), -2);
32        assert_eq!(seq_delta(2, 65535), 3);
33        assert_eq!(seq_delta(65535, 2), -3);
34    }
35}