rsonpath/
classification.rs

1//! Classifiers working on the input stream.
2//!
3//! - [`quotes`] contains the low-level [`QuoteClassifiedIterator`](`quotes::QuoteClassifiedIterator`)
4//!   computing basic information on which characters are escaped or within quotes.
5//! - [`structural`] contains the [`StructuralIterator`](`structural::StructuralIterator`)
6//!   that wraps over a quote classifier to extract a stream of [`Structural`](`structural::Structural`) characters.
7//! - [`depth`] contains the [`DepthIterator`](`depth::DepthIterator`) that works on top of a quote classifier
8//!   to provide quick fast-forwarding over the stream while keeping track of the depth.
9//!
10//! This base module provides the [`ResumeClassifierState`] struct common between all
11//! higher-level classifiers that work on top of a [`QuoteClassifiedIterator`](`quotes::QuoteClassifiedIterator`).
12//! It allows saving the state of a classifier and can be later used to resume classification
13//! from a, possibly different, high-level classifier. This state's index can be pushed
14//! forward.
15#[cfg(test)]
16mod classifier_correctness_tests;
17pub mod depth;
18pub(crate) mod mask;
19pub mod memmem;
20pub mod quotes;
21pub(crate) mod simd;
22pub mod structural;
23
24use std::fmt::Display;
25
26use crate::{debug, input::InputBlockIterator};
27use quotes::{QuoteClassifiedBlock, QuoteClassifiedIterator};
28
29/// State allowing resumption of a classifier from a particular place
30/// in the input along with the stopped [`QuoteClassifiedIterator`].
31pub struct ResumeClassifierState<'i, I, Q, M, const N: usize>
32where
33    I: InputBlockIterator<'i, N>,
34{
35    /// The stopped iterator.
36    pub iter: Q,
37    /// The block at which classification was stopped.
38    pub block: Option<ResumeClassifierBlockState<'i, I, M, N>>,
39    /// Was comma classification turned on when the classification was stopped.
40    pub are_commas_on: bool,
41    /// Was colon classification turned on when the classification was stopped.
42    pub are_colons_on: bool,
43}
44
45/// State of the block at which classification was stopped.
46pub struct ResumeClassifierBlockState<'i, I, M, const N: usize>
47where
48    I: InputBlockIterator<'i, N>,
49{
50    /// Quote classified information about the block.
51    pub block: QuoteClassifiedBlock<I::Block, M, N>,
52    /// The index at which classification was stopped.
53    pub idx: usize,
54}
55
56impl<'i, I, Q, M, const N: usize> ResumeClassifierState<'i, I, Q, M, N>
57where
58    I: InputBlockIterator<'i, N>,
59    Q: QuoteClassifiedIterator<'i, I, M, N>,
60{
61    /// Get the index in the original bytes input at which classification has stopped.
62    #[inline(always)]
63    pub(crate) fn get_idx(&self) -> usize {
64        debug!(
65            "iter offset: {}, block idx: {:?}",
66            self.iter.get_offset(),
67            self.block.as_ref().map(|b| b.idx)
68        );
69
70        self.iter.get_offset() + self.block.as_ref().map_or(0, |b| b.idx)
71    }
72}
73
74/// Get a human-readable description of SIMD capabilities supported by rsonpath
75/// on the current machine.
76#[doc(hidden)]
77#[inline]
78#[must_use]
79pub fn describe_simd() -> impl Display {
80    simd::configure()
81}