use crate::types::TranslationConfig;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranslationLibConfig {
#[serde(default)]
pub translation: TranslationConfig,
}
impl Default for TranslationLibConfig {
fn default() -> Self {
Self {
translation: TranslationConfig::default(),
}
}
}
impl TranslationLibConfig {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
let content = fs::read_to_string(path)?;
let config: TranslationLibConfig = toml::from_str(&content)?;
Ok(config)
}
pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn std::error::Error>> {
let content = toml::to_string_pretty(self)?;
fs::write(path, content)?;
Ok(())
}
pub fn load_from_default_locations() -> Self {
let possible_paths = [
"translation-config.toml",
"config.toml",
".translation-config.toml",
];
for path in &possible_paths {
if Path::new(path).exists() {
match Self::from_file(path) {
Ok(config) => {
println!("Loaded configuration from: {}", path);
return config;
}
Err(e) => {
eprintln!("Warning: Failed to load config from {}: {}", path, e);
}
}
}
}
println!("No configuration file found, using defaults");
Self::default()
}
pub fn generate_example_config<P: AsRef<Path>>(path: P) -> Result<(), Box<dyn std::error::Error>> {
let example_config = Self::default();
example_config.save_to_file(path)?;
Ok(())
}
}