use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use serde_json;
pub fn read_config() -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
let exe_path = std::env::current_exe()?;
let exe_dir = exe_path.parent().ok_or("Failed to get executable directory")?;
let config_path = exe_dir.join("config.json");
if !config_path.exists() {
return Err(format!("config.json not found in {:?}", exe_dir).into());
}
let mut file = File::open(&config_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config: HashMap<String, String> = serde_json::from_str(&contents)?;
Ok(config)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_config() {
match read_config() {
Ok(config) => {
assert!(config.contains_key("apiURL"));
assert!(config.contains_key("apiKey"));
}
Err(e) => panic!("Test failed: {}", e),
}
}
}