Skip to main content

driven/binary/
rule_schema.rs

1//! Zero-Copy Rule Schema
2//!
3//! Binary rule structures using bytemuck for zero-copy access.
4
5use bytemuck::{Pod, Zeroable};
6
7pub use super::infinity_format::{RuleFlags, SectionOffsets};
8
9/// Binary rule entry (12 bytes, cache-line friendly)
10#[repr(C)]
11#[derive(Debug, Clone, Copy, Pod, Zeroable)]
12pub struct BinaryRule {
13    /// Unique rule ID (like StyleId in B-CSS)
14    pub rule_id: u16,
15    /// Category (u8 enum)
16    pub category: u8,
17    /// Priority (0-255)
18    pub priority: u8,
19    /// Pattern string index
20    pub pattern: u32,
21    /// Description string index
22    pub description: u32,
23}
24
25impl BinaryRule {
26    /// Create a new binary rule
27    pub fn new(rule_id: u16, category: u8, priority: u8, pattern: u32, description: u32) -> Self {
28        Self {
29            rule_id,
30            category,
31            priority,
32            pattern,
33            description,
34        }
35    }
36
37    /// Size in bytes
38    pub const fn size() -> usize {
39        std::mem::size_of::<Self>()
40    }
41}
42
43/// Binary workflow step (20 bytes, no padding)
44#[repr(C)]
45#[derive(Debug, Clone, Copy, Pod, Zeroable)]
46pub struct BinaryStep {
47    /// Step ID (2 bytes)
48    pub step_id: u16,
49    /// Number of actions (2 bytes)
50    pub action_count: u16,
51    /// Name string index (4 bytes)
52    pub name: u32,
53    /// Description string index (4 bytes)
54    pub description: u32,
55    /// Condition string index (4 bytes, 0 = no condition)
56    pub condition: u32,
57    /// Reserved (4 bytes)
58    pub _reserved: u32,
59}
60
61impl BinaryStep {
62    /// Create a new binary step
63    pub fn new(step_id: u16, name: u32, description: u32) -> Self {
64        Self {
65            step_id,
66            action_count: 0,
67            name,
68            description,
69            condition: 0,
70            _reserved: 0,
71        }
72    }
73
74    /// Size in bytes
75    pub const fn size() -> usize {
76        std::mem::size_of::<Self>()
77    }
78}
79
80/// Binary persona section header
81#[repr(C)]
82#[derive(Debug, Clone, Copy, Pod, Zeroable)]
83pub struct BinaryPersona {
84    /// Name string index
85    pub name: u32,
86    /// Role string index
87    pub role: u32,
88    /// Identity string index (0 = none)
89    pub identity: u32,
90    /// Style string index (0 = none)
91    pub style: u32,
92    /// Expertise level (0-10)
93    pub expertise_level: u8,
94    /// Number of traits
95    pub trait_count: u8,
96    /// Number of principles
97    pub principle_count: u8,
98    /// Behavior flags
99    pub behavior_flags: u8,
100}
101
102impl BinaryPersona {
103    /// Size in bytes
104    pub const fn size() -> usize {
105        std::mem::size_of::<Self>()
106    }
107}
108
109/// Binary standards section header
110#[repr(C)]
111#[derive(Debug, Clone, Copy, Pod, Zeroable)]
112pub struct BinaryStandardsHeader {
113    /// Number of rules
114    pub rule_count: u16,
115    /// Category table offset (relative)
116    pub category_table_offset: u16,
117    /// Priority index offset (relative)
118    pub priority_index_offset: u32,
119}
120
121impl BinaryStandardsHeader {
122    /// Size in bytes
123    pub const fn size() -> usize {
124        std::mem::size_of::<Self>()
125    }
126}
127
128/// Binary context section
129#[repr(C)]
130#[derive(Debug, Clone, Copy, Pod, Zeroable)]
131pub struct BinaryContext {
132    /// Number of include patterns
133    pub include_count: u16,
134    /// Number of exclude patterns
135    pub exclude_count: u16,
136    /// Number of focus areas
137    pub focus_count: u16,
138    /// Reserved for alignment
139    pub _reserved: u16,
140}
141
142impl BinaryContext {
143    /// Size in bytes
144    pub const fn size() -> usize {
145        std::mem::size_of::<Self>()
146    }
147}
148
149/// Binary signature (100 bytes)
150#[repr(C)]
151#[derive(Debug, Clone, Copy, Pod, Zeroable)]
152pub struct BinarySignature {
153    /// Algorithm (1 = Ed25519)
154    pub algorithm: u8,
155    /// Reserved (3 bytes for alignment)
156    pub _reserved: [u8; 3],
157    /// Public key (32 bytes)
158    pub public_key: [u8; 32],
159    /// Signature (64 bytes)
160    pub signature: [u8; 64],
161}
162
163impl BinarySignature {
164    /// Ed25519 algorithm ID
165    pub const ED25519: u8 = 1;
166
167    /// Size in bytes
168    pub const fn size() -> usize {
169        std::mem::size_of::<Self>()
170    }
171
172    /// Create an empty signature
173    pub fn empty() -> Self {
174        Self {
175            algorithm: 0,
176            _reserved: [0; 3],
177            public_key: [0; 32],
178            signature: [0; 64],
179        }
180    }
181}
182
183#[cfg(test)]
184mod tests {
185    use super::*;
186
187    #[test]
188    fn test_binary_rule_size() {
189        assert_eq!(BinaryRule::size(), 12);
190    }
191
192    #[test]
193    fn test_binary_step_size() {
194        assert_eq!(BinaryStep::size(), 20);
195    }
196
197    #[test]
198    fn test_binary_persona_size() {
199        assert_eq!(BinaryPersona::size(), 20);
200    }
201
202    #[test]
203    fn test_binary_signature_size() {
204        // 1 + 3 + 32 + 64 = 100, but might be padded
205        assert!(BinarySignature::size() >= 100);
206    }
207}