use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
extern crate alloc;
use super::SectionType;
use crate::parser::ast::{Event, Font, Graphic, ScriptInfo, Span, Style};
#[cfg(debug_assertions)]
use core::ops::Range;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Section<'a> {
ScriptInfo(ScriptInfo<'a>),
Styles(Vec<Style<'a>>),
Events(Vec<Event<'a>>),
Fonts(Vec<Font<'a>>),
Graphics(Vec<Graphic<'a>>),
}
impl Section<'_> {
#[must_use]
pub fn span(&self) -> Option<Span> {
match self {
Section::ScriptInfo(info) => Some(info.span),
Section::Styles(styles) => {
if styles.is_empty() {
None
} else {
let first = &styles[0].span;
let last = &styles[styles.len() - 1].span;
Some(Span::new(first.start, last.end, first.line, first.column))
}
}
Section::Events(events) => {
if events.is_empty() {
None
} else {
let first = &events[0].span;
let last = &events[events.len() - 1].span;
Some(Span::new(first.start, last.end, first.line, first.column))
}
}
Section::Fonts(fonts) => {
if fonts.is_empty() {
None
} else {
let first = &fonts[0].span;
let last = &fonts[fonts.len() - 1].span;
Some(Span::new(first.start, last.end, first.line, first.column))
}
}
Section::Graphics(graphics) => {
if graphics.is_empty() {
None
} else {
let first = &graphics[0].span;
let last = &graphics[graphics.len() - 1].span;
Some(Span::new(first.start, last.end, first.line, first.column))
}
}
}
}
#[must_use]
pub const fn section_type(&self) -> SectionType {
match self {
Section::ScriptInfo(_) => SectionType::ScriptInfo,
Section::Styles(_) => SectionType::Styles,
Section::Events(_) => SectionType::Events,
Section::Fonts(_) => SectionType::Fonts,
Section::Graphics(_) => SectionType::Graphics,
}
}
#[cfg(debug_assertions)]
#[must_use]
pub fn validate_spans(&self, source_range: &Range<usize>) -> bool {
match self {
Section::ScriptInfo(info) => info.validate_spans(source_range),
Section::Styles(styles) => styles.iter().all(|s| s.validate_spans(source_range)),
Section::Events(events) => events.iter().all(|e| e.validate_spans(source_range)),
Section::Fonts(fonts) => fonts.iter().all(|f| f.validate_spans(source_range)),
Section::Graphics(graphics) => graphics.iter().all(|g| g.validate_spans(source_range)),
}
}
}