Skip to main content

ass_core/parser/streaming/state/
parser_state.rs

1//! Streaming parser state machine for incremental processing.
2//!
3//! Defines [`ParserState`], tracking the current parsing context to handle
4//! partial data and section boundaries correctly during streaming.
5
6use super::SectionKind;
7
8/// Streaming parser state for incremental processing
9///
10/// Tracks current parsing context to handle partial data and
11/// section boundaries correctly during streaming.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum ParserState {
14    /// Expecting section header or document start
15    ExpectingSection,
16    /// Currently parsing a specific section
17    InSection(SectionKind),
18    /// Parsing an event with potentially incomplete data
19    InEvent {
20        /// Which section type we're in
21        section: SectionKind,
22        /// Number of fields processed so far
23        fields_seen: usize,
24    },
25}
26
27impl ParserState {
28    /// Check if currently inside a section
29    #[must_use]
30    pub const fn is_in_section(&self) -> bool {
31        matches!(self, Self::InSection(_) | Self::InEvent { .. })
32    }
33
34    /// Get current section kind if in a section
35    #[must_use]
36    pub const fn current_section(&self) -> Option<SectionKind> {
37        match self {
38            Self::ExpectingSection => None,
39            Self::InSection(kind) => Some(*kind),
40            Self::InEvent { section, .. } => Some(*section),
41        }
42    }
43
44    /// Transition to new section
45    pub fn enter_section(&mut self, kind: SectionKind) {
46        *self = Self::InSection(kind);
47    }
48
49    /// Begin event parsing within section
50    pub fn enter_event(&mut self, section: SectionKind) {
51        *self = Self::InEvent {
52            section,
53            fields_seen: 0,
54        };
55    }
56
57    /// Exit current section
58    pub fn exit_section(&mut self) {
59        *self = Self::ExpectingSection;
60    }
61}