From a2747d29b9e7fcca7cebb347a69572a0f2c615a9 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Wed, 21 May 2025 09:03:05 +0800 Subject: [PATCH] Add a result counter --- src/main.rs | 26 +++++++++++++++++--------- src/types.rs | 5 +++++ src/utils/counter.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 1 + 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 src/utils/counter.rs diff --git a/src/main.rs b/src/main.rs index a2fb193..48509e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -116,7 +116,7 @@ pub fn export_script( config: &types::ExtraConfig, output: &Option, is_dir: bool, -) -> anyhow::Result<()> { +) -> anyhow::Result { eprintln!("Exporting {}", filename); let script = parse_script(filename, arg, config)?.0; // println!("{:?}", script); @@ -126,7 +126,7 @@ pub fn export_script( // } if mes.is_empty() { eprintln!("No messages found"); - return Ok(()); + return Ok(types::ScriptResult::Ignored); } let of = match &arg.output_type { Some(t) => t.clone(), @@ -172,7 +172,7 @@ pub fn export_script( f.write_all(&b)?; } } - Ok(()) + Ok(types::ScriptResult::Ok) } pub fn import_script( @@ -181,7 +181,7 @@ pub fn import_script( config: &types::ExtraConfig, imp_cfg: &args::ImportArgs, is_dir: bool, -) -> anyhow::Result<()> { +) -> anyhow::Result { eprintln!("Importing {}", filename); let (script, builder) = parse_script(filename, arg, config)?; let of = match &arg.output_type { @@ -201,7 +201,7 @@ pub fn import_script( }; if !std::fs::exists(&out_f).unwrap_or(false) { eprintln!("Output file does not exist"); - return Ok(()); + return Ok(types::ScriptResult::Ignored); } let mes = match of { types::OutputScriptType::Json => { @@ -220,7 +220,7 @@ pub fn import_script( }; if mes.is_empty() { eprintln!("No messages found"); - return Ok(()); + return Ok(types::ScriptResult::Ignored); } let encoding = get_patched_encoding(imp_cfg, builder); let patched_f = if is_dir { @@ -235,7 +235,7 @@ pub fn import_script( imp_cfg.patched.clone() }; script.import_messages(mes, &patched_f, encoding)?; - Ok(()) + Ok(types::ScriptResult::Ok) } fn main() { @@ -246,6 +246,7 @@ fn main() { let cfg = types::ExtraConfig { circus_mes_type: arg.circus_mes_type.clone(), }; + let counter = utils::counter::Counter::new(); match &arg.command { args::Command::Export { input, output } => { let (scripts, is_dir) = utils::files::collect_files(input, arg.recursive).unwrap(); @@ -268,8 +269,11 @@ fn main() { for script in scripts.iter() { let re = export_script(&script, &arg, &cfg, output, is_dir); match re { - Ok(_) => {} + Ok(s) => { + counter.inc(s); + } Err(e) => { + counter.inc_error(); eprintln!("Error exporting {}: {}", script, e); if arg.backtrace { eprintln!("Backtrace: {}", e.backtrace()); @@ -295,8 +299,11 @@ fn main() { for script in scripts.iter() { let re = import_script(&script, &arg, &cfg, args, is_dir); match re { - Ok(_) => {} + Ok(s) => { + counter.inc(s); + } Err(e) => { + counter.inc_error(); eprintln!("Error exporting {}: {}", script, e); if arg.backtrace { eprintln!("Backtrace: {}", e.backtrace()); @@ -306,4 +313,5 @@ fn main() { } } } + eprintln!("{}", counter); } diff --git a/src/types.rs b/src/types.rs index 40e2fda..8400621 100644 --- a/src/types.rs +++ b/src/types.rs @@ -184,3 +184,8 @@ impl Message { Message { message, name } } } + +pub enum ScriptResult { + Ok, + Ignored, +} diff --git a/src/utils/counter.rs b/src/utils/counter.rs new file mode 100644 index 0000000..617dc91 --- /dev/null +++ b/src/utils/counter.rs @@ -0,0 +1,42 @@ +use crate::types::*; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering::SeqCst; + +pub struct Counter { + ok: AtomicUsize, + ignored: AtomicUsize, + error: AtomicUsize, +} + +impl Counter { + pub fn new() -> Self { + Self { + ok: AtomicUsize::new(0), + ignored: AtomicUsize::new(0), + error: AtomicUsize::new(0), + } + } + + pub fn inc_error(&self) { + self.error.fetch_add(1, SeqCst); + } + + pub fn inc(&self, result: ScriptResult) { + match result { + ScriptResult::Ok => self.ok.fetch_add(1, SeqCst), + ScriptResult::Ignored => self.ignored.fetch_add(1, SeqCst), + }; + } +} + +impl std::fmt::Display for Counter { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "OK: {}, Ignored: {}, Error: {}", + self.ok.load(SeqCst), + self.ignored.load(SeqCst), + self.error.load(SeqCst) + ) + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e3b35d8..4bdc743 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,4 @@ +pub mod counter; pub mod encoding; #[cfg(windows)] mod encoding_win;