From 7fe5218d3956bb771d71cbc5bb01175e6b531c6c Mon Sep 17 00:00:00 2001 From: Wataru Otsubo Date: Tue, 10 Sep 2024 21:52:41 +0900 Subject: [PATCH] refactor: split csv writing to func --- src/main.rs | 54 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 448d7cd..3dde35c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -336,6 +336,38 @@ impl PsbQaqcResult { // .collect_vec() // } +fn write_psbqaqc_csv(result: MasterLogResult, outfile: PathBuf) -> Result<()> { + let expanded_results = PsbQaqcResult::from_masterlogresult(result); + + let mut wtr = match outfile.exists() { + true => { + let file = File::options() + .read(true) + .append(true) + .open(outfile.clone())?; + csv::WriterBuilder::new() + .has_headers(false) + .from_writer(file) + } + false => { + println!("Creating new file: {}", outfile.display()); + let file = File::options() + .create_new(true) + .write(true) + .open(outfile.clone())?; + csv::WriterBuilder::new() + .has_headers(true) + .from_writer(file) + } + }; + for result in expanded_results { + wtr.serialize(result)?; + } + wtr.flush()?; + + Ok(()) +} + fn main() -> Result<()> { let args = Args::parse(); env_logger::Builder::new() @@ -361,27 +393,7 @@ fn main() -> Result<()> { let outfile = outfile.unwrap_or(get_output_filename(&result)); - let expanded_results = PsbQaqcResult::from_masterlogresult(result); - - let mut wtr = match outfile.exists() { - true => { - let file = File::options().read(true).append(true).open(outfile)?; - csv::WriterBuilder::new() - .has_headers(false) - .from_writer(file) - } - false => { - println!("Creating new file: {}", outfile.display()); - let file = File::options().create_new(true).write(true).open(outfile)?; - csv::WriterBuilder::new() - .has_headers(true) - .from_writer(file) - } - }; - for result in expanded_results { - wtr.serialize(result)?; - } - wtr.flush()?; + write_psbqaqc_csv(result, outfile)?; } Commands::CheckDB { csvfile } => { // TODO: more friendly message (like row number)