use std::{error::Error, fmt::Display, sync::Arc};
use async_trait::async_trait;
use crate::communication::RegistrationError;
use crate::{UListener, UStatus, UUri};
use super::{CallOptions, UPayload};
#[derive(Debug)]
pub enum NotificationError {
InvalidArgument(String),
NotifyError(UStatus),
}
#[cfg(not(tarpaulin_include))]
impl Display for NotificationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NotificationError::InvalidArgument(s) => f.write_str(s.as_str()),
NotificationError::NotifyError(s) => {
f.write_fmt(format_args!("failed to send notification: {s}"))
}
}
}
}
impl Error for NotificationError {}
#[cfg_attr(any(test, feature = "test-util"), mockall::automock)]
#[async_trait]
pub trait Notifier: Send + Sync {
async fn notify(
&self,
resource_id: u16,
destination: &UUri,
call_options: CallOptions,
payload: Option<UPayload>,
) -> Result<(), NotificationError>;
async fn start_listening(
&self,
topic: &UUri,
listener: Arc<dyn UListener>,
) -> Result<(), RegistrationError>;
async fn stop_listening(
&self,
topic: &UUri,
listener: Arc<dyn UListener>,
) -> Result<(), RegistrationError>;
}