use async_trait::async_trait;
use std::fmt::Display;
#[derive(Default)]
pub enum NotificationLevel {
#[default]
Info,
Warn,
Error,
}
impl Display for NotificationLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let msg = match self {
NotificationLevel::Error => "error",
NotificationLevel::Warn => "warn",
_ => "info",
};
write!(f, "{msg}")
}
}
#[derive(Default)]
pub struct NotificationData {
pub category: String,
pub level: NotificationLevel,
pub title: String,
pub message: String,
}
#[async_trait]
pub trait Notification {
async fn notify(&self, data: NotificationData);
}
pub type NotificationSender = Box<dyn Notification + Send + Sync>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_notification_level() {
let level = NotificationLevel::Error;
assert_eq!(level.to_string(), "error");
let level = NotificationLevel::Warn;
assert_eq!(level.to_string(), "warn");
let level = NotificationLevel::Info;
assert_eq!(level.to_string(), "info");
}
}