Skip to main content

serenade_notifier/
memory.rs

1//! In-memory transport that records sent notifications (tests / local).
2
3use std::sync::Mutex;
4
5use crate::null::validate_for_send;
6use crate::{Channel, Notification, NotifierError, Transport};
7
8/// Records every successful send for inspection in tests and local apps.
9#[derive(Debug, Default)]
10pub struct MemoryTransport {
11    sent: Mutex<Vec<Notification>>,
12}
13
14impl MemoryTransport {
15    /// Empty recorder.
16    #[must_use]
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Snapshot of notifications accepted so far.
22    ///
23    /// # Panics
24    ///
25    /// Panics if the internal lock is poisoned.
26    #[must_use]
27    pub fn sent(&self) -> Vec<Notification> {
28        self.sent.lock().expect("memory transport lock").clone()
29    }
30
31    /// Clears recorded notifications.
32    ///
33    /// # Panics
34    ///
35    /// Panics if the internal lock is poisoned.
36    pub fn clear(&self) {
37        self.sent.lock().expect("memory transport lock").clear();
38    }
39}
40
41impl Transport for MemoryTransport {
42    fn supports(&self, channel: Channel) -> bool {
43        matches!(channel, Channel::Sms | Channel::Push)
44    }
45
46    fn send(&self, notification: &Notification) -> Result<(), NotifierError> {
47        validate_for_send(notification)?;
48        self.sent
49            .lock()
50            .expect("memory transport lock")
51            .push(notification.clone());
52        Ok(())
53    }
54}