use serde::Deserialize; use std::fs; use std::path::Path; #[derive(Debug, Deserialize)] pub struct DefaultConfig { pub headers: Option>, pub indent: Option, } #[derive(Debug, Deserialize)] pub struct Config { pub default: Option, } impl Config { pub fn from_file>(path: P) -> Result> { let content = fs::read_to_string(path)?; let config: Config = toml::from_str(&content)?; Ok(config) } pub fn headers(&self) -> Vec { self.default .as_ref() .and_then(|d| d.headers.clone()) .unwrap_or_default() } pub fn indent(&self) -> usize { self.default.as_ref().and_then(|d| d.indent).unwrap_or(2) } } #[cfg(test)] mod tests { use super::*; use std::env; use std::fs::write; #[test] fn test_from_file_with_realistic_headers() { let toml_content = r#" [default] headers = [ "Authorization: Bearer YOUR_TOKEN", "Content-Type: application/json" ] indent = 4 "#; let tmp_path = std::env::temp_dir().join("test_real_config.toml"); std::fs::write(&tmp_path, toml_content).expect("Failed to write TOML file"); let config = Config::from_file(&tmp_path).expect("Failed to read config from file"); assert_eq!( config.headers(), vec![ "Authorization: Bearer YOUR_TOKEN", "Content-Type: application/json" ] ); assert_eq!(config.indent(), 4); } #[test] fn test_missing_values_defaults() { let toml_content = r#""#; let config: Config = toml::from_str(toml_content).expect("Failed to parse empty TOML"); assert_eq!(config.headers(), Vec::::new()); assert_eq!(config.indent(), 2); } #[test] fn test_from_file_function() { let toml_content = r#" [default] headers = ["User", "Email"] indent = 3 "#; let tmp_dir = env::temp_dir(); let file_path = tmp_dir.join("test_config.toml"); write(&file_path, toml_content).expect("Failed to write temp TOML"); let config = Config::from_file(&file_path).expect("Failed to read config"); assert_eq!(config.headers(), vec!["User", "Email"]); assert_eq!(config.indent(), 3); } }