location_rs/
config.rs

1use serde::Deserialize;
2use std::collections::HashMap;
3use crate::error::ParseError;
4
5/// 国家信息配置
6#[derive(Debug, Deserialize, Clone)]
7pub struct CountryInfo {
8    /// ISO 3166-1 alpha-2代码
9    pub alpha2: String,
10    /// ISO 3166-1 alpha-3代码
11    pub alpha3: String,
12    /// 英文名称
13    pub name_en: String,
14    /// 简体中文名称
15    pub name_zh_cn: String,
16    /// 繁体中文名称
17    pub name_zh_tw: String,
18    /// 国家简称和别称
19    pub abbreviations: Vec<String>,
20}
21
22/// 解析器设置
23#[derive(Debug, Deserialize, Clone)]
24pub struct ParserSettings {
25    /// 是否区分大小写
26    pub case_sensitive: bool,
27    /// 是否启用模糊匹配
28    pub fuzzy_match: bool,
29    /// 超时时间(毫秒)
30    pub timeout_ms: u64,
31}
32
33/// 模式配置
34#[derive(Debug, Deserialize, Clone)]
35pub struct PatternConfig {
36    /// 前缀模式
37    pub prefix_patterns: Vec<String>,
38    /// 后缀模式
39    pub suffix_patterns: Vec<String>,
40}
41
42/// 国家配置
43#[derive(Debug, Deserialize, Clone)]
44pub struct CountriesConfig {
45    /// 配置版本
46    pub version: String,
47    /// 国家信息列表
48    pub countries: Vec<CountryInfo>,
49}
50
51/// 完整配置
52#[derive(Debug, Clone)]
53pub struct Configuration {
54    /// 国家配置
55    pub countries_config: CountriesConfig,
56    /// 模式配置
57    pub patterns: PatternConfig,
58    /// 解析器设置
59    pub settings: ParserSettings,
60}
61
62impl Configuration {
63    /// 从多个嵌入的JSON文件加载配置
64    pub fn load() -> Result<Self, ParseError> {
65        // 加载国家配置
66        let countries_str = include_str!("../resources/countries.json");
67        let countries_config: CountriesConfig = serde_json::from_str(countries_str)
68            .map_err(|e| ParseError::config_error(&format!("国家配置解析失败: {}", e)))?;
69        
70        // 加载模式配置
71        let patterns_str = include_str!("../resources/patterns.json");
72        let patterns: PatternConfig = serde_json::from_str(patterns_str)
73            .map_err(|e| ParseError::config_error(&format!("模式配置解析失败: {}", e)))?;
74        
75        // 加载解析器设置
76        let settings_str = include_str!("../resources/settings.json");
77        let settings: ParserSettings = serde_json::from_str(settings_str)
78            .map_err(|e| ParseError::config_error(&format!("设置配置解析失败: {}", e)))?;
79        
80        Ok(Configuration {
81            countries_config,
82            patterns,
83            settings,
84        })
85    }
86    
87    /// 创建国家代码到国家信息的映射
88    pub fn create_country_mapping(&self) -> HashMap<String, &CountryInfo> {
89        let mut mapping = HashMap::new();
90        
91        for country in &self.countries_config.countries {
92            // 添加alpha-2代码
93            mapping.insert(country.alpha2.clone(), country);
94            
95            // 添加alpha-3代码
96            mapping.insert(country.alpha3.clone(), country);
97            
98            // 添加英文名称
99            mapping.insert(country.name_en.clone(), country);
100            
101            // 添加简体中文名称
102            mapping.insert(country.name_zh_cn.clone(), country);
103            
104            // 添加繁体中文名称
105            mapping.insert(country.name_zh_tw.clone(), country);
106            
107            // 添加所有简称和别称
108            for abbr in &country.abbreviations {
109                mapping.insert(abbr.clone(), country);
110            }
111        }
112        
113        mapping
114    }
115    
116    /// 获取解析器设置
117    pub fn get_settings(&self) -> &ParserSettings {
118        &self.settings
119    }
120    
121    /// 获取模式配置
122    pub fn get_patterns(&self) -> &PatternConfig {
123        &self.patterns
124    }
125    
126    /// 获取所有国家信息
127    pub fn get_countries(&self) -> &[CountryInfo] {
128        &self.countries_config.countries
129    }
130    
131    /// 获取配置版本
132    pub fn get_version(&self) -> &str {
133        &self.countries_config.version
134    }
135}