nanotify_core/
registry.rs1use crate::{Notification, Notify, NotifyError, NotifyReceipt};
2use std::fmt;
3use std::sync::{Arc, OnceLock};
4
5pub const DEFAULT_PROVIDER_ID: &str = "default";
7
8static NOTIFIER: OnceLock<Arc<dyn Notify>> = OnceLock::new();
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct AlreadyInitialized;
13
14impl fmt::Display for AlreadyInitialized {
15 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
22 formatter.write_str("notification implementation is already initialized")
23 }
24}
25
26impl std::error::Error for AlreadyInitialized {}
27
28pub fn init(provider: Arc<dyn Notify>) -> Result<(), AlreadyInitialized> {
35 NOTIFIER.set(provider).map_err(|_| AlreadyInitialized)
37}
38
39pub fn get() -> Option<&'static Arc<dyn Notify>> {
45 NOTIFIER.get()
46}
47
48pub async fn notify(notification: &Notification) -> Result<Option<NotifyReceipt>, NotifyError> {
56 let Some(provider) = get() else {
57 return Ok(None);
58 };
59 provider.notify(notification).await.map(Some)
60}