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