1use serde::Deserialize;
2use std::collections::HashMap;
3use crate::error::ParseError;
4
5#[derive(Debug, Deserialize, Clone)]
7pub struct CountryInfo {
8 pub alpha2: String,
10 pub alpha3: String,
12 pub numeric: String,
14 pub name_en: String,
16 pub name_zh_cn: String,
18 pub name_zh_tw: String,
20 pub region: String,
22}
23
24#[derive(Debug, Deserialize, Clone)]
26pub struct ParserSettings {
27 pub case_sensitive: bool,
29 pub fuzzy_match: bool,
31 pub timeout_ms: u64,
33}
34
35#[derive(Debug, Deserialize, Clone)]
37pub struct PatternConfig {
38 pub prefix_patterns: Vec<String>,
40 pub suffix_patterns: Vec<String>,
42}
43
44#[derive(Debug, Deserialize, Clone)]
46pub struct Configuration {
47 pub version: String,
49 pub countries: Vec<CountryInfo>,
51 pub patterns: PatternConfig,
53 pub settings: ParserSettings,
55}
56
57impl Configuration {
58 pub fn load() -> Result<Self, ParseError> {
60 let config_str = include_str!("../resources/countries.json");
61
62 serde_json::from_str(config_str)
63 .map_err(|e| ParseError::config_error(&format!("配置解析失败: {}", e)))
64 }
65
66 pub fn create_country_mapping(&self) -> HashMap<String, &CountryInfo> {
68 let mut mapping = HashMap::new();
69
70 for country in &self.countries {
71 mapping.insert(country.alpha2.clone(), country);
73
74 mapping.insert(country.alpha3.clone(), country);
76
77 mapping.insert(country.name_en.clone(), country);
79
80 mapping.insert(country.name_zh_cn.clone(), country);
82
83 mapping.insert(country.name_zh_tw.clone(), country);
85 }
86
87 mapping
88 }
89
90 pub fn get_settings(&self) -> &ParserSettings {
92 &self.settings
93 }
94
95 pub fn get_patterns(&self) -> &PatternConfig {
97 &self.patterns
98 }
99
100 pub fn get_countries(&self) -> &[CountryInfo] {
102 &self.countries
103 }
104}