location_rs/
error.rs

1use thiserror::Error;
2
3/// 解析错误类型
4#[derive(Error, Debug, Clone)]
5pub enum ParseError {
6    /// 未找到匹配的国家代码
7    #[error("未找到匹配的国家代码: {text}")]
8    NotFound {
9        text: String,
10    },
11    
12    /// 匹配到多个可能的国家代码
13    #[error("匹配到多个可能的国家代码: {text}, 候选: {candidates:?}")]
14    Ambiguous {
15        text: String,
16        candidates: Vec<String>,
17    },
18    
19    /// 输入文本格式无效
20    #[error("输入文本格式无效: {text}")]
21    InvalidInput {
22        text: String,
23    },
24    
25    /// 配置错误
26    #[error("配置错误: {message}")]
27    ConfigError {
28        message: String,
29    },
30}
31
32impl ParseError {
33    /// 创建未找到错误
34    pub fn not_found(text: &str) -> Self {
35        ParseError::NotFound {
36            text: text.to_string(),
37        }
38    }
39    
40    /// 创建模糊匹配错误
41    pub fn ambiguous(text: &str, candidates: Vec<String>) -> Self {
42        ParseError::Ambiguous {
43            text: text.to_string(),
44            candidates,
45        }
46    }
47    
48    /// 创建无效输入错误
49    pub fn invalid_input(text: &str) -> Self {
50        ParseError::InvalidInput {
51            text: text.to_string(),
52        }
53    }
54    
55    /// 创建配置错误
56    pub fn config_error(message: &str) -> Self {
57        ParseError::ConfigError {
58            message: message.to_string(),
59        }
60    }
61}