Skip to main content

serenade_notifier/
null.rs

1//! Null transport (validates then discards).
2
3use crate::{Channel, Notification, NotifierError, Transport};
4
5/// Discards every notification after validation (Symfony `NullTransport` analogue).
6#[derive(Clone, Copy, Debug, Default)]
7pub struct NullTransport;
8
9impl NullTransport {
10    /// Creates a null transport that supports SMS and push.
11    #[must_use]
12    pub const fn new() -> Self {
13        Self
14    }
15}
16
17impl Transport for NullTransport {
18    fn supports(&self, channel: Channel) -> bool {
19        matches!(channel, Channel::Sms | Channel::Push)
20    }
21
22    fn send(&self, notification: &Notification) -> Result<(), NotifierError> {
23        validate_for_send(notification)
24    }
25}
26
27/// Shared validation for SMS / push payloads.
28pub fn validate_for_send(notification: &Notification) -> Result<(), NotifierError> {
29    match notification {
30        Notification::Sms(message) => {
31            if message.to().trim().is_empty() {
32                return Err(NotifierError::MissingRecipient);
33            }
34            if message.body().trim().is_empty() {
35                return Err(NotifierError::MissingBody);
36            }
37        }
38        Notification::Push(message) => {
39            if message.device_token().trim().is_empty() {
40                return Err(NotifierError::MissingRecipient);
41            }
42            if message.body().trim().is_empty() {
43                return Err(NotifierError::MissingBody);
44            }
45        }
46    }
47    Ok(())
48}