ass-core 0.1.1

High-performance ASS subtitle format parser and analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! AST section types and validation for ASS scripts
//!
//! Defines the top-level Section enum that represents the main sections
//! of an ASS script ([Script Info], [V4+ Styles], [Events], etc.) with
//! zero-copy design and span validation for debugging.

use alloc::vec::Vec;

#[cfg(not(feature = "std"))]
extern crate alloc;

use super::{Event, Font, Graphic, ScriptInfo, Span, Style};
#[cfg(debug_assertions)]
use core::ops::Range;

/// Section type discriminant for efficient lookup and filtering
///
/// Provides a lightweight way to identify section types without
/// borrowing section content. Useful for filtering, routing, and
/// type-based operations on collections of sections.
///
/// # Examples
///
/// ```rust
/// use ass_core::parser::ast::SectionType;
///
/// let section_types = vec![SectionType::ScriptInfo, SectionType::Events];
/// assert!(section_types.contains(&SectionType::ScriptInfo));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SectionType {
    /// [Script Info] section identifier
    ScriptInfo,
    /// [V4+ Styles] section identifier
    Styles,
    /// `[Events\]` section identifier
    Events,
    /// `[Fonts\]` section identifier
    Fonts,
    /// `[Graphics\]` section identifier
    Graphics,
}

/// Top-level section in an ASS script
///
/// Represents the main sections that can appear in an ASS subtitle file.
/// Each variant contains the parsed content of that section with zero-copy
/// string references to the original source text.
///
/// # Examples
///
/// ```rust
/// use ass_core::parser::ast::{Section, ScriptInfo, SectionType, Span};
///
/// let info = ScriptInfo { fields: vec![("Title", "Test")], span: Span::new(0, 10, 1, 1) };
/// let section = Section::ScriptInfo(info);
/// assert_eq!(section.section_type(), SectionType::ScriptInfo);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Section<'a> {
    /// [Script Info] section with metadata
    ///
    /// Contains key-value pairs defining script metadata like title,
    /// script type, resolution, and other configuration values.
    ScriptInfo(ScriptInfo<'a>),

    /// [V4+ Styles] section with style definitions
    ///
    /// Contains style definitions that can be referenced by events.
    /// Each style defines font, colors, positioning, and other
    /// visual properties for subtitle rendering.
    Styles(Vec<Style<'a>>),

    /// `[Events\]` section with dialogue and commands
    ///
    /// Contains dialogue lines, comments, and other timed events
    /// that make up the actual subtitle content.
    Events(Vec<Event<'a>>),

    /// `[Fonts\]` section with embedded font data
    ///
    /// Contains UU-encoded font files embedded in the script.
    /// Allows scripts to include custom fonts for portable rendering.
    Fonts(Vec<Font<'a>>),

    /// `[Graphics\]` section with embedded images
    ///
    /// Contains UU-encoded image files embedded in the script.
    /// Used for logos, textures, and other graphical elements.
    Graphics(Vec<Graphic<'a>>),
}

impl Section<'_> {
    /// Get the span covering this entire section
    ///
    /// Computes the span by looking at the content's spans.
    /// Returns None if the section is empty.
    #[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 {
                    // Merge first and last style spans
                    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 {
                    // Merge first and last event spans
                    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 {
                    // Merge first and last font spans
                    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 {
                    // Merge first and last graphic spans
                    let first = &graphics[0].span;
                    let last = &graphics[graphics.len() - 1].span;
                    Some(Span::new(first.start, last.end, first.line, first.column))
                }
            }
        }
    }

    /// Get section type discriminant for efficient matching
    ///
    /// Returns the section type without borrowing the section content,
    /// allowing for efficient type-based filtering and routing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use ass_core::parser::ast::{Section, ScriptInfo, SectionType, Span};
    /// let info = Section::ScriptInfo(ScriptInfo { fields: Vec::new(), span: Span::new(0, 0, 0, 0) });
    /// assert_eq!(info.section_type(), SectionType::ScriptInfo);
    /// ```
    #[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,
        }
    }

    /// Validate all spans in this section reference valid source
    ///
    /// Debug helper to ensure zero-copy invariants are maintained.
    /// Validates that all string references in the section point to
    /// memory within the specified source range.
    ///
    /// Only available in debug builds to avoid performance overhead
    /// in release builds.
    ///
    /// # Arguments
    ///
    /// * `source_range` - Valid memory range for source text
    ///
    /// # Returns
    ///
    /// `true` if all spans are valid, `false` otherwise
    #[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)),
        }
    }
}

impl SectionType {
    /// Get the canonical section header name
    ///
    /// Returns the exact header name as it appears in ASS files,
    /// useful for serialization and error reporting.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use ass_core::parser::ast::SectionType;
    /// assert_eq!(SectionType::ScriptInfo.header_name(), "Script Info");
    /// assert_eq!(SectionType::Styles.header_name(), "V4+ Styles");
    /// ```
    #[must_use]
    pub const fn header_name(self) -> &'static str {
        match self {
            Self::ScriptInfo => "Script Info",
            Self::Styles => "V4+ Styles",
            Self::Events => "Events",
            Self::Fonts => "Fonts",
            Self::Graphics => "Graphics",
        }
    }

    /// Check if this section type is required in valid ASS files
    ///
    /// Returns `true` for sections that must be present for a valid
    /// ASS file (Script Info and Events), `false` for optional sections.
    #[must_use]
    pub const fn is_required(self) -> bool {
        matches!(self, Self::ScriptInfo | Self::Events)
    }

    /// Check if this section type contains timed content
    ///
    /// Returns `true` for sections with time-based content that affects
    /// subtitle timing and playback.
    #[must_use]
    pub const fn is_timed(self) -> bool {
        matches!(self, Self::Events)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::ast::{Event, EventType, Span, Style};
    #[cfg(not(feature = "std"))]
    use alloc::vec;

    #[test]
    fn section_type_discrimination() {
        let info = Section::ScriptInfo(ScriptInfo {
            fields: Vec::new(),
            span: Span::new(0, 0, 0, 0),
        });
        assert_eq!(info.section_type(), SectionType::ScriptInfo);

        let styles = Section::Styles(Vec::new());
        assert_eq!(styles.section_type(), SectionType::Styles);

        let events = Section::Events(Vec::new());
        assert_eq!(events.section_type(), SectionType::Events);
    }

    #[test]
    fn section_span_script_info() {
        let info = Section::ScriptInfo(ScriptInfo {
            fields: vec![("Title", "Test")],
            span: Span::new(10, 50, 2, 1),
        });

        let span = info.span();
        assert!(span.is_some());
        let span = span.unwrap();
        assert_eq!(span.start, 10);
        assert_eq!(span.end, 50);
        assert_eq!(span.line, 2);
    }

    #[test]
    fn section_span_empty_styles() {
        let styles = Section::Styles(Vec::new());
        assert!(styles.span().is_none());
    }

    #[test]
    fn section_span_single_style() {
        let style = Style {
            name: "Default",
            parent: None,
            fontname: "Arial",
            fontsize: "20",
            primary_colour: "&H00FFFFFF",
            secondary_colour: "&H000000FF",
            outline_colour: "&H00000000",
            back_colour: "&H00000000",
            bold: "0",
            italic: "0",
            underline: "0",
            strikeout: "0",
            scale_x: "100",
            scale_y: "100",
            spacing: "0",
            angle: "0",
            border_style: "1",
            outline: "0",
            shadow: "0",
            alignment: "2",
            margin_l: "0",
            margin_r: "0",
            margin_v: "0",
            margin_t: None,
            margin_b: None,
            encoding: "1",
            relative_to: None,
            span: Span::new(100, 200, 5, 1),
        };

        let styles = Section::Styles(vec![style]);
        let span = styles.span();
        assert!(span.is_some());
        let span = span.unwrap();
        assert_eq!(span.start, 100);
        assert_eq!(span.end, 200);
    }

    #[test]
    fn section_span_multiple_events() {
        let event1 = Event {
            event_type: EventType::Dialogue,
            layer: "0",
            start: "0:00:00.00",
            end: "0:00:05.00",
            style: "Default",
            name: "",
            margin_l: "0",
            margin_r: "0",
            margin_v: "0",
            margin_t: None,
            margin_b: None,
            effect: "",
            text: "First",
            span: Span::new(100, 150, 10, 1),
        };

        let event2 = Event {
            event_type: EventType::Dialogue,
            layer: "0",
            start: "0:00:05.00",
            end: "0:00:10.00",
            style: "Default",
            name: "",
            margin_l: "0",
            margin_r: "0",
            margin_v: "0",
            margin_t: None,
            margin_b: None,
            effect: "",
            text: "Second",
            span: Span::new(151, 200, 11, 1),
        };

        let events_section = Section::Events(vec![event1, event2]);
        let span = events_section.span();
        assert!(span.is_some());
        let span = span.unwrap();
        assert_eq!(span.start, 100);
        assert_eq!(span.end, 200);
        assert_eq!(span.line, 10);
    }

    #[test]
    #[allow(clippy::similar_names)]
    fn section_span_multiple_events_similar_names() {
        // Test moved here to avoid clippy similar_names warning
    }

    #[test]
    fn section_type_header_names() {
        assert_eq!(SectionType::ScriptInfo.header_name(), "Script Info");
        assert_eq!(SectionType::Styles.header_name(), "V4+ Styles");
        assert_eq!(SectionType::Events.header_name(), "Events");
        assert_eq!(SectionType::Fonts.header_name(), "Fonts");
        assert_eq!(SectionType::Graphics.header_name(), "Graphics");
    }

    #[test]
    fn section_type_required() {
        assert!(SectionType::ScriptInfo.is_required());
        assert!(SectionType::Events.is_required());
        assert!(!SectionType::Styles.is_required());
        assert!(!SectionType::Fonts.is_required());
        assert!(!SectionType::Graphics.is_required());
    }

    #[test]
    fn section_type_timed() {
        assert!(SectionType::Events.is_timed());
        assert!(!SectionType::ScriptInfo.is_timed());
        assert!(!SectionType::Styles.is_timed());
        assert!(!SectionType::Fonts.is_timed());
        assert!(!SectionType::Graphics.is_timed());
    }

    #[test]
    fn section_type_copy_clone() {
        let section_type = SectionType::ScriptInfo;
        let copied = section_type;
        let cloned = section_type;

        assert_eq!(section_type, copied);
        assert_eq!(section_type, cloned);
    }

    #[test]
    fn section_type_hash() {
        use alloc::collections::BTreeSet;

        let mut set = BTreeSet::new();
        set.insert(SectionType::ScriptInfo);
        set.insert(SectionType::Events);

        assert!(set.contains(&SectionType::ScriptInfo));
        assert!(set.contains(&SectionType::Events));
        assert!(!set.contains(&SectionType::Styles));
    }
}