use std::sync::Mutex;
use crate::null::validate_for_send;
use crate::{Channel, Notification, NotifierError, Transport};
#[derive(Debug, Default)]
pub struct MemoryTransport {
sent: Mutex<Vec<Notification>>,
}
impl MemoryTransport {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn sent(&self) -> Vec<Notification> {
self.sent.lock().expect("memory transport lock").clone()
}
pub fn clear(&self) {
self.sent.lock().expect("memory transport lock").clear();
}
}
impl Transport for MemoryTransport {
fn supports(&self, channel: Channel) -> bool {
matches!(channel, Channel::Sms | Channel::Push)
}
fn send(&self, notification: &Notification) -> Result<(), NotifierError> {
validate_for_send(notification)?;
self.sent
.lock()
.expect("memory transport lock")
.push(notification.clone());
Ok(())
}
}