forge
Explore
Log in

Don't you dare go Hollow

S13: ec-ass WrapStyle enum replaces the u8 (only value 2 changes behaviour, as before)

Browse files at this commit

f75aa7dd474ef195ae99a2fc0fdad52cfda9449fAhmet Kaan GümüşParent: 0cbcc3a
crates/ec-ass/src/lib.rsmodified+21 -6
@@ -42,7 +42,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> {
42 let mut saw_events = false;42 let mut saw_events = false;
43 // `WrapStyle` is read once, when its line is seen; scanning the metadata for43 // `WrapStyle` is read once, when its line is seen; scanning the metadata for
44 // every Dialogue line instead made a long script quadratic.44 // every Dialogue line instead made a long script quadratic.
45- let mut wrap_style = 0u8;45+ let mut wrap_style = WrapStyle::Auto;
46 let mut wrap_seen = false;46 let mut wrap_seen = false;
47 for line in text.split_inclusive('\n') {47 for line in text.split_inclusive('\n') {
48 let trimmed = line.trim();48 let trimmed = line.trim();
@@ -70,7 +70,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> {
70 let key = snake_case(key);70 let key = snake_case(key);
71 // The first `WrapStyle` line counts, as it always did.71 // The first `WrapStyle` line counts, as it always did.
72 if key == "wrap_style" && !wrap_seen {72 if key == "wrap_style" && !wrap_seen {
73- wrap_style = value.parse::<u8>().unwrap_or(0);73+ wrap_style = WrapStyle::parse(value);
74 wrap_seen = true;74 wrap_seen = true;
75 }75 }
76 track.metadata.push((key, value.to_owned()));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 const DEFAULT: &str = "Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text";232 const DEFAULT: &str = "Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text";
233 let fallback = fields(DEFAULT);233 let fallback = fields(DEFAULT);
234 let format = if format.is_empty() { &fallback } else { format };234 let format = if format.is_empty() { &fallback } else { format };
@@ -276,7 +276,7 @@ struct Overrides {
276 }276 }
277 277
278 /// One `Text` field into runs, plus whatever positioning its overrides state.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 let mut out: Vec<Segment> = Vec::new();280 let mut out: Vec<Segment> = Vec::new();
281 let mut state = Overrides::default();281 let mut state = Overrides::default();
282 let mut position = CuePosition::default();282 let mut position = CuePosition::default();
@@ -445,12 +445,27 @@ fn split_tag(tag: &str) -> Option<(&str, &str)> {
445 }445 }
446 446
447 /// Plain text between override blocks, with the three escapes ASS gives it.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 fn push_text(463 fn push_text(
449 out: &mut Vec<Segment>,464 out: &mut Vec<Segment>,
450 run: &mut String,465 run: &mut String,
451 text: &str,466 text: &str,
452 state: &Overrides,467 state: &Overrides,
453- wrap_style: u8,468+ wrap_style: WrapStyle,
454 ) {469 ) {
455 let mut chars = text.chars().peekable();470 let mut chars = text.chars().peekable();
456 while let Some(c) = chars.next() {471 while let Some(c) = chars.next() {
@@ -466,7 +481,7 @@ fn push_text(
466 flush(out, run, state);481 flush(out, run, state);
467 out.push(Segment::LineBreak);482 out.push(Segment::LineBreak);
468 }483 }
469- Some('n') if wrap_style == 2 => {484+ Some('n') if wrap_style == WrapStyle::Manual => {
470 flush(out, run, state);485 flush(out, run, state);
471 out.push(Segment::LineBreak);486 out.push(Segment::LineBreak);
472 }487 }