use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PostmapEvent {
pub timestamp: DateTime<Utc>,
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,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PostmapDetails {
pub error_type: ErrorType,
pub file_path: Option<String>,
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,
}
}
}