Skip to main content

cloudiful_notifier/
lib.rs

1#[path = "../crates/notifier-core/src/lib.rs"]
2mod core;
3
4#[cfg(feature = "dingtalk")]
5#[path = "../crates/notifier-dingtalk/src/lib.rs"]
6mod dingtalk;
7#[cfg(feature = "email")]
8#[path = "../crates/notifier-email/src/lib.rs"]
9mod email;
10#[cfg(feature = "ntfy")]
11#[path = "../crates/notifier-ntfy/src/lib.rs"]
12mod ntfy;
13#[cfg(feature = "webhook")]
14#[path = "../crates/notifier-webhook/src/lib.rs"]
15mod webhook;
16
17pub use core::{
18    DeliveryChannel, DeliveryResult, MessageEnvelope, NotifierError,
19};
20
21#[cfg(feature = "dingtalk")]
22pub use dingtalk::DingtalkChannel;
23#[cfg(feature = "email")]
24pub use email::{EmailChannel, EmailTlsMode};
25#[cfg(feature = "ntfy")]
26pub use ntfy::NtfyChannel;
27#[cfg(feature = "webhook")]
28pub use webhook::WebhookChannel;
29
30#[derive(Debug, Clone)]
31pub struct Notifier {
32    http_client: reqwest::Client,
33}
34
35impl Notifier {
36    pub fn new(http_client: reqwest::Client) -> Self {
37        Self { http_client }
38    }
39
40    pub async fn send<C: DeliveryChannel>(
41        &self,
42        channel: &C,
43        message: &MessageEnvelope,
44    ) -> Result<DeliveryResult, NotifierError> {
45        channel.deliver(&self.http_client, message).await
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::{DeliveryChannel, DeliveryResult, MessageEnvelope, Notifier, NotifierError};
52
53    struct StubChannel;
54
55    impl DeliveryChannel for StubChannel {
56        async fn deliver(
57            &self,
58            _http_client: &reqwest::Client,
59            _message: &MessageEnvelope,
60        ) -> Result<DeliveryResult, NotifierError> {
61            Ok(DeliveryResult {
62                http_status: Some(202),
63            })
64        }
65    }
66
67    #[tokio::test]
68    async fn notifier_delegates_to_channel() {
69        let notifier = Notifier::new(reqwest::Client::new());
70        let message = MessageEnvelope::new("hello");
71
72        let result = notifier.send(&StubChannel, &message).await.unwrap();
73
74        assert_eq!(result.http_status, Some(202));
75    }
76}