use std::sync::Arc;
use serenade_di::{CompilePass, ContainerBuilder, DiError, ServiceDefinition};
use crate::{Notification, NotifierError, NullTransport, Transport};
pub const NOTIFIER_TRANSPORT_TAG: &str = "notifier.transport";
pub const DEFAULT_NOTIFIER_SERVICE: &str = "notifier";
#[derive(Clone)]
pub struct NotifierService(pub Arc<dyn Transport>);
impl NotifierService {
pub fn send(&self, notification: &Notification) -> Result<(), NotifierError> {
self.0.send(notification)
}
}
#[derive(Debug, Default)]
pub struct RegisterDefaultNotifierPass;
impl CompilePass for RegisterDefaultNotifierPass {
fn name(&self) -> &'static str {
"register_default_notifier"
}
fn process(&self, builder: &mut ContainerBuilder) -> Result<(), DiError> {
let has_default = builder
.definitions()
.iter()
.any(|definition| definition.id() == DEFAULT_NOTIFIER_SERVICE);
if has_default {
return Ok(());
}
builder.register(
ServiceDefinition::new(DEFAULT_NOTIFIER_SERVICE).with_tag(NOTIFIER_TRANSPORT_TAG),
|_container| {
Ok(Box::new(NotifierService(
Arc::new(NullTransport::new()) as Arc<dyn Transport>
)))
},
)
}
}