|
| 1 | +use super::{error::ConfigError, schema::ConfigFile}; |
| 2 | +use crate::i18n::t; |
| 3 | +use std::fmt; |
| 4 | + |
| 5 | +/// Language options for the application |
| 6 | +#[derive(Debug, Clone)] |
| 7 | +#[allow(dead_code)] |
| 8 | +pub enum Language { |
| 9 | + English, |
| 10 | + Chinese, |
| 11 | +} |
| 12 | + |
| 13 | +// Implement the Display trait |
| 14 | +// e.g. `println!("{}", Language::English);` will print `en` |
| 15 | +impl fmt::Display for Language { |
| 16 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 17 | + match self { |
| 18 | + Language::English => write!(f, "en"), |
| 19 | + Language::Chinese => write!(f, "zh"), |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// Parsing from string to enumeration implementation |
| 25 | +impl Language { |
| 26 | + pub fn from_str(s: &str) -> Option<Self> { |
| 27 | + match s.to_lowercase().as_str() { |
| 28 | + "en" => Some(Language::English), |
| 29 | + "zh" => Some(Language::Chinese), |
| 30 | + _ => None, |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Global configuration structure |
| 36 | +#[derive(Debug, Clone)] |
| 37 | +pub struct Config { |
| 38 | + pub language: Language, |
| 39 | + pub authors: Vec<String>, |
| 40 | + pub date_range: Vec<String>, |
| 41 | + pub repos: Vec<String>, |
| 42 | + pub format: std::collections::HashMap<String, String>, |
| 43 | + pub includes: Vec<String>, |
| 44 | + pub excludes: Vec<String>, |
| 45 | +} |
| 46 | + |
| 47 | +impl Config { |
| 48 | + /// Create a new configuration instance with default values |
| 49 | + pub fn new_from_file(file_config: &ConfigFile) -> Result<Self, ConfigError> { |
| 50 | + let language = Language::from_str(&file_config.lang) |
| 51 | + .ok_or_else(|| ConfigError::InvalidConfig("Invalid language setting".to_string()))?; |
| 52 | + |
| 53 | + Ok(Self { |
| 54 | + language, |
| 55 | + authors: file_config.authors.clone(), |
| 56 | + date_range: file_config.date_range.clone(), |
| 57 | + repos: file_config.repos.clone(), |
| 58 | + format: file_config.format.clone(), |
| 59 | + includes: file_config.includes.clone(), |
| 60 | + excludes: file_config.excludes.clone(), |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl fmt::Display for Config { |
| 66 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 67 | + writeln!(f, "")?; |
| 68 | + writeln!(f, "{}", t("config_details"))?; |
| 69 | + writeln!(f, "")?; |
| 70 | + writeln!( |
| 71 | + f, |
| 72 | + "{}: {}", |
| 73 | + t("language"), |
| 74 | + if matches!(self.language, Language::Chinese) { |
| 75 | + "Chinese" |
| 76 | + } else { |
| 77 | + "English" |
| 78 | + } |
| 79 | + )?; |
| 80 | + writeln!(f, "{}: {:?}", t("authors"), self.authors)?; |
| 81 | + writeln!( |
| 82 | + f, |
| 83 | + "{}: {} - {}", |
| 84 | + t("date_range"), |
| 85 | + self.date_range.get(0).unwrap_or(&"N/A".to_string()), |
| 86 | + self.date_range.get(1).unwrap_or(&"N/A".to_string()) |
| 87 | + )?; |
| 88 | + writeln!(f, "{}:", t("repos"))?; |
| 89 | + for repo in &self.repos { |
| 90 | + if let Some(display_name) = self.format.get(repo) { |
| 91 | + writeln!(f, " - {} ({})", display_name, repo)?; |
| 92 | + } else { |
| 93 | + writeln!(f, " - {}", repo)?; |
| 94 | + } |
| 95 | + } |
| 96 | + writeln!(f, "{}:", t("includes"))?; |
| 97 | + for inc in &self.includes { |
| 98 | + writeln!(f, " - {}", inc)?; |
| 99 | + } |
| 100 | + writeln!(f, "{}:", t("excludes"))?; |
| 101 | + for exc in &self.excludes { |
| 102 | + writeln!(f, " - {}", exc)?; |
| 103 | + } |
| 104 | + writeln!(f, "")?; |
| 105 | + writeln!(f, "--------------------------------")?; |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | +} |
0 commit comments