reovim_plugin_notification/
notification.rs

1//! Notification types
2//!
3//! Defines notification levels and structure.
4
5use std::time::Instant;
6
7/// Notification severity level
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum NotificationLevel {
10    /// Informational message
11    #[default]
12    Info,
13    /// Success/completion message
14    Success,
15    /// Warning message
16    Warning,
17    /// Error message
18    Error,
19}
20
21impl NotificationLevel {
22    /// Get display icon for the level
23    #[must_use]
24    pub const fn icon(&self) -> &'static str {
25        match self {
26            Self::Info => "󰋽 ",
27            Self::Success => "󰄬 ",
28            Self::Warning => "󰀦 ",
29            Self::Error => "󰅚 ",
30        }
31    }
32}
33
34/// A single notification
35#[derive(Debug, Clone)]
36pub struct Notification {
37    /// Unique identifier
38    pub id: String,
39    /// Severity level
40    pub level: NotificationLevel,
41    /// Message content
42    pub message: String,
43    /// Duration to display in milliseconds
44    pub duration_ms: u64,
45    /// When the notification was created
46    pub created_at: Instant,
47    /// Optional source (e.g., "LSP", "rust-analyzer")
48    pub source: Option<String>,
49}
50
51impl Notification {
52    /// Create a new notification
53    #[must_use]
54    pub fn new(level: NotificationLevel, message: impl Into<String>) -> Self {
55        Self {
56            id: format!("{:?}", Instant::now()),
57            level,
58            message: message.into(),
59            duration_ms: 3000,
60            created_at: Instant::now(),
61            source: None,
62        }
63    }
64
65    /// Set the duration
66    #[must_use]
67    pub const fn with_duration(mut self, duration_ms: u64) -> Self {
68        self.duration_ms = duration_ms;
69        self
70    }
71
72    /// Set the source
73    #[must_use]
74    pub fn with_source(mut self, source: impl Into<String>) -> Self {
75        self.source = Some(source.into());
76        self
77    }
78
79    /// Check if this notification has expired
80    #[must_use]
81    #[allow(clippy::cast_possible_truncation)]
82    pub fn is_expired(&self) -> bool {
83        self.created_at.elapsed().as_millis() as u64 >= self.duration_ms
84    }
85}