rsubs-lib 0.3.4

Pure Rust subtitle manipulation and conversion library.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Implements helpers for `.ass` and `.ssa`.
//!
//! It describes the [SSAFile], [SSAEvent] and [SSAStyle] structs and
//! provides the [parse] function.

use regex::Regex;
use std::collections::HashMap;
use std::fmt::Display;

use crate::util::Alignment;
use serde::{Deserialize, Serialize};

use crate::error;
use crate::ssa::parse::TIME_FORMAT;
use crate::util::Color;
use crate::vtt::VTT;
use time::Time;

use super::srt::{SRTLine, SRT};
use super::strip_bom;

/// [SSAInfo] contains headers and general information about the script.
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct SSAInfo {
    /// Description of the script.
    pub title: Option<String>,
    /// Original author(s) of the script.
    pub original_script: Option<String>,
    /// Original translator of the dialogue.
    pub original_translation: Option<String>,
    /// Original script editor(s).
    pub original_editing: Option<String>,
    /// Whoever timed the original script
    pub original_timing: Option<String>,
    /// Description of where in the video the script should begin playback.
    pub synch_point: Option<String>,
    /// Names of any other subtitling groups who edited the original script.
    pub script_update_by: Option<String>,
    /// The details of any updates to the original script - made by other subtitling groups
    pub update_details: Option<String>,
    /// The SSA script format version.
    pub script_type: Option<String>,
    /// Determines how subtitles are moved, when automatically preventing onscreen collisions.
    /// Allowed values:
    /// - `Normal`: SSA will attempt to position subtitles in the position specified by the
    ///   "margins". However, subtitles can be shifted vertically to prevent onscreen collisions.
    ///   With "normal" collision prevention, the subtitles will "stack up" one above the other -
    ///   but they will always be positioned as close the vertical (bottom) margin as possible -
    ///   filling in "gaps" in other subtitles if one large enough is available.
    /// - `Reverse`: Subtitles will be shifted upwards to make room for subsequent overlapping
    ///   subtitles. This means the subtitles can nearly always be read top-down - but it also means
    ///   that the first subtitle can appear halfway up the screen before the subsequent overlapping
    ///   subtitles appear. It can use a lot of screen area.
    pub collisions: Option<String>,
    /// The height of the screen used by the script's author(s) when playing the script.
    pub play_res_y: Option<u32>,
    /// The width of the screen used by the script's author(s) when playing the script.
    pub play_res_x: Option<u32>,
    /// The color depth used by the script's author(s) when playing the script.
    pub play_depth: Option<u32>,
    /// The Timer Speed for the script, as percentage. So `100` == `100%`.
    pub timer: Option<f32>,
    /// Defines the default wrapping style.
    /// Allowed values are:
    /// - `0`: smart wrapping, lines are evenly broken
    /// - `1`: end-of-line word wrapping, only \N breaks
    /// - `2`: no word wrapping, \n \N both breaks
    /// - `3`: same as 0, but lower line gets wider
    pub wrap_style: Option<u8>,

    /// Additional fields that aren't covered by the ASS spec.
    pub additional_fields: HashMap<String, String>,
}
impl Eq for SSAInfo {}

/// [SSAStyle] describes each part of the `Format: ` side of a `.ssa` or `.ass` subtitle.
///
/// Currently only supports `.ass`, more precisely `ScriptType: V4.00+` and `[V4+ Styles]`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SSAStyle {
    /// Name of the style. Case-sensitive. Cannot include commas.
    pub name: String,
    /// Fontname as used by Windows. Case-sensitive.
    pub fontname: String,
    /// Fontsize.
    pub fontsize: f32,
    /// The color that a subtitle will normally appear in.
    pub primary_color: Option<Color>,
    /// This color may be used instead of the Primary colour when a subtitle is automatically
    /// shifted to prevent an onscreen collision, to distinguish the different subtitles.
    pub secondary_color: Option<Color>,
    /// This color may be used instead of the Primary or Secondary colour when a subtitle is
    /// automatically shifted to prevent an onscreen collision, to distinguish the different
    /// subtitles.
    pub outline_color: Option<Color>,
    /// The color of the subtitle outline or shadow.
    pub back_color: Option<Color>,
    /// Defines whether text is bold or not.
    pub bold: bool,
    /// Defines whether text is italic or not.
    pub italic: bool,
    /// Defines whether text is underlined or not.
    pub underline: bool,
    /// Defines whether text is strikeout or not.
    pub strikeout: bool,
    /// Modifies the width of the font. Value is percentage.
    pub scale_x: f32,
    /// Modifies the height of the font. Value is percentage.
    pub scale_y: f32,
    /// Extra space between characters (in pixels).
    pub spacing: f32,
    /// Origin of the rotation is defined by the alignment (as degrees).
    pub angle: f32,
    /// Border style.
    /// Allowed values are:
    /// - `1`: Outline + drop shadow
    /// - `3`: Opaque box
    pub border_style: u8,
    /// If [SSAStyle::border_style] is `1`, then this specifies the width of the outline around the
    /// text (in pixels).
    /// Values may be `0`, `1`, `2`, `3` or `4`.
    pub outline: f32,
    /// If [SSAStyle::border_style] is `1`, then this specifies the depth of the drop shadow behind
    /// the text (in pixels). Values may be `0`, `1`, `2`, `3` or `4`. Drop shadow is always used in
    /// addition to an outline - SSA will force an outline of 1 pixel if no outline width is given.
    pub shadow: f32,
    /// Sets how text is "justified" within the Left/Right onscreen margins, and also the vertical
    /// placing.
    pub alignment: Alignment,
    /// Defines the Left Margin in pixels.
    pub margin_l: f32,
    /// Defines the Right Margin in pixels.
    pub margin_r: f32,
    /// Defines the Vertical Left Margin in pixels.
    pub margin_v: f32,
    /// Specifies the font character set or encoding and on multilingual Windows installations it
    /// provides access to characters used in multiple than one language. It is usually 0 (zero)
    /// for English (Western, ANSI) Windows.
    pub encoding: f32,
}
impl Eq for SSAStyle {}

impl Default for SSAStyle {
    fn default() -> Self {
        SSAStyle {
            name: "Default".to_string(),
            fontname: "Trebuchet MS".to_string(),
            fontsize: 25.5,
            primary_color: None,
            secondary_color: None,
            outline_color: None,
            back_color: None,
            bold: false,
            italic: false,
            underline: false,
            strikeout: false,
            scale_x: 120.0,
            scale_y: 120.0,
            spacing: 0.0,
            angle: 0.0,
            border_style: 1,
            outline: 1.0,
            shadow: 1.0,
            alignment: Alignment::BottomCenter,
            margin_l: 0.0,
            margin_r: 0.0,
            margin_v: 20.0,
            encoding: 0.0,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum SSAEventLineType {
    Dialogue,
    Comment,
    Other(String),
}

/// Describes each individual element of an `Event` line in the `.ass` format
///
/// Each element can be individually changed.
///
/// Because of its comma separated values in the event line, the timestamp looks like
/// `00:00:20.00` and it can be represented using [Time::to_ass_string]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SSAEvent {
    /// Subtitles having different layer number will be ignored during the collusion detection.
    /// Higher numbered layers will be drawn over the lower numbered.
    pub layer: u32,
    /// Start time of the line being displayed.
    pub start: Time,
    /// End time of the line being displayed
    pub end: Time,
    /// String value relating to an [SSAStyle].
    pub style: String,
    /// Generally this is used for "speaker name", in most cases it's an unused field.
    pub name: String,
    /// SSA/ASS documentation describes the l/r/v margins as being floats so...here goes
    /// In practice it gets represented as `0020` and similar `{:0>4}` patterns.
    pub margin_l: f32,
    /// SSA/ASS documentation describes the l/r/v margins as being floats so...here goes
    /// In practice it gets represented as `0020` and similar `{:0>4}` patterns.
    pub margin_r: f32,
    /// SSA/ASS documentation describes the l/r/v margins as being floats so...here goes
    /// In practice it gets represented as `0020` and similar `{:0>4}` patterns.
    pub margin_v: f32,
    /// SSA Documentation describes it, it's here, no idea what it does, but you can write it if you
    /// wish.
    pub effect: String,
    /// The line's text.
    pub text: String,
    pub line_type: SSAEventLineType,
}
impl Eq for SSAEvent {}

impl Default for SSAEvent {
    fn default() -> Self {
        SSAEvent {
            layer: 0,
            start: Time::from_hms(0, 0, 0).unwrap(),
            end: Time::from_hms(0, 0, 0).unwrap(),
            style: "Default".to_string(),
            name: "".to_string(),
            margin_l: 0.0,
            margin_r: 0.0,
            margin_v: 0.0,
            effect: "".to_string(),
            text: "".to_string(),
            line_type: SSAEventLineType::Dialogue,
        }
    }
}
/// Contains the styles, events and info as well as a format mentioning whether it's `.ass` or `.ssa`
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct SSA {
    pub info: SSAInfo,
    pub styles: Vec<SSAStyle>,
    pub events: Vec<SSAEvent>,
    pub fonts: Vec<String>,
    pub graphics: Vec<String>,
}

impl SSA {
    /// Parses the given [String] into [SSA].
    pub fn parse<S: AsRef<str>>(content: S) -> Result<SSA, SSAError> {
        let mut blocks = Vec::new();
        for (i, line) in (1..).zip(strip_bom(&content).lines()) {
            match line.trim() {
                l if l.is_empty() || l.starts_with(&[';', '#']) => continue,
                l if l.starts_with('[') => blocks.push(vec![(i, line)]),
                _ => {
                    if let Some(b) = blocks.last_mut() {
                        b.push((i, line))
                    }
                }
            }
        }

        if !blocks
            .first()
            .map(|b| &b[0])
            .is_some_and(|l| l.1 == "[Script Info]")
        {
            return Err(SSAError::new(SSAErrorKind::Invalid, 1));
        }

        let mut ssa = SSA::default();

        for block in blocks {
            let mut iter = block.into_iter();
            let (i, line) = iter.next().unwrap(); // safe unwrap: each block is guaranteed non-empty
            match line {
                "[Script Info]" => ssa.info = parse::parse_script_info_block(iter)?,
                "[V4+ Styles]" => ssa.styles = parse::parse_style_block(i, iter)?,
                "[Events]" => ssa.events = parse::parse_events_block(i, iter)?,
                "[Fonts]" => ssa.fonts = parse::parse_fonts_block(iter)?,
                "[Graphics]" => ssa.graphics = parse::parse_graphics_block(iter)?,
                _ => continue,
            }
        }

        Ok(ssa)
    }

    /// Converts the SSAFile to a SRTFile. Due to `.srt` being a far less complex
    /// format, most styles are being ignored.
    ///
    /// Styling of the text can happen with `{i1}aaa{i0}` tags where `i` represents
    ///  the style and `0`/`1` represent the on/off triggers.
    ///
    /// `.srt` supports HTML-like tags for `i`,`b`,`u`, representing italic, bold, underline.
    ///
    /// If found, ssa specific triggers for those supported tags are replaced with their `.srt` alternatives.
    ///
    pub fn to_srt(&self) -> SRT {
        let style_remove_regex = Regex::new(r"(?m)\{\\.+?}").unwrap();

        let mut lines = vec![];

        for (i, event) in self.events.iter().enumerate() {
            let mut text = event
                .text
                .replace("{\\b1}", "<b>")
                .replace("{\\b0}", "</b>")
                .replace("{\\i1}", "<i>")
                .replace("{\\i0}", "</i>")
                .replace("{\\u1}", "<u>")
                .replace("{\\u0}", "</u>")
                .replace("\\N", "\r\n");

            if !event.style.is_empty() {
                if let Some(style) = self.styles.iter().find(|s| s.name == event.style) {
                    if style.bold {
                        text = format!("<b>{text}</b>")
                    }
                    if style.italic {
                        text = format!("<i>{text}</i>")
                    }
                    if style.underline {
                        text = format!("<u>{text}</u>")
                    }
                }
            }

            lines.push(SRTLine {
                sequence_number: i as u32 + 1,
                start: event.start,
                end: event.end,
                text: style_remove_regex.replace_all(&text, "").to_string(),
            })
        }

        SRT { lines }
    }
    /// Converts the SSAFile to a VTTFile.
    ///
    /// Styling of the text can happen with `{i1}aaa{i0}` tags where `i` represents
    ///  the style and `0`/`1` represent the on/off triggers.
    ///
    /// `.vtt` supports HTML-like tags for `i`,`b`,`u`, representing italic, bold, underline.
    ///
    /// If found, ssa specific triggers for those supported tags are replaced with their `.vtt` alternatives.
    ///
    /// In addition, if an SSAEvent has a related SSAStyle, the SSAStyle is converted to a VTTStyle that will be wrapped around the lines indicating it.
    pub fn to_vtt(self) -> VTT {
        self.to_srt().to_vtt()
    }
}

impl Display for SSA {
    #[rustfmt::skip]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut lines = vec![];

        lines.push("[Script Info]".to_string());
        lines.extend(self.info.title.as_ref().map(|l| format!("Title: {l}")));
        lines.extend(self.info.original_script.as_ref().map(|l| format!("Original Script: {l}")));
        lines.extend(self.info.original_translation.as_ref().map(|l| format!("Original Translation: {l}")));
        lines.extend(self.info.original_editing.as_ref().map(|l| format!("Original Editing: {l}")));
        lines.extend(self.info.original_timing.as_ref().map(|l| format!("Original Timing: {l}")));
        lines.extend(self.info.synch_point.as_ref().map(|l| format!("Synch Point: {l}")));
        lines.extend(self.info.script_update_by.as_ref().map(|l| format!("Script Updated By: {l}")));
        lines.extend(self.info.update_details.as_ref().map(|l| format!("Update Details: {l}")));
        lines.extend(self.info.script_type.as_ref().map(|l| format!("Script Type: {l}")));
        lines.extend(self.info.collisions.as_ref().map(|l| format!("Collisions: {l}")));
        lines.extend(self.info.play_res_y.map(|l| format!("PlayResY: {l}")));
        lines.extend(self.info.play_res_x.map(|l| format!("PlayResX: {l}")));
        lines.extend(self.info.play_depth.map(|l| format!("PlayDepth: {l}")));
        lines.extend(self.info.timer.map(|l| format!("Timer: {l}")));
        lines.extend(self.info.wrap_style.map(|l| format!("WrapStyle: {l}")));
        for (k, v) in &self.info.additional_fields {
            lines.push(format!("{k}: {v}"))
        }

        lines.push("".to_string());
        lines.push("[V4+ Styles]".to_string());
        lines.push("Format: Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,OutlineColour,BackColour,Bold,Italic,Underline,StrikeOut,ScaleX,ScaleY,Spacing,Angle,BorderStyle,Outline,Shadow,Alignment,MarginL,MarginR,MarginV,Encoding".to_string());
        for style in &self.styles {
            let line = [
                style.name.to_string(),
                style.fontname.to_string(),
                style.fontsize.to_string(),
                style.primary_color.map(|c| c.to_ssa_string()).unwrap_or_default(),
                style.secondary_color.map(|c| c.to_ssa_string()).unwrap_or_default(),
                style.outline_color.map(|c| c.to_ssa_string()).unwrap_or_default(),
                style.back_color.map(|c| c.to_ssa_string()).unwrap_or_default(),
                if style.bold { "-1" } else { "0" }.to_string(),
                if style.italic { "-1" } else { "0" }.to_string(),
                if style.underline { "-1" } else { "0" }.to_string(),
                if style.strikeout { "-1" } else { "0" }.to_string(),
                style.scale_x.to_string(),
                style.scale_y.to_string(),
                style.spacing.to_string(),
                style.angle.to_string(),
                style.border_style.to_string(),
                style.outline.to_string(),
                style.shadow.to_string(),
                (style.alignment as u8).to_string(),
                style.margin_l.to_string(),
                style.margin_r.to_string(),
                style.margin_v.to_string(),
                style.encoding.to_string(),
            ];
            lines.push(format!("Style: {}", line.join(",")))
        }

        lines.push("".to_string());
        lines.push("[Events]".to_string());
        lines.push("Format: Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text".to_string());
        for event in &self.events {
            let line = [
                event.layer.to_string(),
                event.start.format(TIME_FORMAT).unwrap(),
                event.end.format(TIME_FORMAT).unwrap(),
                event.style.to_string(),
                event.name.to_string(),
                event.margin_l.to_string(),
                event.margin_r.to_string(),
                event.margin_v.to_string(),
                event.effect.to_string(),
                event.text.to_string()
            ];
            lines.push(format!("Dialogue: {}", line.join(",")))
        }

        write!(f, "{}", lines.join("\n"))
    }
}

error! {
    SSAError => SSAErrorKind {
        Invalid,
        EmptyBlock,
        Parse(String),
        MissingHeader(String),
    }
}

mod parse {
    use super::*;
    use std::num::{ParseFloatError, ParseIntError};
    use time::format_description::BorrowedFormatItem;
    use time::macros::format_description;

    pub(super) struct Error {
        pub(super) line: usize,
        pub(super) kind: SSAErrorKind,
    }

    impl From<Error> for SSAError {
        fn from(e: Error) -> SSAError {
            SSAError::new(e.kind, e.line)
        }
    }

    pub(super) const TIME_FORMAT: &[BorrowedFormatItem] =
        format_description!("[hour padding:none]:[minute]:[second].[subsecond digits:2]");

    type Result<T> = std::result::Result<T, Error>;

    pub(super) fn parse_script_info_block<'a, I: Iterator<Item = (usize, &'a str)>>(
        block_lines: I,
    ) -> Result<SSAInfo> {
        let mut info = SSAInfo::default();

        for (i, line) in block_lines {
            let Some((name, mut value)) = line.split_once(':') else {
                return Err(Error {
                    line: i,
                    kind: SSAErrorKind::Parse("delimiter ':' missing".to_string()),
                });
            };
            value = value.trim();

            if value.is_empty() {
                continue;
            }

            match name {
                "Title" => info.title = Some(value.to_string()),
                "Original Script" => info.original_script = Some(value.to_string()),
                "Original Translation" => info.original_translation = Some(value.to_string()),
                "Original Editing" => info.original_editing = Some(value.to_string()),
                "Original Timing" => info.original_timing = Some(value.to_string()),
                "Synch Point" => info.synch_point = Some(value.to_string()),
                "Script Updated By" => info.script_update_by = Some(value.to_string()),
                "Update Details" => info.update_details = Some(value.to_string()),
                "ScriptType" => info.script_type = Some(value.to_string()),
                "Collisions" => info.collisions = Some(value.to_string()),
                "PlayResY" => {
                    info.play_res_y = value.parse::<u32>().map(Some).map_err(|e| Error {
                        line: i,
                        kind: SSAErrorKind::Parse(e.to_string()),
                    })?
                }
                "PlayResX" => {
                    info.play_res_x = value.parse::<u32>().map(Some).map_err(|e| Error {
                        line: i,
                        kind: SSAErrorKind::Parse(e.to_string()),
                    })?
                }
                "PlayDepth" => {
                    info.play_depth = value.parse::<u32>().map(Some).map_err(|e| Error {
                        line: i,
                        kind: SSAErrorKind::Parse(e.to_string()),
                    })?
                }
                "Timer" => {
                    info.timer = value.parse::<f32>().map(Some).map_err(|e| Error {
                        line: i,
                        kind: SSAErrorKind::Parse(e.to_string()),
                    })?
                }
                "WrapStyle" => {
                    info.wrap_style = value.parse::<u8>().map(Some).map_err(|e| Error {
                        line: i,
                        kind: SSAErrorKind::Parse(e.to_string()),
                    })?
                }
                _ => {
                    info.additional_fields
                        .insert(name.to_string(), value.to_string());
                }
            }
        }

        Ok(info)
    }

    fn parse_block_header<'a, I: Iterator<Item = (usize, &'a str)>>(
        header_line: usize,
        mut block_lines: I,
    ) -> Result<(usize, Vec<&'a str>)> {
        let (i, line) = block_lines.next().ok_or_else(|| Error {
            line: header_line,
            kind: SSAErrorKind::EmptyBlock,
        })?;

        let header = line.strip_prefix("Format:").ok_or_else(|| Error {
            line: i,
            kind: SSAErrorKind::Parse("header must start with 'Format:'".to_string()),
        })?;

        Ok((i, header.trim().split(',').collect()))
    }

    pub(super) fn parse_style_block<'a, I: Iterator<Item = (usize, &'a str)>>(
        header_line: usize,
        mut block_lines: I,
    ) -> Result<Vec<SSAStyle>> {
        let (header_line, headers) = parse_block_header(header_line, &mut block_lines)?;

        let mut styles = vec![];

        for (i, line) in block_lines {
            let Some(line) = line.strip_prefix("Style:") else {
                return Err(Error {
                    line: i,
                    kind: SSAErrorKind::Parse("styles line must start with 'Style:'".to_string()),
                });
            };
            let line_list: Vec<&str> = line.trim().split(',').collect();

            styles.push(SSAStyle {
                name: get_line_value(&headers, "Name", &line_list, header_line, i)?.to_string(),
                fontname: get_line_value(&headers, "Fontname", &line_list, header_line, i)?
                    .to_string(),
                fontsize: get_line_value(&headers, "Fontsize", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                primary_color: Color::from_ssa(get_line_value(
                    &headers,
                    "PrimaryColour",
                    &line_list,
                    header_line,
                    i,
                )?)
                .map_err(|e| Error {
                    line: i,
                    kind: SSAErrorKind::Parse(e.to_string()),
                })?,
                secondary_color: Color::from_ssa(get_line_value(
                    &headers,
                    "SecondaryColour",
                    &line_list,
                    header_line,
                    i,
                )?)
                .map_err(|e| Error {
                    line: i,
                    kind: SSAErrorKind::Parse(e.to_string()),
                })?,
                outline_color: Color::from_ssa(get_line_value(
                    &headers,
                    "OutlineColour",
                    &line_list,
                    header_line,
                    i,
                )?)
                .map_err(|e| Error {
                    line: i,
                    kind: SSAErrorKind::Parse(e.to_string()),
                })?,
                back_color: Color::from_ssa(get_line_value(
                    &headers,
                    "BackColour",
                    &line_list,
                    header_line,
                    i,
                )?)
                .map_err(|e| Error {
                    line: i,
                    kind: SSAErrorKind::Parse(e.to_string()),
                })?,
                bold: parse_str_to_bool(
                    get_line_value(&headers, "Bold", &line_list, header_line, i)?,
                    i,
                )?,
                italic: parse_str_to_bool(
                    get_line_value(&headers, "Italic", &line_list, header_line, i)?,
                    i,
                )?,
                underline: parse_str_to_bool(
                    get_line_value(&headers, "Underline", &line_list, header_line, i)?,
                    i,
                )?,
                strikeout: parse_str_to_bool(
                    get_line_value(&headers, "StrikeOut", &line_list, header_line, i)?,
                    i,
                )?,
                scale_x: get_line_value(&headers, "ScaleX", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                scale_y: get_line_value(&headers, "ScaleY", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                spacing: get_line_value(&headers, "Spacing", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                angle: get_line_value(&headers, "Angle", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                border_style: get_line_value(&headers, "BorderStyle", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_int_err(e, i))?,
                outline: get_line_value(&headers, "Outline", &line_list, header_line, i)?
                    .parse()
                    .map(|op: f32| f32::from(op))
                    .map_err(|e| map_parse_float_err(e, i))?,
                shadow: get_line_value(&headers, "Shadow", &line_list, header_line, i)?
                    .parse()
                    .map(|op: f32| f32::from(op))
                    .map_err(|e| map_parse_float_err(e, i))?,
                alignment: Alignment::infer_from_str(get_line_value(
                    &headers,
                    "Alignment",
                    &line_list,
                    header_line,
                    i,
                )?)
                .unwrap(),
                margin_l: get_line_value(&headers, "MarginL", &line_list, header_line, i)?
                    .parse()
                    .map(|op: f32| f32::from(op))
                    .map_err(|e| map_parse_float_err(e, i))?,
                margin_r: get_line_value(&headers, "MarginR", &line_list, header_line, i)?
                    .parse()
                    .map(|op: f32| f32::from(op))
                    .map_err(|e| map_parse_float_err(e, i))?,
                margin_v: get_line_value(&headers, "MarginV", &line_list, header_line, i)?
                    .parse()
                    .map(|op: f32| f32::from(op))
                    .map_err(|e| map_parse_float_err(e, i))?,
                encoding: get_line_value(&headers, "Encoding", &line_list, header_line, i)?
                    .parse()
                    .map(|op: f32| f32::from(op))
                    .map_err(|e| map_parse_float_err(e, i))?,
            })
        }

        Ok(styles)
    }

    pub(super) fn parse_events_block<'a, I: Iterator<Item = (usize, &'a str)>>(
        header_line: usize,
        mut block_lines: I,
    ) -> Result<Vec<SSAEvent>> {
        let (header_line, headers) = parse_block_header(header_line, &mut block_lines)?;

        let mut events = vec![];

        for (i, line) in block_lines {
            let Some((line_type, line)) = line.split_once(':') else {
                return Err(Error {
                    line: i,
                    kind: SSAErrorKind::Parse("delimiter ':' missing".to_string()),
                });
            };
            let line_list: Vec<&str> = line.trim().splitn(10, ',').collect();

            events.push(SSAEvent {
                layer: get_line_value(&headers, "Layer", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_int_err(e, i))?,
                start: Time::parse(
                    get_line_value(&headers, "Start", &line_list, header_line, i)?,
                    TIME_FORMAT,
                )
                .map_err(|e| Error {
                    line: i,
                    kind: SSAErrorKind::Parse(e.to_string()),
                })?,
                end: Time::parse(
                    get_line_value(&headers, "End", &line_list, header_line, i)?,
                    TIME_FORMAT,
                )
                .map_err(|e| Error {
                    line: i,
                    kind: SSAErrorKind::Parse(e.to_string()),
                })?,
                style: get_line_value(&headers, "Style", &line_list, header_line, i)?.to_string(),
                name: get_line_value(&headers, "Name", &line_list, header_line, i)?.to_string(),
                margin_l: get_line_value(&headers, "MarginL", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                margin_r: get_line_value(&headers, "MarginR", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                margin_v: get_line_value(&headers, "MarginV", &line_list, header_line, i)?
                    .parse()
                    .map_err(|e| map_parse_float_err(e, i))?,
                effect: get_line_value(&headers, "Effect", &line_list, header_line, i)?.to_string(),
                text: get_line_value(&headers, "Text", &line_list, header_line, i)?.to_string(),
                line_type: match line_type {
                    "Dialogue" => SSAEventLineType::Dialogue,
                    "Comment" => SSAEventLineType::Comment,
                    _ => SSAEventLineType::Other(line_type.to_string()),
                },
            })
        }

        Ok(events)
    }

    pub(super) fn parse_fonts_block<'a, I: Iterator<Item = (usize, &'a str)>>(
        block_lines: I,
    ) -> Result<Vec<String>> {
        let mut fonts = vec![];

        for (i, line) in block_lines {
            let Some(line) = line.strip_prefix("fontname:") else {
                return Err(Error {
                    line: i,
                    kind: SSAErrorKind::Parse("fonts line must start with 'fontname:'".to_string()),
                });
            };
            fonts.push(line.trim().to_string())
        }

        Ok(fonts)
    }

    pub(super) fn parse_graphics_block<'a, I: Iterator<Item = (usize, &'a str)>>(
        block_lines: I,
    ) -> Result<Vec<String>> {
        let mut graphics = vec![];

        for (i, line) in block_lines {
            let Some(line) = line.strip_prefix("filename:") else {
                return Err(Error {
                    line: i,
                    kind: SSAErrorKind::Parse(
                        "graphics line must start with 'filename:'".to_string(),
                    ),
                });
            };
            graphics.push(line.trim().to_string())
        }

        Ok(graphics)
    }

    #[allow(clippy::ptr_arg)]
    fn get_line_value<'a>(
        headers: &Vec<&str>,
        name: &str,
        list: &'a Vec<&str>,
        header_line: usize,
        current_line: usize,
    ) -> Result<&'a str> {
        let pos = headers
            .iter()
            .position(|h| {
                let value: &str = h.trim();

                value.to_lowercase() == name.to_lowercase()
            })
            .ok_or(Error {
                line: header_line,
                kind: SSAErrorKind::MissingHeader(name.to_string()),
            })?;

        list.get(pos).map(|l| l.trim()).ok_or(Error {
            line: current_line,
            kind: SSAErrorKind::Parse(format!("no value for header '{}'", name)),
        })
    }
    fn parse_str_to_bool(s: &str, line: usize) -> Result<bool> {
        match s {
            "0" => Ok(false),
            "-1" => Ok(true),
            _ => Err(Error {
                line,
                kind: SSAErrorKind::Parse(
                    "boolean value must be '-1 (true) or '0' (false)".to_string(),
                ),
            }),
        }
    }
    fn map_parse_int_err(e: ParseIntError, line: usize) -> Error {
        Error {
            line,
            kind: SSAErrorKind::Parse(e.to_string()),
        }
    }
    fn map_parse_float_err(e: ParseFloatError, line: usize) -> Error {
        Error {
            line,
            kind: SSAErrorKind::Parse(e.to_string()),
        }
    }
}