Skip to main content

serenade_notifier/
notification.rs

1//! Notification payloads for SMS and push.
2
3use crate::Channel;
4
5/// SMS payload (phone + body).
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct SmsMessage {
8    to: String,
9    body: String,
10}
11
12impl SmsMessage {
13    /// Builds an SMS message.
14    #[must_use]
15    pub fn new(to: impl Into<String>, body: impl Into<String>) -> Self {
16        Self {
17            to: to.into(),
18            body: body.into(),
19        }
20    }
21
22    /// Phone / MSISDN recipient.
23    #[must_use]
24    pub fn to(&self) -> &str {
25        &self.to
26    }
27
28    /// Message body.
29    #[must_use]
30    pub fn body(&self) -> &str {
31        &self.body
32    }
33}
34
35/// Push payload (device token + title + body).
36#[derive(Clone, Debug, Eq, PartialEq)]
37pub struct PushMessage {
38    device_token: String,
39    title: String,
40    body: String,
41}
42
43impl PushMessage {
44    /// Builds a push message.
45    #[must_use]
46    pub fn new(
47        device_token: impl Into<String>,
48        title: impl Into<String>,
49        body: impl Into<String>,
50    ) -> Self {
51        Self {
52            device_token: device_token.into(),
53            title: title.into(),
54            body: body.into(),
55        }
56    }
57
58    /// Device token / registration id.
59    #[must_use]
60    pub fn device_token(&self) -> &str {
61        &self.device_token
62    }
63
64    /// Notification title.
65    #[must_use]
66    pub fn title(&self) -> &str {
67        &self.title
68    }
69
70    /// Notification body.
71    #[must_use]
72    pub fn body(&self) -> &str {
73        &self.body
74    }
75}
76
77/// Outgoing notification before a transport sends it.
78#[derive(Clone, Debug, Eq, PartialEq)]
79pub enum Notification {
80    /// SMS channel.
81    Sms(SmsMessage),
82    /// Push channel.
83    Push(PushMessage),
84}
85
86impl Notification {
87    /// Channel for this notification.
88    #[must_use]
89    pub const fn channel(&self) -> Channel {
90        match self {
91            Self::Sms(_) => Channel::Sms,
92            Self::Push(_) => Channel::Push,
93        }
94    }
95
96    /// SMS constructor.
97    #[must_use]
98    pub fn sms(to: impl Into<String>, body: impl Into<String>) -> Self {
99        Self::Sms(SmsMessage::new(to, body))
100    }
101
102    /// Push constructor.
103    #[must_use]
104    pub fn push(
105        device_token: impl Into<String>,
106        title: impl Into<String>,
107        body: impl Into<String>,
108    ) -> Self {
109        Self::Push(PushMessage::new(device_token, title, body))
110    }
111}