Skip to main content

synapse_primitives/
lib.rs

1//! Synapse Core - Encoding utilities and optimizations
2//!
3//! This crate provides efficient encoding utilities for the Synapse gateway system:
4//!
5//! - **id**: Strongly-typed identifiers (ServiceId, InterfaceId, MethodId, etc.)
6//! - **siphash**: Name-to-ID mapping using SipHash for stable numeric identifiers
7//! - **semver**: Decimal semantic version packing into u32
8//! - **flags**: Bitfield flag packing for efficient boolean storage
9//! - **binary**: Fixed-size binary encodings (UUIDs, timestamps)
10
11pub mod flags;
12pub mod id;
13pub mod semver;
14pub mod siphash;
15
16// Re-export commonly used types
17pub use flags::{Flags32, Flags64};
18pub use id::{HeaderKeyId, InstanceId, InterfaceId, MethodId, MetricId, ServiceId};
19pub use semver::{PackedVersion, VersionError};
20pub use uuid::Uuid;
21
22/// Convert a UUID to the wire type
23pub fn uuid_bytes(id: Uuid) -> bytes::Bytes {
24    bytes::Bytes::copy_from_slice(id.as_bytes())
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_integration_example() {
33        // Interface ID from name
34        let interface_id = siphash::hash_name_u32("mensa.user.v2.UserInterface");
35        assert_ne!(interface_id, 0);
36
37        // Version packing
38        let version = semver::pack_version(2, 3, 1).unwrap();
39        assert_eq!(version.as_u32(), 2_003_001);
40
41        // Flags
42        let mut flags = Flags64::new();
43        flags.set(0, true);
44        assert!(flags.get(0));
45
46        // UUID
47        let uuid = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
48        assert_eq!(uuid.as_bytes().len(), 16);
49    }
50}