rsonpath/result/
count.rs

1//! [`Recorder`] implementation for counting the number of matches.
2//!
3//! This is faster than any recorder that actually examines the values.
4use super::*;
5use std::cell::Cell;
6
7/// Recorder that keeps only the number of matches, no details.
8pub struct CountRecorder {
9    count: Cell<MatchCount>,
10}
11
12impl CountRecorder {
13    pub(crate) fn new() -> Self {
14        Self { count: Cell::new(0) }
15    }
16}
17
18impl From<CountRecorder> for MatchCount {
19    #[inline]
20    fn from(val: CountRecorder) -> Self {
21        val.count.into_inner()
22    }
23}
24
25impl<B: Deref<Target = [u8]>> InputRecorder<B> for CountRecorder {
26    #[inline(always)]
27    fn record_block_start(&self, _new_block: B) {
28        // Intentionally left empty.
29    }
30}
31
32impl<B: Deref<Target = [u8]>> Recorder<B> for CountRecorder {
33    #[inline]
34    fn record_match(&self, _idx: usize, _depth: Depth, _ty: MatchedNodeType) -> Result<(), EngineError> {
35        self.count.set(self.count.get() + 1);
36        Ok(())
37    }
38
39    #[inline]
40    fn record_value_terminator(&self, _idx: usize, _depth: Depth) -> Result<(), EngineError> {
41        // Intentionally left empty.
42        Ok(())
43    }
44}