tuiserial_core/
notification.rs

1//! Notification types and functionality
2//!
3//! This module defines notification messages shown to users with different
4//! severity levels and automatic expiration.
5
6use std::time::Instant;
7
8/// Notification level for user messages
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum NotificationLevel {
11    Info,
12    Warning,
13    Error,
14    Success,
15}
16
17/// A notification message shown to the user
18#[derive(Debug, Clone)]
19pub struct Notification {
20    pub message: String,
21    pub level: NotificationLevel,
22    pub created_at: Instant,
23    pub duration_ms: u64,
24}
25
26impl Notification {
27    /// Create a new notification with the specified level
28    pub fn new(message: String, level: NotificationLevel) -> Self {
29        Self {
30            message,
31            level,
32            created_at: Instant::now(),
33            duration_ms: 3000, // Default 3 seconds
34        }
35    }
36
37    /// Create an info notification
38    pub fn info(message: String) -> Self {
39        Self::new(message, NotificationLevel::Info)
40    }
41
42    /// Create a warning notification
43    pub fn warning(message: String) -> Self {
44        Self::new(message, NotificationLevel::Warning)
45    }
46
47    /// Create an error notification
48    pub fn error(message: String) -> Self {
49        Self::new(message, NotificationLevel::Error)
50    }
51
52    /// Create a success notification
53    pub fn success(message: String) -> Self {
54        Self::new(message, NotificationLevel::Success)
55    }
56
57    /// Check if the notification has expired
58    pub fn is_expired(&self) -> bool {
59        self.created_at.elapsed().as_millis() as u64 > self.duration_ms
60    }
61}