rsonpath/result/
index.rs

1//! [`Recorder`] implementation finding the starts of all matches.
2//! Faster than a full [`NodesRecorder`](super::nodes::NodesRecorder).
3use super::*;
4use std::cell::RefCell;
5
6/// Recorder that saves only the start indices to the [`Sink`].
7pub struct IndexRecorder<'s, S> {
8    sink: RefCell<&'s mut S>,
9    leading_padding_len: usize,
10}
11
12impl<'s, S> IndexRecorder<'s, S> {
13    #[inline]
14    pub(crate) fn new(sink: &'s mut S, leading_padding_len: usize) -> Self {
15        Self {
16            sink: RefCell::new(sink),
17            leading_padding_len,
18        }
19    }
20}
21
22impl<B: Deref<Target = [u8]>, S> InputRecorder<B> for IndexRecorder<'_, S>
23where
24    S: Sink<MatchIndex>,
25{
26    #[inline(always)]
27    fn record_block_start(&self, _new_block: B) {
28        // Intentionally left empty.
29    }
30}
31
32impl<B: Deref<Target = [u8]>, S> Recorder<B> for IndexRecorder<'_, S>
33where
34    S: Sink<MatchIndex>,
35{
36    #[inline]
37    fn record_match(&self, idx: usize, _depth: Depth, _ty: MatchedNodeType) -> Result<(), EngineError> {
38        self.sink
39            .borrow_mut()
40            .add_match(idx - self.leading_padding_len)
41            .map_err(|err| EngineError::SinkError(Box::new(err)))
42    }
43
44    #[inline]
45    fn record_value_terminator(&self, _idx: usize, _depth: Depth) -> Result<(), EngineError> {
46        // Intentionally left empty.
47        Ok(())
48    }
49}