introduce anyhow, test

This commit is contained in:
132nd-Professor
2022-08-15 20:24:21 +02:00
parent 2bf2352c67
commit 126ef3708f
5 changed files with 213 additions and 129 deletions
+7 -47
View File
@@ -4,61 +4,21 @@ mod reader;
mod tacview;
mod writer;
use std::sync::Arc;
use std::{process, thread};
use anyhow::Result;
use crate::{tacview::Coalition, writer::StringWriter};
use crate::writer::OutputData;
fn main() {
if let Err(err) = main_inner() {
eprintln!("{err}");
process::exit(1);
}
}
fn main_inner() -> Result<(), Box<dyn std::error::Error>> {
fn main() -> Result<()> {
let (input_filename, is_zip) = reader::find_input_file()?;
println!("Processing {}", input_filename);
let lines = reader::read_data(&input_filename, is_zip)?;
let (header, body) = processor::split_into_header_and_body(lines)?;
let coalition_per_line = processor::divide_body_by_coalition(&body)?;
let coalition_per_line = Arc::new(processor::divide_body_by_coalition(&body)?);
let header = Arc::new(header);
let body = Arc::new(body);
let input_filename = Arc::new(input_filename);
let coalitions = vec![Coalition::Blue, Coalition::Red, Coalition::Violet];
// clippy wants us to combine both ierators into one. this is not what we want, because then
// we would spawn a thread, join it, and only then spawn a new one.
#[allow(clippy::needless_collect)]
let handles: Vec<_> = coalitions
.iter()
.map(|coalition| {
let z = is_zip;
let b = body.clone();
let cpl = coalition_per_line.clone();
let c = coalition.clone();
let h = header.clone();
let i = input_filename.clone();
thread::spawn(move || {
let mut writer = writer::create_writer(z, &*i.clone(), &c).unwrap();
writer.write_strings(&*h).unwrap();
writer.write_for_coalition(&*b, &*cpl, c).unwrap();
})
})
.collect();
let _: Vec<_> = handles
.into_iter()
.zip(coalitions)
.map(|(h, c)| {
h.join()
.unwrap_or_else(|_| println!("could not write data for {c}"))
})
.collect();
let output_data = OutputData::new(input_filename, is_zip, header, body, coalition_per_line);
output_data.save_to_disk()?;
Ok(())
}