Skip to main content

ironflow/
config.rs

1use serde::{Deserialize, Serialize};
2use anyhow::Result;
3use std::path::Path;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Config {
7    pub server: ServerConfig,
8    pub database: DatabaseConfig,
9    pub executor: ExecutorConfig,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct ServerConfig {
14    pub port: u16,
15    pub host: String,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct DatabaseConfig {
20    pub path: String,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct ExecutorConfig {
25    pub max_parallel_tasks: u32,
26    pub default_timeout_secs: u64,
27}
28
29impl Default for Config {
30    fn default() -> Self {
31        Config {
32            server: ServerConfig {
33                port: 8080,
34                host: "0.0.0.0".to_string(),
35            },
36            database: DatabaseConfig {
37                path: "./ironflow.db".to_string(),
38            },
39            executor: ExecutorConfig {
40                max_parallel_tasks: 10,
41                default_timeout_secs: 3600,
42            },
43        }
44    }
45}
46
47impl Config {
48    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
49        let content = std::fs::read_to_string(path)?;
50        let config = toml::from_str(&content)?;
51        Ok(config)
52    }
53
54    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
55        let content = toml::to_string_pretty(self)?;
56        std::fs::write(path, content)?;
57        Ok(())
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_default_config() {
67        let config = Config::default();
68        assert_eq!(config.server.port, 8080);
69        assert_eq!(config.executor.max_parallel_tasks, 10);
70    }
71
72    #[test]
73    fn test_config_serialization() {
74        let config = Config::default();
75        let toml_str = toml::to_string_pretty(&config).unwrap();
76        assert!(toml_str.contains("8080"));
77    }
78}