jetcrab\api/
config.rs

1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub enum OptimizationLevel {
6    None,
7    Basic,
8    Aggressive,
9}
10
11impl Default for OptimizationLevel {
12    fn default() -> Self {
13        Self::Basic
14    }
15}
16
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub enum ModuleSystem {
19    None,
20    CommonJS,
21    ES6,
22}
23
24impl Default for ModuleSystem {
25    fn default() -> Self {
26        Self::ES6
27    }
28}
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub enum SecurityLevel {
32    Permissive,
33    Standard,
34    Strict,
35}
36
37impl Default for SecurityLevel {
38    fn default() -> Self {
39        Self::Standard
40    }
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct MemoryConfig {
45    pub initial_heap_size: usize,
46    pub max_heap_size: usize,
47    pub gc_threshold: usize,
48    pub gc_interval: Duration,
49}
50
51impl Default for MemoryConfig {
52    fn default() -> Self {
53        Self {
54            initial_heap_size: 1024 * 1024,   // 1MB
55            max_heap_size: 100 * 1024 * 1024, // 100MB
56            gc_threshold: 512 * 1024,         // 512KB
57            gc_interval: Duration::from_millis(100),
58        }
59    }
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct EngineConfig {
64    pub optimization_level: OptimizationLevel,
65    pub memory_config: MemoryConfig,
66    pub timeout: Option<Duration>,
67    pub strict_mode: bool,
68    pub module_system: ModuleSystem,
69    pub security_level: SecurityLevel,
70    pub enable_debugging: bool,
71    pub enable_profiling: bool,
72    pub allow_unsafe_operations: bool,
73    pub max_execution_depth: usize,
74    pub max_loop_iterations: usize,
75}
76
77impl Default for EngineConfig {
78    fn default() -> Self {
79        Self {
80            optimization_level: OptimizationLevel::default(),
81            memory_config: MemoryConfig::default(),
82            timeout: Some(Duration::from_secs(30)),
83            strict_mode: false,
84            module_system: ModuleSystem::default(),
85            security_level: SecurityLevel::default(),
86            enable_debugging: false,
87            enable_profiling: false,
88            allow_unsafe_operations: false,
89            max_execution_depth: 1000,
90            max_loop_iterations: 1_000_000,
91        }
92    }
93}
94
95impl EngineConfig {
96    pub fn new() -> Self {
97        Self::default()
98    }
99
100    pub fn with_optimization(mut self, level: OptimizationLevel) -> Self {
101        self.optimization_level = level;
102        self
103    }
104
105    pub fn with_memory_config(mut self, config: MemoryConfig) -> Self {
106        self.memory_config = config;
107        self
108    }
109
110    pub fn with_timeout(mut self, timeout: Duration) -> Self {
111        self.timeout = Some(timeout);
112        self
113    }
114
115    pub fn with_strict_mode(mut self, strict: bool) -> Self {
116        self.strict_mode = strict;
117        self
118    }
119
120    pub fn with_module_system(mut self, system: ModuleSystem) -> Self {
121        self.module_system = system;
122        self
123    }
124
125    pub fn with_security_level(mut self, level: SecurityLevel) -> Self {
126        self.security_level = level;
127        self
128    }
129
130    pub fn with_debugging(mut self, enable: bool) -> Self {
131        self.enable_debugging = enable;
132        self
133    }
134
135    pub fn with_profiling(mut self, enable: bool) -> Self {
136        self.enable_profiling = enable;
137        self
138    }
139
140    pub fn with_execution_limits(mut self, max_depth: usize, max_iterations: usize) -> Self {
141        self.max_execution_depth = max_depth;
142        self.max_loop_iterations = max_iterations;
143        self
144    }
145
146    pub fn is_production_ready(&self) -> bool {
147        !self.enable_debugging
148            && !self.enable_profiling
149            && !self.allow_unsafe_operations
150            && matches!(self.security_level, SecurityLevel::Strict)
151    }
152
153    pub fn validate(&self) -> Result<(), String> {
154        if self.memory_config.initial_heap_size > self.memory_config.max_heap_size {
155            return Err("Initial heap size cannot be larger than max heap size".to_string());
156        }
157
158        if self.memory_config.gc_threshold > self.memory_config.max_heap_size {
159            return Err("GC threshold cannot be larger than max heap size".to_string());
160        }
161
162        if self.max_execution_depth == 0 {
163            return Err("Max execution depth must be greater than 0".to_string());
164        }
165
166        if self.max_loop_iterations == 0 {
167            return Err("Max loop iterations must be greater than 0".to_string());
168        }
169
170        Ok(())
171    }
172}
173
174#[cfg(test)]
175mod tests {
176    use super::*;
177
178    #[test]
179    fn test_default_config() {
180        let config = EngineConfig::default();
181        assert!(config.validate().is_ok());
182    }
183
184    #[test]
185    fn test_config_builder() {
186        let config = EngineConfig::new()
187            .with_optimization(OptimizationLevel::Aggressive)
188            .with_strict_mode(true)
189            .with_timeout(Duration::from_secs(60));
190
191        assert_eq!(config.optimization_level, OptimizationLevel::Aggressive);
192        assert!(config.strict_mode);
193        assert_eq!(config.timeout, Some(Duration::from_secs(60)));
194    }
195
196    #[test]
197    fn test_memory_config_validation() {
198        let mut memory_config = MemoryConfig::default();
199        memory_config.initial_heap_size = 200 * 1024 * 1024; // 200MB
200        memory_config.max_heap_size = 100 * 1024 * 1024; // 100MB
201
202        let config = EngineConfig::new().with_memory_config(memory_config);
203        assert!(config.validate().is_err());
204    }
205
206    #[test]
207    fn test_production_ready() {
208        let production_config = EngineConfig::new()
209            .with_security_level(SecurityLevel::Strict)
210            .with_debugging(false)
211            .with_profiling(false);
212
213        assert!(production_config.is_production_ready());
214    }
215}