ass_core/parser/streaming/delta/parse_delta.rs
1//! Parse delta enum for streaming updates
2//!
3//! Defines [`ParseDelta`], the atomic change representation produced during
4//! incremental parsing along with its zero-copy constructors and inspectors.
5
6use crate::parser::ast::Section;
7use alloc::string::String;
8
9/// Delta operations for streaming updates
10///
11/// Represents atomic changes detected during incremental parsing.
12/// Used by editors and streaming applications to efficiently update
13/// internal state without full re-parsing.
14///
15/// # Performance
16///
17/// Deltas use zero-copy design where possible to minimize allocations.
18/// Section references point to parsed AST nodes using lifetime parameters.
19///
20/// # Example
21///
22/// ```rust
23/// use ass_core::parser::streaming::ParseDelta;
24///
25/// // Handle delta operations
26/// fn apply_deltas(deltas: Vec<ParseDelta>) {
27/// for delta in deltas {
28/// match delta {
29/// ParseDelta::AddSection(section) => {
30/// // Add new section to document
31/// }
32/// ParseDelta::UpdateSection(index, section) => {
33/// // Update existing section
34/// }
35/// ParseDelta::RemoveSection(index) => {
36/// // Remove section at index
37/// }
38/// ParseDelta::ParseIssue(issue) => {
39/// // Handle parsing error/warning
40/// }
41/// }
42/// }
43/// }
44/// ```
45#[derive(Debug, Clone)]
46pub enum ParseDelta<'a> {
47 /// Section was added to the script
48 ///
49 /// Contains the complete parsed section with zero-copy references
50 /// to source text spans.
51 AddSection(Section<'a>),
52
53 /// Section was modified during incremental parsing
54 ///
55 /// Contains the section index and updated section data. Consumers should replace
56 /// the existing section at the specified index with this new data.
57 UpdateSection(usize, Section<'a>),
58
59 /// Section was removed by index
60 ///
61 /// Contains the zero-based index of the section that should be
62 /// removed from the script.
63 RemoveSection(usize),
64
65 /// Parsing issue detected during processing
66 ///
67 /// Contains error or warning message about parsing problems.
68 /// Parsing may continue despite issues for error recovery.
69 ParseIssue(String),
70}
71
72impl<'a> ParseDelta<'a> {
73 /// Create delta for adding a section
74 #[must_use]
75 pub const fn add_section(section: Section<'a>) -> Self {
76 Self::AddSection(section)
77 }
78
79 /// Create delta for updating a section
80 #[must_use]
81 pub const fn update_section(index: usize, section: Section<'a>) -> Self {
82 Self::UpdateSection(index, section)
83 }
84
85 /// Create delta for removing a section by index
86 #[must_use]
87 pub const fn remove_section(index: usize) -> Self {
88 Self::RemoveSection(index)
89 }
90
91 /// Create delta for parsing issue
92 #[must_use]
93 pub const fn parse_issue(message: String) -> Self {
94 Self::ParseIssue(message)
95 }
96
97 /// Check if delta represents an error condition
98 #[must_use]
99 pub const fn is_error(&self) -> bool {
100 matches!(self, Self::ParseIssue(_))
101 }
102
103 /// Check if delta modifies document structure
104 #[must_use]
105 pub const fn is_structural(&self) -> bool {
106 matches!(
107 self,
108 Self::AddSection(_) | Self::UpdateSection(_, _) | Self::RemoveSection(_)
109 )
110 }
111
112 /// Get section reference if delta contains one
113 #[must_use]
114 pub const fn section(&self) -> Option<&Section<'a>> {
115 match self {
116 Self::AddSection(section) | Self::UpdateSection(_, section) => Some(section),
117 _ => None,
118 }
119 }
120}