refactor: split csv writing to func

This commit is contained in:
Wataru Otsubo 2024-09-10 21:52:41 +09:00
parent 7529d003a6
commit 7fe5218d39

View file

@ -336,6 +336,38 @@ impl PsbQaqcResult {
// .collect_vec() // .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<()> { fn main() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
env_logger::Builder::new() env_logger::Builder::new()
@ -361,27 +393,7 @@ fn main() -> Result<()> {
let outfile = outfile.unwrap_or(get_output_filename(&result)); let outfile = outfile.unwrap_or(get_output_filename(&result));
let expanded_results = PsbQaqcResult::from_masterlogresult(result); write_psbqaqc_csv(result, outfile)?;
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()?;
} }
Commands::CheckDB { csvfile } => { Commands::CheckDB { csvfile } => {
// TODO: more friendly message (like row number) // TODO: more friendly message (like row number)