use std::time::Instant;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotificationKind {
Success,
Info,
#[allow(dead_code)]
Warning,
}
#[derive(Debug, Clone)]
pub struct Notification {
pub message: String,
pub kind: NotificationKind,
pub created_at: Instant,
}
impl Notification {
pub fn new(message: impl Into<String>, kind: NotificationKind) -> Self {
Self {
message: message.into(),
kind,
created_at: Instant::now(),
}
}
pub fn success(message: impl Into<String>) -> Self {
Self::new(message, NotificationKind::Success)
}
pub fn info(message: impl Into<String>) -> Self {
Self::new(message, NotificationKind::Info)
}
#[allow(dead_code)]
pub fn warning(message: impl Into<String>) -> Self {
Self::new(message, NotificationKind::Warning)
}
pub fn is_expired(&self) -> bool {
self.created_at.elapsed().as_secs() >= 5
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_notification_new() {
let n = Notification::new("Test message", NotificationKind::Success);
assert_eq!(n.message, "Test message");
assert_eq!(n.kind, NotificationKind::Success);
}
#[test]
fn test_notification_success() {
let n = Notification::success("Operation completed");
assert_eq!(n.kind, NotificationKind::Success);
}
#[test]
fn test_notification_info() {
let n = Notification::info("FYI");
assert_eq!(n.kind, NotificationKind::Info);
}
#[test]
fn test_notification_warning() {
let n = Notification::warning("Careful!");
assert_eq!(n.kind, NotificationKind::Warning);
}
#[test]
fn test_notification_not_expired_immediately() {
let n = Notification::success("Test");
assert!(!n.is_expired());
}
#[test]
fn test_notification_string_conversion() {
let n = Notification::success(String::from("Owned string"));
assert_eq!(n.message, "Owned string");
}
}