postfix_log_parser/events/
local.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// 本地投递组件事件
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub enum LocalEvent {
8    ConfigurationWarning(ConfigurationWarning),
9    LocalDelivery(LocalDelivery),
10    ExternalDelivery(ExternalDelivery),
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct ConfigurationWarning {
15    pub timestamp: DateTime<Utc>,
16    pub warning_type: WarningType,
17    pub message: String,
18    pub details: HashMap<String, String>,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
22pub enum WarningType {
23    NisDomainNotSet,
24    RequiredAliasNotFound,
25    FilePermission,
26    Configuration,
27    Other,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct LocalDelivery {
32    pub timestamp: DateTime<Utc>,
33    pub queue_id: String,
34    pub recipient: String,
35    pub original_recipient: Option<String>,
36    pub relay: String,
37    pub delay: f64,
38    pub delays: Vec<f64>,
39    pub dsn: String,
40    pub status: DeliveryStatus,
41    pub delivery_method: DeliveryMethod,
42    pub size: Option<u64>,
43    pub nrcpt: Option<u32>,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
47pub enum DeliveryStatus {
48    Sent,
49    Bounced,
50    Deferred,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
54pub enum DeliveryMethod {
55    Mailbox,
56    Discarded,
57    Forwarded,
58    Piped,
59    File,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct ExternalDelivery {
64    pub timestamp: DateTime<Utc>,
65    pub queue_id: String,
66    pub recipient: String,
67    pub original_recipient: Option<String>,
68    pub command: Option<String>,
69    pub file_path: Option<String>,
70    pub status: DeliveryStatus,
71    pub delay: f64,
72    pub details: HashMap<String, String>,
73}
74
75impl LocalEvent {
76    pub fn timestamp(&self) -> DateTime<Utc> {
77        match self {
78            LocalEvent::ConfigurationWarning(event) => event.timestamp,
79            LocalEvent::LocalDelivery(event) => event.timestamp,
80            LocalEvent::ExternalDelivery(event) => event.timestamp,
81        }
82    }
83
84    pub fn event_type(&self) -> &'static str {
85        match self {
86            LocalEvent::ConfigurationWarning(_) => "configuration_warning",
87            LocalEvent::LocalDelivery(_) => "local_delivery",
88            LocalEvent::ExternalDelivery(_) => "external_delivery",
89        }
90    }
91}
92
93impl WarningType {
94    pub fn as_str(&self) -> &'static str {
95        match self {
96            WarningType::NisDomainNotSet => "nis_domain_not_set",
97            WarningType::RequiredAliasNotFound => "required_alias_not_found",
98            WarningType::FilePermission => "file_permission",
99            WarningType::Configuration => "configuration",
100            WarningType::Other => "other",
101        }
102    }
103}
104
105impl DeliveryStatus {
106    pub fn as_str(&self) -> &'static str {
107        match self {
108            DeliveryStatus::Sent => "sent",
109            DeliveryStatus::Bounced => "bounced",
110            DeliveryStatus::Deferred => "deferred",
111        }
112    }
113}
114
115impl DeliveryMethod {
116    pub fn as_str(&self) -> &'static str {
117        match self {
118            DeliveryMethod::Mailbox => "mailbox",
119            DeliveryMethod::Discarded => "discarded",
120            DeliveryMethod::Forwarded => "forwarded",
121            DeliveryMethod::Piped => "piped",
122            DeliveryMethod::File => "file",
123        }
124    }
125}