S13: ec-ass WrapStyle enum replaces the u8 (only value 2 changes behaviour, as before)
Browse files at this commitf75aa7dd474ef195ae99a2fc0fdad52cfda9449fParent: 0cbcc3a1 changed file +21 -6
- crates/ec-ass/src/lib.rs +21 -6
crates/ec-ass/src/lib.rsmodified+21 -6
| @@ -42,7 +42,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | ||
| 42 | 42 | let mut saw_events = false; |
| 43 | 43 | // `WrapStyle` is read once, when its line is seen; scanning the metadata for |
| 44 | 44 | // every Dialogue line instead made a long script quadratic. |
| 45 | - let mut wrap_style = 0u8; | |
| 45 | + let mut wrap_style = WrapStyle::Auto; | |
| 46 | 46 | let mut wrap_seen = false; |
| 47 | 47 | for line in text.split_inclusive('\n') { |
| 48 | 48 | let trimmed = line.trim(); |
| @@ -70,7 +70,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | ||
| 70 | 70 | let key = snake_case(key); |
| 71 | 71 | // The first `WrapStyle` line counts, as it always did. |
| 72 | 72 | if key == "wrap_style" && !wrap_seen { |
| 73 | - wrap_style = value.parse::<u8>().unwrap_or(0); | |
| 73 | + wrap_style = WrapStyle::parse(value); | |
| 74 | 74 | wrap_seen = true; |
| 75 | 75 | } |
| 76 | 76 | track.metadata.push((key, value.to_owned())); |
| @@ -228,7 +228,7 @@ fn parse_color(value: &str) -> Option<(u8, u8, u8, u8)> { | ||
| 228 | 228 | )) |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | -fn dialogue(format: &[String], row: &str, wrap_style: u8) -> Option<SubtitleCue> { | |
| 231 | +fn dialogue(format: &[String], row: &str, wrap_style: WrapStyle) -> Option<SubtitleCue> { | |
| 232 | 232 | const DEFAULT: &str = "Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text"; |
| 233 | 233 | let fallback = fields(DEFAULT); |
| 234 | 234 | let format = if format.is_empty() { &fallback } else { format }; |
| @@ -276,7 +276,7 @@ struct Overrides { | ||
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | /// One `Text` field into runs, plus whatever positioning its overrides state. |
| 279 | -fn body(text: &str, wrap_style: u8) -> (Vec<Segment>, Option<CuePosition>) { | |
| 279 | +fn body(text: &str, wrap_style: WrapStyle) -> (Vec<Segment>, Option<CuePosition>) { | |
| 280 | 280 | let mut out: Vec<Segment> = Vec::new(); |
| 281 | 281 | let mut state = Overrides::default(); |
| 282 | 282 | let mut position = CuePosition::default(); |
| @@ -445,12 +445,27 @@ fn split_tag(tag: &str) -> Option<(&str, &str)> { | ||
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | /// Plain text between override blocks, with the three escapes ASS gives it. |
| 448 | +#[derive(Clone, Copy, PartialEq, Eq)] | |
| 449 | +enum WrapStyle { | |
| 450 | + Auto, | |
| 451 | + Manual, | |
| 452 | +} | |
| 453 | + | |
| 454 | +impl WrapStyle { | |
| 455 | + fn parse(value: &str) -> WrapStyle { | |
| 456 | + match value.parse::<u8>() { | |
| 457 | + Ok(2) => WrapStyle::Manual, | |
| 458 | + _ => WrapStyle::Auto, | |
| 459 | + } | |
| 460 | + } | |
| 461 | +} | |
| 462 | + | |
| 448 | 463 | fn push_text( |
| 449 | 464 | out: &mut Vec<Segment>, |
| 450 | 465 | run: &mut String, |
| 451 | 466 | text: &str, |
| 452 | 467 | state: &Overrides, |
| 453 | - wrap_style: u8, | |
| 468 | + wrap_style: WrapStyle, | |
| 454 | 469 | ) { |
| 455 | 470 | let mut chars = text.chars().peekable(); |
| 456 | 471 | while let Some(c) = chars.next() { |
| @@ -466,7 +481,7 @@ fn push_text( | ||
| 466 | 481 | flush(out, run, state); |
| 467 | 482 | out.push(Segment::LineBreak); |
| 468 | 483 | } |
| 469 | - Some('n') if wrap_style == 2 => { | |
| 484 | + Some('n') if wrap_style == WrapStyle::Manual => { | |
| 470 | 485 | flush(out, run, state); |
| 471 | 486 | out.push(Segment::LineBreak); |
| 472 | 487 | } |