postfix_log_parser/events/
postmap.rs

1//! Postmap映射表工具模块
2//!
3//! 处理Postfix postmap工具的事件,包括映射表创建、文件错误和命令执行问题
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9/// Postmap工具事件
10///
11/// 记录postmap命令执行过程中的各种事件和错误
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct PostmapEvent {
14    /// 事件发生时间戳(UTC时间)
15    pub timestamp: DateTime<Utc>,
16
17    /// 执行postmap命令的进程ID
18    pub process_id: u32,
19
20    /// 事件类型(文件错误或命令错误)
21    pub event_type: PostmapEventType,
22
23    /// 错误详细信息
24    pub details: PostmapDetails,
25
26    /// 扩展属性(可选的附加信息)
27    /// 用于存储事件相关的额外元数据
28    pub extensions: HashMap<String, String>,
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub enum PostmapEventType {
33    FileError,
34    CommandError,
35}
36
37/// Postmap错误详细信息
38///
39/// 包含postmap操作失败时的具体错误信息和上下文
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
41pub struct PostmapDetails {
42    /// 错误类型分类
43    pub error_type: ErrorType,
44
45    /// 相关文件路径(如果适用)
46    /// 当错误与特定文件相关时提供文件路径
47    pub file_path: Option<String>,
48
49    /// 错误消息描述
50    /// 来自postmap命令的具体错误信息
51    pub error_message: String,
52
53    /// 命令参数(如果适用)
54    /// 导致错误的命令行参数
55    pub command_args: Option<String>,
56}
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59pub enum ErrorType {
60    FileNotFound,
61    DirectoryNotFound,
62    InvalidCommandArgs,
63}
64
65impl PostmapEvent {
66    pub fn new(
67        timestamp: DateTime<Utc>,
68        process_id: u32,
69        event_type: PostmapEventType,
70        details: PostmapDetails,
71    ) -> Self {
72        Self {
73            timestamp,
74            process_id,
75            event_type,
76            details,
77            extensions: HashMap::new(),
78        }
79    }
80
81    pub fn add_extension(&mut self, key: String, value: String) {
82        self.extensions.insert(key, value);
83    }
84}
85
86impl PostmapDetails {
87    pub fn new_file_error(error_type: ErrorType, file_path: String, error_message: String) -> Self {
88        Self {
89            error_type,
90            file_path: Some(file_path),
91            error_message,
92            command_args: None,
93        }
94    }
95
96    pub fn new_command_error(error_message: String, command_args: Option<String>) -> Self {
97        Self {
98            error_type: ErrorType::InvalidCommandArgs,
99            file_path: None,
100            error_message,
101            command_args,
102        }
103    }
104}