use bunsen::errors::BunsenResult;
use super::{
Clock,
Event,
Opts,
};
#[derive(Clone, Copy, Debug)]
pub(super) struct Cx {
pub(super) clock: Clock,
pub(super) diarized: bool,
}
pub(super) trait Sink {
fn push(
&mut self,
event: &Event,
cx: &Cx,
opts: &Opts,
) -> BunsenResult<()>;
fn finish(self: Box<Self>) -> BunsenResult<()>;
}
pub struct Publisher {
pub(super) sinks: Vec<(Opts, Box<dyn Sink>)>,
pub(super) cx: Cx,
}
impl Publisher {
#[cfg_attr(not(feature = "live"), allow(dead_code))]
pub fn set_clock(
&mut self,
clock: Clock,
) {
self.cx.clock = clock;
}
pub fn push(
&mut self,
event: &Event,
) -> BunsenResult<()> {
let kind = event.kind();
for (opts, sink) in &mut self.sinks {
if opts.events.contains(&kind) {
sink.push(event, &self.cx, opts)?;
}
}
Ok(())
}
pub fn finish(self) -> BunsenResult<()> {
let mut first = Ok(());
for (_, sink) in self.sinks {
let result = sink.finish();
if first.is_ok() {
first = result;
}
}
first
}
}