markdown_translator/
config.rs1use crate::types::TranslationConfig;
6use serde::{Deserialize, Serialize};
7use std::fs;
8use std::path::Path;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct TranslationLibConfig {
30 #[serde(default)]
32 pub translation: TranslationConfig,
33}
34
35impl Default for TranslationLibConfig {
36 fn default() -> Self {
37 Self {
38 translation: TranslationConfig::default(),
39 }
40 }
41}
42
43impl TranslationLibConfig {
44 pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
46 let content = fs::read_to_string(path)?;
47 let config: TranslationLibConfig = toml::from_str(&content)?;
48 Ok(config)
49 }
50
51 pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn std::error::Error>> {
53 let content = toml::to_string_pretty(self)?;
54 fs::write(path, content)?;
55 Ok(())
56 }
57
58 pub fn load_from_default_locations() -> Self {
60 let possible_paths = [
61 "translation-config.toml",
62 "config.toml",
63 ".translation-config.toml",
64 ];
65
66 for path in &possible_paths {
67 if Path::new(path).exists() {
68 match Self::from_file(path) {
69 Ok(config) => {
70 println!("Loaded configuration from: {}", path);
71 return config;
72 }
73 Err(e) => {
74 eprintln!("Warning: Failed to load config from {}: {}", path, e);
75 }
76 }
77 }
78 }
79
80 println!("No configuration file found, using defaults");
81 Self::default()
82 }
83
84 pub fn generate_example_config<P: AsRef<Path>>(path: P) -> Result<(), Box<dyn std::error::Error>> {
86 let example_config = Self::default();
87 example_config.save_to_file(path)?;
88 Ok(())
89 }
90}