location_rs/
lib.rs

1//! 国家代码解析器库
2//! 
3//! 这个库提供从标题文本中解析国家或地区代码的功能。
4//! 支持ISO 3166-1 alpha-2/alpha-3代码以及简体中文和繁体中文的国家名称。
5
6pub mod error;
7pub mod config;
8pub mod parser;
9
10// 重新导出主要类型
11pub use error::ParseError;
12pub use config::{Configuration, CountryInfo, ParserSettings};
13
14
15
16/// 主要的解析函数
17/// 
18/// # 示例
19/// 
20/// ```rust
21/// use location_rs::parse_country_code;
22/// 
23/// let result = parse_country_code("@HK Vip1");
24/// assert!(result.is_ok());
25/// ```
26pub fn parse_country_code(text: &str) -> Result<CountryInfo, ParseError> {
27    parser::parse_country_code(text)
28}
29
30/// 解析器配置
31#[derive(Debug, Clone)]
32pub struct ParserConfig {
33    /// 是否区分大小写
34    pub case_sensitive: bool,
35    /// 是否启用模糊匹配
36    pub fuzzy_match: bool,
37}
38
39impl Default for ParserConfig {
40    fn default() -> Self {
41        Self {
42            case_sensitive: false,
43            fuzzy_match: true,
44        }
45    }
46}
47
48/// 解析器实例
49pub struct Parser {
50    config: ParserConfig,
51}
52
53impl Parser {
54    /// 使用默认配置创建解析器
55    pub fn new() -> Self {
56        Self {
57            config: ParserConfig::default(),
58        }
59    }
60    
61    /// 使用自定义配置创建解析器
62    pub fn with_config(config: ParserConfig) -> Self {
63        Self { config }
64    }
65    
66    /// 解析文本中的国家代码
67    pub fn parse(&self, text: &str) -> Result<CountryInfo, ParseError> {
68        parser::parse_country_code_with_config(text, &self.config)
69    }
70}
71
72impl Default for Parser {
73    fn default() -> Self {
74        Self::new()
75    }
76}