reovim_plugin_notification/
notification.rs1use std::time::Instant;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum NotificationLevel {
10 #[default]
12 Info,
13 Success,
15 Warning,
17 Error,
19}
20
21impl NotificationLevel {
22 #[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#[derive(Debug, Clone)]
36pub struct Notification {
37 pub id: String,
39 pub level: NotificationLevel,
41 pub message: String,
43 pub duration_ms: u64,
45 pub created_at: Instant,
47 pub source: Option<String>,
49}
50
51impl Notification {
52 #[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 #[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 #[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 #[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}