use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fs;
#[derive(Serialize, Deserialize)]
pub struct ConfigFile {
#[serde(flatten)]
pub other: std::collections::HashMap<String, serde_yaml::Value>,
}
pub fn read_config_file(config_file: &str) -> Result<ConfigFile, Box<dyn Error>> {
let config_content = fs::read_to_string(config_file)?;
let config: ConfigFile = serde_yaml::from_str(&config_content)?;
Ok(config)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_config_file() {
let yaml_content = r#"
key1: value1
key2: value2
"#;
let temp_file_path = "test_config.yaml";
fs::write(temp_file_path, yaml_content).expect("Unable to write test file");
let config = read_config_file(temp_file_path).expect("Failed to read config file");
assert_eq!(config.other.get("key1").unwrap(), "value1");
assert_eq!(config.other.get("key2").unwrap(), "value2");
fs::remove_file(temp_file_path).expect("Unable to delete test file");
}
}