added parsing for scriptinfo

This commit is contained in:
Barna Máté 2024-11-25 06:54:24 +01:00
parent e6500e0116
commit 5c9b2ddfda
4 changed files with 117 additions and 18 deletions

View file

@ -3,6 +3,8 @@ name = "subtytle"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
license = "AGPL-3.0" license = "AGPL-3.0"
keywords = ["subtitle", "srt", "ass"]
description = "A library to work with the .ASS format"
authors = ["4o1x5 <4o1x5@4o1x5.dev>"] authors = ["4o1x5 <4o1x5@4o1x5.dev>"]

77
flake.lock Normal file
View file

@ -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
}

View file

@ -469,7 +469,6 @@ impl FromStr for AssSubtitle {
} }
} }
CurrentSection::ScriptInfo => { CurrentSection::ScriptInfo => {
// Do nothing
let parts: Vec<String> = line.split(":").map(|e| e.to_string()).collect(); let parts: Vec<String> = line.split(":").map(|e| e.to_string()).collect();
let key = parts.first().unwrap().clone(); let key = parts.first().unwrap().clone();
let value = parts.get(1).ok_or(AssConvertError::KeyHasNoValue)?.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(), text: dialogue.get("Text").map(|v| v.to_string()).unwrap(),
}); });
} }
// TODO: Parse ScriptInfo
let script_info = ScriptInfo { let script_info = ScriptInfo {
title: "Subtitle".to_string(), title: script_fields.get("Title").map(|v| v.to_owned()).ok_or(
original_script: "Original Script".to_string(), AssConvertError::InvalidScriptInfo("Title missing".to_string()),
script_type: "v4.00+".to_string(), )?,
collisions: None, original_script: script_fields
play_depth: None, .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 { Ok(Self {

View file

@ -163,8 +163,9 @@ 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: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. 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); tracing::info!("Less tags: {:?}", ass_decoded);
assert_eq!(ass_decoded.is_err(), true);
} }
#[test(test)] #[test(test)]