tabnas_alchemy/shared/
route.rs1use std::sync::Arc;
14
15use crate::shared::datum::Datum;
16use crate::shared::error::Fail;
17use crate::shared::matcher::CaptureId;
18use crate::shared::selector::{Path, Selector};
19use crate::shared::sink::Flow;
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub enum CaptureMode {
24 Materialize,
26 Observe,
28}
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub struct Budget {
34 pub bytes: usize,
35 pub name: &'static str,
36}
37
38#[derive(Clone, Debug, PartialEq, Eq)]
40pub struct CaptureSpec {
41 pub tag: Arc<str>,
44 pub selector: Selector,
45 pub mode: CaptureMode,
46 pub budget: Option<Budget>,
50}
51
52impl CaptureSpec {
53 pub fn new(tag: impl Into<Arc<str>>, selector: Selector, mode: CaptureMode) -> CaptureSpec {
54 CaptureSpec {
55 tag: tag.into(),
56 selector,
57 mode,
58 budget: None,
59 }
60 }
61
62 pub fn materialize(tag: impl Into<Arc<str>>, selector: Selector) -> CaptureSpec {
63 CaptureSpec::new(tag, selector, CaptureMode::Materialize)
64 }
65
66 pub fn observe(tag: impl Into<Arc<str>>, selector: Selector) -> CaptureSpec {
67 CaptureSpec::new(tag, selector, CaptureMode::Observe)
68 }
69
70 pub fn budget(mut self, bytes: usize, name: &'static str) -> CaptureSpec {
71 self.budget = Some(Budget { bytes, name });
72 self
73 }
74}
75
76#[derive(Clone, Debug, PartialEq)]
78pub struct Selected {
79 pub id: CaptureId,
82 pub tag: Arc<str>,
83 pub path: Path,
84 pub value: Option<Datum>,
86}
87
88pub trait RouteSink {
90 fn began(&mut self, id: CaptureId, tag: &str) -> Result<(), Fail> {
95 let _ = (id, tag);
96 Ok(())
97 }
98
99 fn selected(&mut self, selected: Selected) -> Result<Flow, Fail>;
100
101 fn end(&mut self) -> Result<Flow, Fail>;
103}
104
105impl RouteSink for Vec<Selected> {
106 fn selected(&mut self, selected: Selected) -> Result<Flow, Fail> {
107 self.push(selected);
108 Ok(Flow::Continue)
109 }
110
111 fn end(&mut self) -> Result<Flow, Fail> {
112 Ok(Flow::Continue)
113 }
114}
115
116pub struct FnRoute<F>(pub F);
118
119impl<F> RouteSink for FnRoute<F>
120where
121 F: FnMut(Selected) -> Result<Flow, Fail>,
122{
123 fn selected(&mut self, selected: Selected) -> Result<Flow, Fail> {
124 (self.0)(selected)
125 }
126
127 fn end(&mut self) -> Result<Flow, Fail> {
128 Ok(Flow::Continue)
129 }
130}