use serde::{Deserialize, Serialize};
use anyhow::Result;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub server: ServerConfig,
pub database: DatabaseConfig,
pub executor: ExecutorConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
pub port: u16,
pub host: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
pub path: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutorConfig {
pub max_parallel_tasks: u32,
pub default_timeout_secs: u64,
}
impl Default for Config {
fn default() -> Self {
Config {
server: ServerConfig {
port: 8080,
host: "0.0.0.0".to_string(),
},
database: DatabaseConfig {
path: "./ironflow.db".to_string(),
},
executor: ExecutorConfig {
max_parallel_tasks: 10,
default_timeout_secs: 3600,
},
}
}
}
impl Config {
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
let content = std::fs::read_to_string(path)?;
let config = toml::from_str(&content)?;
Ok(config)
}
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let content = toml::to_string_pretty(self)?;
std::fs::write(path, content)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = Config::default();
assert_eq!(config.server.port, 8080);
assert_eq!(config.executor.max_parallel_tasks, 10);
}
#[test]
fn test_config_serialization() {
let config = Config::default();
let toml_str = toml::to_string_pretty(&config).unwrap();
assert!(toml_str.contains("8080"));
}
}