fusabi_stdlib_ext/
config.rs

1//! Configuration for stdlib modules.
2
3use std::time::Duration;
4
5use crate::safety::SafetyConfig;
6
7/// Configuration for a specific module.
8#[derive(Debug, Clone)]
9pub struct ModuleConfig {
10    /// Whether the module is enabled.
11    pub enabled: bool,
12    /// Default timeout for operations.
13    pub timeout: Option<Duration>,
14    /// Custom configuration options.
15    pub options: std::collections::HashMap<String, String>,
16}
17
18impl Default for ModuleConfig {
19    fn default() -> Self {
20        Self {
21            enabled: true,
22            timeout: Some(Duration::from_secs(30)),
23            options: std::collections::HashMap::new(),
24        }
25    }
26}
27
28impl ModuleConfig {
29    /// Create a new module configuration.
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Disable the module.
35    pub fn disabled() -> Self {
36        Self {
37            enabled: false,
38            ..Self::default()
39        }
40    }
41
42    /// Set enabled state.
43    pub fn with_enabled(mut self, enabled: bool) -> Self {
44        self.enabled = enabled;
45        self
46    }
47
48    /// Set timeout.
49    pub fn with_timeout(mut self, timeout: Duration) -> Self {
50        self.timeout = Some(timeout);
51        self
52    }
53
54    /// Remove timeout limit.
55    pub fn no_timeout(mut self) -> Self {
56        self.timeout = None;
57        self
58    }
59
60    /// Set a custom option.
61    pub fn with_option(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
62        self.options.insert(key.into(), value.into());
63        self
64    }
65}
66
67/// Configuration for the stdlib registry.
68#[derive(Debug, Clone)]
69pub struct StdlibConfig {
70    /// Safety configuration.
71    pub safety: SafetyConfig,
72
73    /// Process module configuration.
74    pub process: ModuleConfig,
75
76    /// Filesystem module configuration.
77    pub fs: ModuleConfig,
78
79    /// Path module configuration.
80    pub path: ModuleConfig,
81
82    /// Environment module configuration.
83    pub env: ModuleConfig,
84
85    /// Format module configuration.
86    pub format: ModuleConfig,
87
88    /// Network module configuration.
89    pub net: ModuleConfig,
90
91    /// Time module configuration.
92    pub time: ModuleConfig,
93
94    /// Metrics module configuration.
95    pub metrics: ModuleConfig,
96}
97
98impl Default for StdlibConfig {
99    fn default() -> Self {
100        Self {
101            safety: SafetyConfig::default(),
102            process: ModuleConfig::disabled(), // Disabled by default for security
103            fs: ModuleConfig::default(),
104            path: ModuleConfig::default(),
105            env: ModuleConfig::default(),
106            format: ModuleConfig::default(),
107            net: ModuleConfig::disabled(), // Disabled by default for security
108            time: ModuleConfig::default(),
109            metrics: ModuleConfig::default(),
110        }
111    }
112}
113
114impl StdlibConfig {
115    /// Create a new stdlib configuration.
116    pub fn new() -> Self {
117        Self::default()
118    }
119
120    /// Create a permissive configuration (for trusted code only).
121    pub fn permissive() -> Self {
122        Self {
123            safety: SafetyConfig::permissive(),
124            process: ModuleConfig::default(),
125            fs: ModuleConfig::default(),
126            path: ModuleConfig::default(),
127            env: ModuleConfig::default(),
128            format: ModuleConfig::default(),
129            net: ModuleConfig::default(),
130            time: ModuleConfig::default(),
131            metrics: ModuleConfig::default(),
132        }
133    }
134
135    /// Create a strict configuration (minimal permissions).
136    pub fn strict() -> Self {
137        Self {
138            safety: SafetyConfig::strict(),
139            process: ModuleConfig::disabled(),
140            fs: ModuleConfig::disabled(),
141            path: ModuleConfig::default(),
142            env: ModuleConfig::disabled(),
143            format: ModuleConfig::default(),
144            net: ModuleConfig::disabled(),
145            time: ModuleConfig::default(),
146            metrics: ModuleConfig::disabled(),
147        }
148    }
149
150    /// Set safety configuration.
151    pub fn with_safety(mut self, safety: SafetyConfig) -> Self {
152        self.safety = safety;
153        self
154    }
155
156    /// Configure the process module.
157    pub fn with_process(mut self, config: ModuleConfig) -> Self {
158        self.process = config;
159        self
160    }
161
162    /// Configure the filesystem module.
163    pub fn with_fs(mut self, config: ModuleConfig) -> Self {
164        self.fs = config;
165        self
166    }
167
168    /// Configure the path module.
169    pub fn with_path(mut self, config: ModuleConfig) -> Self {
170        self.path = config;
171        self
172    }
173
174    /// Configure the environment module.
175    pub fn with_env(mut self, config: ModuleConfig) -> Self {
176        self.env = config;
177        self
178    }
179
180    /// Configure the format module.
181    pub fn with_format(mut self, config: ModuleConfig) -> Self {
182        self.format = config;
183        self
184    }
185
186    /// Configure the network module.
187    pub fn with_net(mut self, config: ModuleConfig) -> Self {
188        self.net = config;
189        self
190    }
191
192    /// Configure the time module.
193    pub fn with_time(mut self, config: ModuleConfig) -> Self {
194        self.time = config;
195        self
196    }
197
198    /// Configure the metrics module.
199    pub fn with_metrics(mut self, config: ModuleConfig) -> Self {
200        self.metrics = config;
201        self
202    }
203
204    /// Enable all modules.
205    pub fn enable_all(mut self) -> Self {
206        self.process.enabled = true;
207        self.fs.enabled = true;
208        self.path.enabled = true;
209        self.env.enabled = true;
210        self.format.enabled = true;
211        self.net.enabled = true;
212        self.time.enabled = true;
213        self.metrics.enabled = true;
214        self
215    }
216
217    /// Disable all modules.
218    pub fn disable_all(mut self) -> Self {
219        self.process.enabled = false;
220        self.fs.enabled = false;
221        self.path.enabled = false;
222        self.env.enabled = false;
223        self.format.enabled = false;
224        self.net.enabled = false;
225        self.time.enabled = false;
226        self.metrics.enabled = false;
227        self
228    }
229}
230
231#[cfg(test)]
232mod tests {
233    use super::*;
234
235    #[test]
236    fn test_module_config() {
237        let config = ModuleConfig::new()
238            .with_timeout(Duration::from_secs(10))
239            .with_option("key", "value");
240
241        assert!(config.enabled);
242        assert_eq!(config.timeout, Some(Duration::from_secs(10)));
243        assert_eq!(config.options.get("key"), Some(&"value".to_string()));
244    }
245
246    #[test]
247    fn test_stdlib_config_defaults() {
248        let config = StdlibConfig::default();
249
250        // Process and net disabled by default
251        assert!(!config.process.enabled);
252        assert!(!config.net.enabled);
253
254        // Others enabled by default
255        assert!(config.fs.enabled);
256        assert!(config.time.enabled);
257    }
258
259    #[test]
260    fn test_stdlib_config_permissive() {
261        let config = StdlibConfig::permissive();
262
263        assert!(config.process.enabled);
264        assert!(config.net.enabled);
265        assert!(config.fs.enabled);
266    }
267
268    #[test]
269    fn test_stdlib_config_strict() {
270        let config = StdlibConfig::strict();
271
272        assert!(!config.process.enabled);
273        assert!(!config.net.enabled);
274        assert!(!config.fs.enabled);
275        assert!(!config.env.enabled);
276    }
277}