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// 重新导出isocountry的CountryCode
15pub use isocountry::CountryCode;
16
17/// 主要的解析函数
18/// 
19/// # 示例
20/// 
21/// ```rust
22/// use location_rs::parse_country_code;
23/// 
24/// let result = parse_country_code("@HK Vip1");
25/// assert!(result.is_ok());
26/// ```
27pub fn parse_country_code(text: &str) -> Result<CountryCode, ParseError> {
28    parser::parse_country_code(text)
29}
30
31/// 解析器配置
32#[derive(Debug, Clone)]
33pub struct ParserConfig {
34    /// 是否区分大小写
35    pub case_sensitive: bool,
36    /// 是否启用模糊匹配
37    pub fuzzy_match: bool,
38    /// 解析超时时间(毫秒)
39    pub timeout_ms: u64,
40}
41
42impl Default for ParserConfig {
43    fn default() -> Self {
44        Self {
45            case_sensitive: false,
46            fuzzy_match: true,
47            timeout_ms: 100,
48        }
49    }
50}
51
52/// 解析器实例
53pub struct Parser {
54    config: ParserConfig,
55}
56
57impl Parser {
58    /// 使用默认配置创建解析器
59    pub fn new() -> Self {
60        Self {
61            config: ParserConfig::default(),
62        }
63    }
64    
65    /// 使用自定义配置创建解析器
66    pub fn with_config(config: ParserConfig) -> Self {
67        Self { config }
68    }
69    
70    /// 解析文本中的国家代码
71    pub fn parse(&self, text: &str) -> Result<CountryCode, ParseError> {
72        parser::parse_country_code_with_config(text, &self.config)
73    }
74}
75
76impl Default for Parser {
77    fn default() -> Self {
78        Self::new()
79    }
80}