postfix-log-parser 0.2.0

高性能模块化Postfix日志解析器,经3.2GB生产数据验证,SMTPD事件100%准确率
Documentation
//! Postmap映射表工具模块
//!
//! 处理Postfix postmap工具的事件,包括映射表创建、文件错误和命令执行问题

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Postmap工具事件
///
/// 记录postmap命令执行过程中的各种事件和错误
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PostmapEvent {
    /// 事件发生时间戳(UTC时间)
    pub timestamp: DateTime<Utc>,

    /// 执行postmap命令的进程ID
    pub process_id: u32,

    /// 事件类型(文件错误或命令错误)
    pub event_type: PostmapEventType,

    /// 错误详细信息
    pub details: PostmapDetails,

    /// 扩展属性(可选的附加信息)
    /// 用于存储事件相关的额外元数据
    pub extensions: HashMap<String, String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PostmapEventType {
    FileError,
    CommandError,
}

/// Postmap错误详细信息
///
/// 包含postmap操作失败时的具体错误信息和上下文
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PostmapDetails {
    /// 错误类型分类
    pub error_type: ErrorType,

    /// 相关文件路径(如果适用)
    /// 当错误与特定文件相关时提供文件路径
    pub file_path: Option<String>,

    /// 错误消息描述
    /// 来自postmap命令的具体错误信息
    pub error_message: String,

    /// 命令参数(如果适用)
    /// 导致错误的命令行参数
    pub command_args: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ErrorType {
    FileNotFound,
    DirectoryNotFound,
    InvalidCommandArgs,
}

impl PostmapEvent {
    pub fn new(
        timestamp: DateTime<Utc>,
        process_id: u32,
        event_type: PostmapEventType,
        details: PostmapDetails,
    ) -> Self {
        Self {
            timestamp,
            process_id,
            event_type,
            details,
            extensions: HashMap::new(),
        }
    }

    pub fn add_extension(&mut self, key: String, value: String) {
        self.extensions.insert(key, value);
    }
}

impl PostmapDetails {
    pub fn new_file_error(error_type: ErrorType, file_path: String, error_message: String) -> Self {
        Self {
            error_type,
            file_path: Some(file_path),
            error_message,
            command_args: None,
        }
    }

    pub fn new_command_error(error_message: String, command_args: Option<String>) -> Self {
        Self {
            error_type: ErrorType::InvalidCommandArgs,
            file_path: None,
            error_message,
            command_args,
        }
    }
}