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

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 => "",
}
}