use super::{EventType, Span};
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Event<'a> {
pub event_type: EventType,
pub layer: &'a str,
pub start: &'a str,
pub end: &'a str,
pub style: &'a str,
pub name: &'a str,
pub margin_l: &'a str,
pub margin_r: &'a str,
pub margin_v: &'a str,
pub margin_t: Option<&'a str>,
pub margin_b: Option<&'a str>,
pub effect: &'a str,
pub text: &'a str,
pub span: Span,
}
impl Event<'_> {
#[must_use]
pub const fn is_dialogue(&self) -> bool {
matches!(self.event_type, EventType::Dialogue)
}
#[must_use]
pub const fn is_comment(&self) -> bool {
matches!(self.event_type, EventType::Comment)
}
pub fn start_time_cs(&self) -> Result<u32, crate::utils::CoreError> {
crate::utils::parse_ass_time(self.start)
}
pub fn end_time_cs(&self) -> Result<u32, crate::utils::CoreError> {
crate::utils::parse_ass_time(self.end)
}
pub fn duration_cs(&self) -> Result<u32, crate::utils::CoreError> {
let start = self.start_time_cs()?;
let end = self.end_time_cs()?;
Ok(end.saturating_sub(start))
}
}
impl Default for Event<'_> {
fn default() -> Self {
Self {
event_type: EventType::Dialogue,
layer: "0",
start: "0:00:00.00",
end: "0:00:00.00",
style: "Default",
name: "",
margin_l: "0",
margin_r: "0",
margin_v: "0",
margin_t: None,
margin_b: None,
effect: "",
text: "",
span: Span::new(0, 0, 0, 0),
}
}
}