ferro_notifications/
notification.rs1use crate::channel::Channel;
4use crate::channels::{
5 DatabaseMessage, InAppMessage, MailMessage, PushMessage, SlackMessage, SmsMessage,
6 WhatsAppMessage,
7};
8
9pub trait Notification: Send + Sync {
43 fn via(&self) -> Vec<Channel>;
45
46 fn to_mail(&self) -> Option<MailMessage> {
48 None
49 }
50
51 fn to_database(&self) -> Option<DatabaseMessage> {
53 None
54 }
55
56 fn to_slack(&self) -> Option<SlackMessage> {
58 None
59 }
60
61 fn to_whatsapp(&self) -> Option<WhatsAppMessage> {
63 None
64 }
65
66 fn to_in_app(&self) -> Option<InAppMessage> {
68 None
69 }
70
71 fn to_sms(&self) -> Option<SmsMessage> {
73 None
74 }
75
76 fn to_push(&self) -> Option<PushMessage> {
78 None
79 }
80
81 fn notification_type(&self) -> &'static str {
83 std::any::type_name::<Self>()
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 struct TestNotification;
92
93 impl Notification for TestNotification {
94 fn via(&self) -> Vec<Channel> {
95 vec![Channel::Mail, Channel::Database]
96 }
97
98 fn to_mail(&self) -> Option<MailMessage> {
99 Some(MailMessage::new().subject("Test").body("Test body"))
100 }
101 }
102
103 #[test]
104 fn test_notification_via() {
105 let notification = TestNotification;
106 let channels = notification.via();
107 assert_eq!(channels.len(), 2);
108 assert!(channels.contains(&Channel::Mail));
109 assert!(channels.contains(&Channel::Database));
110 }
111
112 #[test]
113 fn test_notification_to_mail() {
114 let notification = TestNotification;
115 let mail = notification.to_mail();
116 assert!(mail.is_some());
117 let mail = mail.unwrap();
118 assert_eq!(mail.subject, "Test");
119 }
120
121 #[test]
122 fn test_notification_to_database_default() {
123 let notification = TestNotification;
124 assert!(notification.to_database().is_none());
125 }
126
127 #[test]
128 fn test_notification_to_whatsapp_default() {
129 let notification = TestNotification;
130 assert!(notification.to_whatsapp().is_none());
131 }
132
133 #[test]
134 fn test_notification_to_in_app_default() {
135 let notification = TestNotification;
136 assert!(notification.to_in_app().is_none());
137 }
138
139 #[test]
140 fn test_notification_to_sms_default() {
141 let notification = TestNotification;
142 assert!(notification.to_sms().is_none());
143 }
144
145 #[test]
146 fn test_notification_to_push_default() {
147 let notification = TestNotification;
148 assert!(notification.to_push().is_none());
149 }
150}