Skip to main content

schema/
lib.rs

1//! Replication schema and field codec definitions for the sdec codec.
2//!
3//! This crate defines how game state is represented for replication:
4//! - Schema model for entity types, components, and fields
5//! - Field codecs (bool, integers, fixed-point, varints)
6//! - Quantization and threshold configuration
7//! - Deterministic schema hashing
8//!
9//! # Design Principles
10//!
11//! - **Runtime-first** - v0 uses runtime schema building, derive macros come later.
12//! - **Explicit schemas** - No reflection on arbitrary Rust types.
13//! - **Deterministic hashing** - Schema hash is stable given the same definition.
14
15mod field;
16mod hash;
17
18pub use field::{FieldCodec, FieldKind};
19pub use hash::schema_hash;
20
21/// A component ID within a schema.
22pub type ComponentId = u16;
23
24/// A field ID within a component.
25pub type FieldId = u16;
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use std::mem::size_of;
31
32    #[test]
33    fn public_api_exports() {
34        // Verify all expected items are exported
35        let _ = FieldCodec::bool();
36        let _ = FieldKind::Bool;
37        let _ = schema_hash(&[]);
38
39        // Type aliases
40        let _: ComponentId = 0;
41        let _: FieldId = 0;
42    }
43
44    #[test]
45    fn field_codec_basic_usage() {
46        let codec = FieldCodec::bool();
47        assert!(matches!(codec.kind, FieldKind::Bool));
48    }
49
50    #[test]
51    fn schema_hash_stub() {
52        assert_eq!(schema_hash(&[1, 2, 3]), 0);
53    }
54
55    #[test]
56    fn component_id_and_field_id_sizes() {
57        // Verify the type sizes match WIRE_FORMAT.md
58        assert_eq!(size_of::<ComponentId>(), 2);
59        assert_eq!(size_of::<FieldId>(), 2);
60    }
61}