use std::sync::Arc;
use crate::shared::datum::Datum;
use crate::shared::error::Fail;
use crate::shared::matcher::CaptureId;
use crate::shared::selector::{Path, Selector};
use crate::shared::sink::Flow;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CaptureMode {
Materialize,
Observe,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Budget {
pub bytes: usize,
pub name: &'static str,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CaptureSpec {
pub tag: Arc<str>,
pub selector: Selector,
pub mode: CaptureMode,
pub budget: Option<Budget>,
}
impl CaptureSpec {
pub fn new(tag: impl Into<Arc<str>>, selector: Selector, mode: CaptureMode) -> CaptureSpec {
CaptureSpec {
tag: tag.into(),
selector,
mode,
budget: None,
}
}
pub fn materialize(tag: impl Into<Arc<str>>, selector: Selector) -> CaptureSpec {
CaptureSpec::new(tag, selector, CaptureMode::Materialize)
}
pub fn observe(tag: impl Into<Arc<str>>, selector: Selector) -> CaptureSpec {
CaptureSpec::new(tag, selector, CaptureMode::Observe)
}
pub fn budget(mut self, bytes: usize, name: &'static str) -> CaptureSpec {
self.budget = Some(Budget { bytes, name });
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Selected {
pub id: CaptureId,
pub tag: Arc<str>,
pub path: Path,
pub value: Option<Datum>,
}
pub trait RouteSink {
fn began(&mut self, id: CaptureId, tag: &str) -> Result<(), Fail> {
let _ = (id, tag);
Ok(())
}
fn selected(&mut self, selected: Selected) -> Result<Flow, Fail>;
fn end(&mut self) -> Result<Flow, Fail>;
}
impl RouteSink for Vec<Selected> {
fn selected(&mut self, selected: Selected) -> Result<Flow, Fail> {
self.push(selected);
Ok(Flow::Continue)
}
fn end(&mut self) -> Result<Flow, Fail> {
Ok(Flow::Continue)
}
}
pub struct FnRoute<F>(pub F);
impl<F> RouteSink for FnRoute<F>
where
F: FnMut(Selected) -> Result<Flow, Fail>,
{
fn selected(&mut self, selected: Selected) -> Result<Flow, Fail> {
(self.0)(selected)
}
fn end(&mut self) -> Result<Flow, Fail> {
Ok(Flow::Continue)
}
}