oats_framework/
lib.rs

1//! OATS - Objects • Actions • Traits • Systems
2//! 
3//! Universal architecture pattern for infinite scale across any domain.
4//! 
5//! This library provides the core abstractions and implementations for the OATS pattern:
6//! - **Objects**: Identity containers that compose traits
7//! - **Actions**: Stateless logic that reads traits and returns updates
8//! - **Traits**: Immutable state containing domain data
9//! - **Systems**: Orchestration that coordinates actions and manages resources
10
11pub mod objects;
12pub mod actions;
13pub mod traits;
14pub mod systems;
15pub mod error;
16
17// Re-export main types for convenience
18pub use objects::Object;
19pub use actions::{Action, ActionContext, ActionResult};
20pub use traits::{Trait, TraitData};
21pub use systems::{System, SystemManager, Priority};
22pub use error::OatsError;
23
24/// Result type for OATS operations
25pub type Result<T> = std::result::Result<T, OatsError>;
26
27/// Core OATS system that orchestrates all components
28#[derive(Default)]
29pub struct OatsSystem {
30    objects: Vec<Object>,
31    actions: Vec<Box<dyn Action>>,
32    systems: Vec<Box<dyn System>>,
33}
34
35impl OatsSystem {
36    /// Create a new OATS system
37    #[inline]
38    pub fn new() -> Self {
39        Self::default()
40    }
41
42    /// Create a new OATS system with pre-allocated capacity
43    pub fn with_capacity(objects: usize, actions: usize, systems: usize) -> Self {
44        Self {
45            objects: Vec::with_capacity(objects),
46            actions: Vec::with_capacity(actions),
47            systems: Vec::with_capacity(systems),
48        }
49    }
50
51    /// Add an object to the system
52    #[inline]
53    pub fn add_object(&mut self, object: Object) {
54        self.objects.push(object);
55    }
56
57    /// Add multiple objects efficiently
58    pub fn add_objects(&mut self, objects: impl IntoIterator<Item = Object>) {
59        self.objects.extend(objects);
60    }
61
62    /// Add an action to the system
63    #[inline]
64    pub fn add_action(&mut self, action: Box<dyn Action>) {
65        self.actions.push(action);
66    }
67
68    /// Add a system to the system
69    #[inline]
70    pub fn add_system(&mut self, system: Box<dyn System>) {
71        self.systems.push(system);
72    }
73
74    /// Get all objects in the system
75    #[inline]
76    pub fn objects(&self) -> &[Object] {
77        &self.objects
78    }
79
80    /// Get all actions in the system
81    #[inline]
82    pub fn actions(&self) -> &[Box<dyn Action>] {
83        &self.actions
84    }
85
86    /// Get all systems in the system
87    #[inline]
88    pub fn systems(&self) -> &[Box<dyn System>] {
89        &self.systems
90    }
91
92    /// Get object count
93    #[inline]
94    pub fn object_count(&self) -> usize {
95        self.objects.len()
96    }
97
98    /// Get action count
99    #[inline]
100    pub fn action_count(&self) -> usize {
101        self.actions.len()
102    }
103
104    /// Get system count
105    #[inline]
106    pub fn system_count(&self) -> usize {
107        self.systems.len()
108    }
109
110    /// Clear all objects
111    #[inline]
112    pub fn clear_objects(&mut self) {
113        self.objects.clear();
114    }
115
116    /// Clear all actions
117    #[inline]
118    pub fn clear_actions(&mut self) {
119        self.actions.clear();
120    }
121
122    /// Clear all systems
123    #[inline]
124    pub fn clear_systems(&mut self) {
125        self.systems.clear();
126    }
127
128    /// Reserve capacity for objects
129    #[inline]
130    pub fn reserve_objects(&mut self, additional: usize) {
131        self.objects.reserve(additional);
132    }
133
134    /// Reserve capacity for actions
135    #[inline]
136    pub fn reserve_actions(&mut self, additional: usize) {
137        self.actions.reserve(additional);
138    }
139
140    /// Reserve capacity for systems
141    #[inline]
142    pub fn reserve_systems(&mut self, additional: usize) {
143        self.systems.reserve(additional);
144    }
145}
146
147#[cfg(test)]
148mod tests {
149    use super::*;
150
151    #[test]
152    fn test_oats_system_creation() {
153        let system = OatsSystem::new();
154        assert_eq!(system.object_count(), 0);
155        assert_eq!(system.action_count(), 0);
156        assert_eq!(system.system_count(), 0);
157    }
158
159    #[test]
160    fn test_oats_system_with_capacity() {
161        let system = OatsSystem::with_capacity(100, 10, 5);
162        assert_eq!(system.object_count(), 0);
163        assert_eq!(system.action_count(), 0);
164        assert_eq!(system.system_count(), 0);
165    }
166
167    #[test]
168    fn test_oats_system_operations() {
169        let mut system = OatsSystem::new();
170        
171        // Test object operations
172        let obj = Object::new("test", "type");
173        system.add_object(obj);
174        assert_eq!(system.object_count(), 1);
175        
176        // Test bulk object operations
177        let objects = vec![
178            Object::new("obj1", "type"),
179            Object::new("obj2", "type"),
180        ];
181        system.add_objects(objects);
182        assert_eq!(system.object_count(), 3);
183        
184        // Test capacity reservation
185        system.reserve_objects(100);
186        assert!(system.objects.capacity() >= 103);
187    }
188}