use crate::Result;
use super::super::{
delta::DeltaBatch,
state::{ParserState, SectionKind, StreamingContext},
};
pub struct LineProcessor {
pub state: ParserState,
pub context: StreamingContext,
}
impl LineProcessor {
#[must_use]
pub const fn new() -> Self {
Self {
state: ParserState::ExpectingSection,
context: StreamingContext::new(),
}
}
pub fn process_line(&mut self, line: &str) -> Result<DeltaBatch<'static>> {
self.context.next_line();
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with(';') || trimmed.starts_with("!:") {
return Ok(DeltaBatch::new());
}
if trimmed.starts_with('[') && trimmed.ends_with(']') {
return Ok(self.process_section_header(trimmed));
}
match &self.state {
ParserState::ExpectingSection => {
Ok(DeltaBatch::new())
}
ParserState::InSection(section_kind) => {
Ok(self.process_section_content(line, *section_kind))
}
ParserState::InEvent {
section,
fields_seen,
} => Ok(self.process_event_continuation(line, *section, *fields_seen)),
}
}
fn process_section_header(&mut self, line: &str) -> DeltaBatch<'static> {
let section_name = &line[1..line.len() - 1]; let section_kind = SectionKind::from_header(section_name);
self.state.enter_section(section_kind);
self.context.enter_section(section_kind);
if section_kind.expects_format() {
match section_kind {
SectionKind::Events => self.context.events_format = None,
SectionKind::Styles => self.context.styles_format = None,
_ => {}
}
}
DeltaBatch::new()
}
fn process_section_content(
&mut self,
line: &str,
section_kind: SectionKind,
) -> DeltaBatch<'static> {
match section_kind {
SectionKind::ScriptInfo => Self::process_script_info_line(line),
SectionKind::Styles => self.process_styles_line(line),
SectionKind::Events => self.process_events_line(line),
SectionKind::Fonts | SectionKind::Graphics => Self::process_binary_line(line),
SectionKind::Unknown => {
DeltaBatch::new()
}
}
}
pub fn reset(&mut self) {
self.state = ParserState::ExpectingSection;
self.context.reset();
}
}
impl Default for LineProcessor {
fn default() -> Self {
Self::new()
}
}