streamson_lib/
handler.rs

1//! Collections of handler (what to do with matched paths and data).
2//!
3
4pub mod analyser;
5pub mod buffer;
6pub mod group;
7pub mod indenter;
8pub mod indexer;
9pub mod output;
10#[cfg(feature = "with_regex")]
11pub mod regex;
12pub mod replace;
13pub mod shorten;
14pub mod unstringify;
15
16use std::any::Any;
17
18use crate::{error, path::Path, streamer::Token};
19
20pub use self::analyser::Analyser;
21pub use self::buffer::Buffer;
22pub use self::group::Group;
23pub use self::indenter::Indenter;
24pub use self::indexer::Indexer;
25pub use self::output::Output;
26#[cfg(feature = "with_regex")]
27pub use self::regex::Regex;
28pub use self::replace::Replace;
29pub use self::shorten::Shorten;
30pub use self::unstringify::Unstringify;
31
32/// Common handler trait
33pub trait Handler: Send {
34    /// Is called when a path is matched
35    ///
36    /// # Arguments
37    /// * `path` - path which was matched
38    /// * `matcher_idx`- idx of matcher which was used
39    /// * `token` - further info about matched data
40    ///
41    /// # Returns
42    /// * `Ok(None)` - All went well, no output
43    /// * `Ok(Some(data))` - All went, handler has some output
44    /// * `Err(_)` - Failed to execute handler
45    fn start(
46        &mut self,
47        _path: &Path,
48        _matcher_idx: usize,
49        _token: Token,
50    ) -> Result<Option<Vec<u8>>, error::Handler> {
51        Ok(None)
52    }
53
54    /// Is called when handler receives some data
55    ///
56    /// # Arguments
57    /// * `data` - a part of matched data
58    /// * `matcher_idx`- idx of matcher which was used
59    ///
60    /// # Returns
61    /// * `Ok(None)` - All went well, no output
62    /// * `Ok(Some(data))` - All went, handler has some output
63    /// * `Err(_)` - Failed to execute handler
64    fn feed(
65        &mut self,
66        _data: &[u8],
67        _matcher_idx: usize,
68    ) -> Result<Option<Vec<u8>>, error::Handler> {
69        Ok(None)
70    }
71
72    /// Is called when the path is no longer matched
73    ///
74    /// # Arguments
75    /// * `path` - path which was matched
76    /// * `matcher_idx`- idx of matcher which was used
77    /// * `token` - further info about matched data
78    ///
79    /// # Returns
80    /// * `Ok(None)` - All went well, no data conversion needed
81    /// * `Ok(Some(data))` - All went well, data converted
82    /// * `Err(_)` - Failed to execute handler
83    fn end(
84        &mut self,
85        _path: &Path,
86        _matcher_idx: usize,
87        _token: Token,
88    ) -> Result<Option<Vec<u8>>, error::Handler> {
89        Ok(None)
90    }
91
92    /// Should be handler used to convert data
93    fn is_converter(&self) -> bool {
94        false
95    }
96
97    /// Function to allow downcasting
98    fn as_any(&self) -> &dyn Any;
99}