use crate::events::PostfixLogEvent;
use thiserror::Error;
#[derive(Error, Debug, Clone)]
pub enum ParseError {
#[error("无法识别的组件: {component}")]
UnknownComponent { component: String },
#[error("无效的时间戳格式: {timestamp}")]
InvalidTimestamp { timestamp: String },
#[error("无效的日志格式: {reason}")]
InvalidLogFormat { reason: String },
#[error("组件解析失败: {component} - {reason}")]
ComponentParseError { component: String, reason: String },
#[error("正则表达式错误: {0}")]
RegexError(#[from] regex::Error),
#[error("日期时间解析错误: {0}")]
ChronoParseError(#[from] chrono::ParseError),
}
#[derive(Debug, Clone)]
pub struct ParseResult {
pub event: Option<PostfixLogEvent>,
pub confidence: f32,
pub parsing_errors: Vec<String>,
}
impl ParseResult {
pub fn success(event: PostfixLogEvent, confidence: f32) -> Self {
Self {
event: Some(event),
confidence,
parsing_errors: Vec::new(),
}
}
pub fn failure(error: ParseError) -> Self {
Self {
event: None,
confidence: 0.0,
parsing_errors: vec![error.to_string()],
}
}
pub fn partial(event: PostfixLogEvent, confidence: f32, warnings: Vec<String>) -> Self {
Self {
event: Some(event),
confidence,
parsing_errors: warnings,
}
}
pub fn is_success(&self) -> bool {
self.event.is_some() && self.confidence > 0.5
}
pub fn is_failure(&self) -> bool {
self.event.is_none()
}
pub fn main_error(&self) -> Option<&String> {
self.parsing_errors.first()
}
}