Add YAML support

This commit is contained in:
2025-08-10 11:11:55 +08:00
parent 8f5a5b170f
commit 808c5acd2b
4 changed files with 121 additions and 0 deletions

42
Cargo.lock generated
View File

@@ -414,6 +414,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "fancy-regex"
version = "0.16.1"
@@ -481,6 +487,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "hashbrown"
version = "0.15.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
[[package]]
name = "heck"
version = "0.5.0"
@@ -594,6 +606,16 @@ dependencies = [
"icu_properties",
]
[[package]]
name = "indexmap"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "inout"
version = "0.1.4"
@@ -757,6 +779,7 @@ dependencies = [
"rand",
"serde",
"serde_json",
"serde_yaml_ng",
"sha1",
"unicode-segmentation",
"url",
@@ -993,6 +1016,19 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml_ng"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4db627b98b36d4203a7b458cf3573730f2bb591b28871d916dfa9efabfd41f"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "sha1"
version = "0.10.6"
@@ -1084,6 +1120,12 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "url"
version = "2.5.4"

View File

@@ -26,6 +26,7 @@ png = { version = "0.17", optional = true }
rand = { version = "0.9", optional = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml_ng = "0.10"
sha1 = { version = "0.10", optional = true }
unicode-segmentation = "1.12"
url = { version = "2.5", optional = true }

View File

@@ -622,6 +622,41 @@ pub fn export_script(
}
}
}
types::OutputScriptType::Yaml => {
let enc = get_output_encoding(arg);
let s = match serde_yaml_ng::to_string(&mes) {
Ok(s) => s,
Err(e) => {
eprintln!("Error serializing messages to YAML: {}", e);
COUNTER.inc_error();
continue;
}
};
let b = match utils::encoding::encode_string(enc, &s, false) {
Ok(b) => b,
Err(e) => {
eprintln!("Error encoding string: {}", e);
COUNTER.inc_error();
continue;
}
};
let mut f = match utils::files::write_file(&out_path) {
Ok(f) => f,
Err(e) => {
eprintln!("Error writing file {}: {}", out_path.display(), e);
COUNTER.inc_error();
continue;
}
};
match f.write_all(&b) {
Ok(_) => {}
Err(e) => {
eprintln!("Error writing to file {}: {}", out_path.display(), e);
COUNTER.inc_error();
continue;
}
}
}
types::OutputScriptType::Custom => {
let enc = get_output_encoding(arg);
if let Err(e) = script_file.custom_export(&out_path, enc) {
@@ -859,6 +894,13 @@ pub fn export_script(
let mut f = utils::files::write_file(&f)?;
f.write_all(&b)?;
}
types::OutputScriptType::Yaml => {
let enc = get_output_encoding(arg);
let s = serde_yaml_ng::to_string(&mes)?;
let b = utils::encoding::encode_string(enc, &s, false)?;
let mut f = utils::files::write_file(&f)?;
f.write_all(&b)?;
}
types::OutputScriptType::Custom => {
let enc = get_output_encoding(arg);
script.custom_export(f.as_ref(), enc)?;
@@ -1068,6 +1110,33 @@ pub fn import_script(
}
}
}
types::OutputScriptType::Yaml => {
let enc = get_output_encoding(arg);
let b = match utils::files::read_file(&out_path) {
Ok(b) => b,
Err(e) => {
eprintln!("Error reading file {}: {}", out_path.display(), e);
COUNTER.inc_error();
continue;
}
};
let s = match utils::encoding::decode_to_string(enc, &b, true) {
Ok(s) => s,
Err(e) => {
eprintln!("Error decoding string: {}", e);
COUNTER.inc_error();
continue;
}
};
match serde_yaml_ng::from_str::<Vec<types::Message>>(&s) {
Ok(mes) => mes,
Err(e) => {
eprintln!("Error parsing YAML: {}", e);
COUNTER.inc_error();
continue;
}
}
}
types::OutputScriptType::Custom => {
Vec::new() // Custom scripts handle their own messages
}
@@ -1244,6 +1313,12 @@ pub fn import_script(
let mut parser = output_scripts::m3t::M3tParser::new(&s);
parser.parse()?
}
types::OutputScriptType::Yaml => {
let enc = get_output_encoding(arg);
let b = utils::files::read_file(&out_f)?;
let s = utils::encoding::decode_to_string(enc, &b, true)?;
serde_yaml_ng::from_str::<Vec<types::Message>>(&s)?
}
types::OutputScriptType::Custom => {
Vec::new() // Custom scripts handle their own messages
}

View File

@@ -60,6 +60,8 @@ pub enum OutputScriptType {
M3t,
/// JSON which can be used for GalTransl
Json,
/// YAML (same as JSON, but with YAML syntax)
Yaml,
/// Custom output
Custom,
}
@@ -75,6 +77,7 @@ impl AsRef<str> for OutputScriptType {
match self {
OutputScriptType::M3t => "m3t",
OutputScriptType::Json => "json",
OutputScriptType::Yaml => "yaml",
OutputScriptType::Custom => "",
}
}