use super::context_watch::ContextReporter;
use super::keys::{ServicePublisher, service_envelope};
use super::{ServiceAction, ServiceEffectError, ServiceKind};
use std::sync::Arc;
pub const ALARMS_SCHEMA: &[u8] = b"liminal.services.alarms/1";
pub const LAUNCHES_SCHEMA: &[u8] = b"liminal.services.launches/1";
#[derive(Debug)]
pub struct PublishingActions {
publisher: Arc<dyn ServicePublisher>,
context: Arc<ContextReporter>,
}
impl PublishingActions {
#[must_use]
pub const fn new(publisher: Arc<dyn ServicePublisher>, context: Arc<ContextReporter>) -> Self {
Self { publisher, context }
}
}
impl ServiceAction for PublishingActions {
fn act(
&self,
service: ServiceKind,
payload: &[u8],
operation: &str,
) -> Result<(), ServiceEffectError> {
let schema = match service {
ServiceKind::Alarms => ALARMS_SCHEMA,
ServiceKind::Launches => LAUNCHES_SCHEMA,
ServiceKind::ContextWatch => return self.context.report(payload, operation),
ServiceKind::Seats | ServiceKind::Budgets | ServiceKind::SessionKeys => {
return Err(ServiceEffectError(format!(
"{} is not acted on as a bare service",
service.name()
)));
}
};
if payload.is_empty() {
return Err(ServiceEffectError(format!(
"an empty {} request carries nothing to publish",
service.name()
)));
}
self.publisher.publish(
service.subject_shape(),
&service_envelope(schema, payload),
operation,
)
}
}