Skip to main content

wire/
lib.rs

1//! Wire framing and packet layout for the sdec codec.
2//!
3//! This crate handles the binary wire format: packet headers, section framing,
4//! and limit enforcement. It does not know about game state types—only the
5//! structure of packets.
6//!
7//! # Design Principles
8//!
9//! - **Stable wire format** - The format is versioned and changes are documented.
10//! - **Bounded decoding** - All length fields are validated against limits before iteration.
11//! - **No domain knowledge** - This crate handles framing, not game logic.
12//!
13//! See `WIRE_FORMAT.md` for the complete specification.
14
15mod error;
16mod header;
17mod limits;
18
19pub use error::{WireError, WireResult};
20pub use header::{PacketFlags, PacketHeader, HEADER_SIZE, MAGIC, VERSION};
21pub use limits::Limits;
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use std::mem::size_of;
27
28    #[test]
29    fn public_api_exports() {
30        // Verify all expected items are exported
31        let _ = MAGIC;
32        let _ = VERSION;
33        let _ = HEADER_SIZE;
34        let _ = PacketFlags::full_snapshot();
35        let _ = PacketHeader::full_snapshot(0, 0, 0);
36        let _ = Limits::default();
37
38        // Error types
39        let _: WireResult<()> = Ok(());
40    }
41
42    #[test]
43    fn limits_default_is_reasonable() {
44        let limits = Limits::default();
45        // Should be able to handle typical FPS scenarios
46        assert!(
47            limits.max_packet_bytes >= 1024,
48            "should allow at least 1KB packets"
49        );
50        assert!(
51            limits.max_entities_update >= 64,
52            "should allow at least 64 entity updates"
53        );
54    }
55
56    #[test]
57    fn header_size_constant_correct() {
58        // Sanity check the header size calculation
59        assert_eq!(
60            HEADER_SIZE,
61            size_of::<u32>() // magic
62                + size_of::<u16>() // version
63                + size_of::<u16>() // flags
64                + size_of::<u64>() // schema_hash
65                + size_of::<u32>() // tick
66                + size_of::<u32>() // baseline_tick
67                + size_of::<u32>() // payload_len
68        );
69    }
70
71    #[test]
72    fn packet_flags_and_header_integration() {
73        let flags = PacketFlags::delta_snapshot();
74        let header = PacketHeader::delta_snapshot(0x1234, 100, 95, 512);
75
76        assert_eq!(header.flags, flags);
77        assert!(header.flags.is_valid_v0());
78    }
79}