use std::str::FromStr; use chrono::{DateTime, Local}; use eyre::{eyre, ContextCompat}; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Serialize, Deserialize)] pub enum Severity { Fatal, Error, Warning, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LogMsg { /// The time that the error was reported. pub time: Option>, pub severity: Severity, /// The service which reported the error. pub service: String, /// The log message. pub message: String, /// The host that the service is running on. pub hostname: String, pub file: Option, pub line: Option, } impl LogMsg { pub fn new(service: String, message: String, hostname: String) -> Self { Self { time: Some(Local::now()), severity: Severity::Error, service, message, hostname, file: None, line: None, } } } impl Severity { pub fn as_str(&self) -> &'static str { match self { Severity::Fatal => "FATAL", Severity::Error => "ERROR", Severity::Warning => "WARNING", } } } impl FromStr for Severity { type Err = eyre::Report; fn from_str(s: &str) -> Result { let err = || eyre!("{s:?} is not a valid severity level"); let first_char = s .trim() .chars() .next() .wrap_err_with(err)? .to_ascii_lowercase(); Ok(match first_char { 'f' => Severity::Fatal, 'e' => Severity::Error, 'w' => Severity::Warning, _ => return Err(err()), }) } }