postfix_log_parser/
error.rs1use crate::events::PostfixLogEvent;
2use thiserror::Error;
3
4#[derive(Error, Debug, Clone)]
6pub enum ParseError {
7 #[error("无法识别的组件: {component}")]
8 UnknownComponent { component: String },
9
10 #[error("无效的时间戳格式: {timestamp}")]
11 InvalidTimestamp { timestamp: String },
12
13 #[error("无效的日志格式: {reason}")]
14 InvalidLogFormat { reason: String },
15
16 #[error("组件解析失败: {component} - {reason}")]
17 ComponentParseError { component: String, reason: String },
18
19 #[error("正则表达式错误: {0}")]
20 RegexError(#[from] regex::Error),
21
22 #[error("日期时间解析错误: {0}")]
23 ChronoParseError(#[from] chrono::ParseError),
24}
25
26#[derive(Debug, Clone)]
30pub struct ParseResult {
31 pub event: Option<PostfixLogEvent>,
33
34 pub confidence: f32,
40
41 pub parsing_errors: Vec<String>,
43}
44
45impl ParseResult {
46 pub fn success(event: PostfixLogEvent, confidence: f32) -> Self {
48 Self {
49 event: Some(event),
50 confidence,
51 parsing_errors: Vec::new(),
52 }
53 }
54
55 pub fn failure(error: ParseError) -> Self {
57 Self {
58 event: None,
59 confidence: 0.0,
60 parsing_errors: vec![error.to_string()],
61 }
62 }
63
64 pub fn partial(event: PostfixLogEvent, confidence: f32, warnings: Vec<String>) -> Self {
66 Self {
67 event: Some(event),
68 confidence,
69 parsing_errors: warnings,
70 }
71 }
72
73 pub fn is_success(&self) -> bool {
75 self.event.is_some() && self.confidence > 0.5
76 }
77
78 pub fn is_failure(&self) -> bool {
80 self.event.is_none()
81 }
82
83 pub fn main_error(&self) -> Option<&String> {
85 self.parsing_errors.first()
86 }
87}