use crate::parser::errors::ParseError;
use crate::parser::SectionType;
pub fn find_section_header_start(
source: &str,
start_hint: usize,
section_type: SectionType,
) -> Result<usize, ParseError> {
let header = match section_type {
SectionType::ScriptInfo => "[Script Info]",
SectionType::Styles => "[V4+ Styles]",
SectionType::Events => "[Events]",
SectionType::Fonts => "[Fonts]",
SectionType::Graphics => "[Graphics]",
};
let search_start = start_hint.saturating_sub(header.len() + 100); let search_text = &source[search_start..start_hint.min(source.len())];
search_text
.rfind(header)
.map_or(Err(ParseError::SectionNotFound), |pos| {
let header_pos = search_start + pos;
let line_start = source[..header_pos].rfind('\n').map_or(0, |p| p + 1);
Ok(line_start)
})
}
pub fn find_section_end(
source: &str,
end_hint: usize,
_section_type: SectionType,
) -> Result<usize, ParseError> {
let section_headers = [
"[Script Info]",
"[V4+ Styles]",
"[Events]",
"[Fonts]",
"[Graphics]",
];
let search_text = &source[end_hint..];
let mut min_pos = None;
for header in §ion_headers {
if let Some(pos) = search_text.find(header) {
min_pos = Some(min_pos.map_or(pos, |min: usize| min.min(pos)));
}
}
min_pos.map_or(Ok(source.len()), |pos| {
let next_section_pos = end_hint + pos;
let line_start = source[..next_section_pos].rfind('\n').map_or(0, |p| p + 1);
Ok(line_start)
})
}