From 5c9b2ddfda3dd3bee46119d11150cbaefea9218f Mon Sep 17 00:00:00 2001 From: 4o1x5 <4o1x5@4o1x5.dev> Date: Mon, 25 Nov 2024 06:54:24 +0100 Subject: [PATCH] added parsing for scriptinfo --- Cargo.toml | 2 ++ flake.lock | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ass.rs | 33 ++++++++++++++++++----- src/lib.rs | 23 ++++++++-------- 4 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 flake.lock diff --git a/Cargo.toml b/Cargo.toml index e24f76a..253daa0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,8 @@ name = "subtytle" version = "0.1.0" edition = "2021" license = "AGPL-3.0" +keywords = ["subtitle", "srt", "ass"] +description = "A library to work with the .ASS format" authors = ["4o1x5 <4o1x5@4o1x5.dev>"] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..9340ae2 --- /dev/null +++ b/flake.lock @@ -0,0 +1,77 @@ +{ + "nodes": { + "crane": { + "locked": { + "lastModified": 1732407143, + "narHash": "sha256-qJOGDT6PACoX+GbNH2PPx2ievlmtT1NVeTB80EkRLys=", + "owner": "ipetkov", + "repo": "crane", + "rev": "f2b4b472983817021d9ffb60838b2b36b9376b20", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1732238832, + "narHash": "sha256-sQxuJm8rHY20xq6Ah+GwIUkF95tWjGRd1X8xF+Pkk38=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "8edf06bea5bcbee082df1b7369ff973b91618b8d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/src/ass.rs b/src/ass.rs index 79c0532..cf366af 100644 --- a/src/ass.rs +++ b/src/ass.rs @@ -469,7 +469,6 @@ impl FromStr for AssSubtitle { } } CurrentSection::ScriptInfo => { - // Do nothing let parts: Vec = line.split(":").map(|e| e.to_string()).collect(); let key = parts.first().unwrap().clone(); let value = parts.get(1).ok_or(AssConvertError::KeyHasNoValue)?.clone(); @@ -639,13 +638,33 @@ impl FromStr for AssSubtitle { text: dialogue.get("Text").map(|v| v.to_string()).unwrap(), }); } - // TODO: Parse ScriptInfo + let script_info = ScriptInfo { - title: "Subtitle".to_string(), - original_script: "Original Script".to_string(), - script_type: "v4.00+".to_string(), - collisions: None, - play_depth: None, + title: script_fields.get("Title").map(|v| v.to_owned()).ok_or( + AssConvertError::InvalidScriptInfo("Title missing".to_string()), + )?, + original_script: script_fields + .get("Original Script") + .map(|v| v.to_owned()) + .ok_or(AssConvertError::InvalidScriptInfo( + "Original script missing".to_string(), + ))?, + script_type: script_fields + .get("ScriptType") + .map(|v| v.to_owned()) + .ok_or(AssConvertError::InvalidScriptInfo( + "ScriptType missing".to_string(), + ))?, + collisions: script_fields.get("Collisions").map(|v| v.to_owned()), + play_depth: match script_fields.get("Play Depth") { + None => None, + Some(val) => { + let v = val.parse().map_err(|_| { + AssConvertError::InvalidScriptInfo("Play Depth is not a number".to_string()) + })?; + Some(v) + } + }, }; Ok(Self { diff --git a/src/lib.rs b/src/lib.rs index c2d05e0..8d1ffbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,24 +163,25 @@ Dialogue: 0:01:00.00,0:01:10.00,A7,{\pos(600,400)}The quick brown fox jumps over Dialogue: 0:01:10.00,0:01:20.00,A8,{\pos(600,400)}The quick brown fox jumps over a lazy dog.\NSphinx of black quartz, judge my vow. Dialogue: 0:01:20.00,0:01:30.00,A9,{\pos(600,400)}The quick brown fox jumps over a lazy dog.\NSphinx of black quartz, judge my vow. "#; - let ass_decoded = AssSubtitle::from_str(ass).unwrap(); + let ass_decoded = AssSubtitle::from_str(ass); tracing::info!("Less tags: {:?}", ass_decoded); + assert_eq!(ass_decoded.is_err(), true); } #[test(test)] #[traced_test] fn parse_event_from_srt() { let srt = r#"1 - 00:00:00,500 --> 00:00:02,000 - Hello, world! - 2 - 00:00:05,500 --> 00:00:09,000 - asdasd - asd - asd - 2 - 00:00:05,500 --> 00:00:09,000 - something"#; +00:00:00,500 --> 00:00:02,000 +Hello, world! +2 +00:00:05,500 --> 00:00:09,000 +asdasd +asd +asd +2 +00:00:05,500 --> 00:00:09,000 +something"#; let srt = srt.replace(" ", ""); let events = Event::from_srt(srt.as_str()).unwrap(); tracing::info!("Subtitles {:?}", events);