Skip to main content

ass_editor/core/builders/
event.rs

1//! Event builder type with fluent constructors and field setters.
2
3use ass_core::parser::ast::EventType;
4
5#[cfg(feature = "std")]
6use std::borrow::Cow;
7
8#[cfg(not(feature = "std"))]
9use alloc::{borrow::Cow, string::ToString};
10
11/// Builder for creating ASS events with fluent API
12///
13/// Provides an ergonomic way to construct ASS events with method chaining.
14/// Supports all event types and automatically handles format validation.
15///
16/// # Examples
17///
18/// ```
19/// use ass_editor::{EventBuilder, EditorDocument};
20///
21/// let mut doc = EditorDocument::new();
22///
23/// // Create a dialogue event
24/// let event_line = EventBuilder::dialogue()
25///     .start_time("0:00:00.00")
26///     .end_time("0:00:05.00")
27///     .style("Default")
28///     .speaker("Character")
29///     .text("Hello, world!")
30///     .layer(0)
31///     .build()
32///     .unwrap();
33///
34/// // Add to document
35/// doc.add_event_line(&event_line).unwrap();
36/// ```
37#[derive(Debug, Default)]
38pub struct EventBuilder<'a> {
39    pub(super) event_type: Option<EventType>,
40    pub(super) start: Option<Cow<'a, str>>,
41    pub(super) end: Option<Cow<'a, str>>,
42    pub(super) style: Option<Cow<'a, str>>,
43    pub(super) name: Option<Cow<'a, str>>,
44    pub(super) text: Option<Cow<'a, str>>,
45    pub(super) layer: Option<Cow<'a, str>>,
46    pub(super) margin_l: Option<Cow<'a, str>>,
47    pub(super) margin_r: Option<Cow<'a, str>>,
48    pub(super) margin_v: Option<Cow<'a, str>>,
49    pub(super) margin_t: Option<Cow<'a, str>>,
50    pub(super) margin_b: Option<Cow<'a, str>>,
51    pub(super) effect: Option<Cow<'a, str>>,
52}
53
54impl<'a> EventBuilder<'a> {
55    /// Create a new event builder
56    pub fn new() -> Self {
57        Self::default()
58    }
59
60    /// Create a dialogue event builder
61    pub fn dialogue() -> Self {
62        Self {
63            event_type: Some(EventType::Dialogue),
64            ..Self::default()
65        }
66    }
67
68    /// Create a comment event builder
69    pub fn comment() -> Self {
70        Self {
71            event_type: Some(EventType::Comment),
72            ..Self::default()
73        }
74    }
75
76    /// Set start time (e.g., "0:00:05.00")
77    pub fn start_time<S: Into<Cow<'a, str>>>(mut self, time: S) -> Self {
78        self.start = Some(time.into());
79        self
80    }
81
82    /// Set end time (e.g., "0:00:10.00")
83    pub fn end_time<S: Into<Cow<'a, str>>>(mut self, time: S) -> Self {
84        self.end = Some(time.into());
85        self
86    }
87
88    /// Set speaker/character name
89    pub fn speaker<S: Into<Cow<'a, str>>>(mut self, name: S) -> Self {
90        self.name = Some(name.into());
91        self
92    }
93
94    /// Set dialogue text
95    pub fn text<S: Into<Cow<'a, str>>>(mut self, text: S) -> Self {
96        self.text = Some(text.into());
97        self
98    }
99
100    /// Set style name
101    pub fn style<S: Into<Cow<'a, str>>>(mut self, style: S) -> Self {
102        self.style = Some(style.into());
103        self
104    }
105
106    /// Set layer (higher layers render on top)
107    pub fn layer(mut self, layer: u32) -> Self {
108        self.layer = Some(Cow::Owned(layer.to_string()));
109        self
110    }
111
112    /// Set left margin
113    pub fn margin_left(mut self, margin: u32) -> Self {
114        self.margin_l = Some(Cow::Owned(margin.to_string()));
115        self
116    }
117
118    /// Set right margin
119    pub fn margin_right(mut self, margin: u32) -> Self {
120        self.margin_r = Some(Cow::Owned(margin.to_string()));
121        self
122    }
123
124    /// Set vertical margin
125    pub fn margin_vertical(mut self, margin: u32) -> Self {
126        self.margin_v = Some(Cow::Owned(margin.to_string()));
127        self
128    }
129
130    /// Set top margin (V4++)
131    pub fn margin_top(mut self, margin: u32) -> Self {
132        self.margin_t = Some(Cow::Owned(margin.to_string()));
133        self
134    }
135
136    /// Set bottom margin (V4++)
137    pub fn margin_bottom(mut self, margin: u32) -> Self {
138        self.margin_b = Some(Cow::Owned(margin.to_string()));
139        self
140    }
141
142    /// Set effect
143    pub fn effect<S: Into<Cow<'a, str>>>(mut self, effect: S) -> Self {
144        self.effect = Some(effect.into());
145        self
146    }
147}