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
+92 -18
View File
@@ -1,16 +1,20 @@
use crate::constants::{EXTENSION_TXT, EXTENSION_ZIP};
use crate::tacview::Coalition;
use std::error::Error as ErrTrait;
use std::fs::File;
use std::io::{Error, Write};
use std::io::Write;
use std::sync::Arc;
use std::thread;
use anyhow::{Context, Result};
use zip::write::FileOptions;
use zip::ZipWriter;
use crate::constants::{EXTENSION_TXT, EXTENSION_ZIP};
use crate::tacview::Coalition;
pub fn create_writer(
is_zip: bool,
filename: &str,
coalition: &Coalition,
) -> Result<Box<dyn Write>, Box<dyn ErrTrait>> {
) -> Result<Box<dyn Write>> {
let base_name = remove_extension(filename, is_zip);
let txt_name = format!("{base_name}_{coalition}{EXTENSION_TXT}");
@@ -31,11 +35,11 @@ fn remove_extension(filename: &str, is_zip: bool) -> &str {
}
}
fn create_textwriter(filename: &str) -> Result<File, Error> {
File::create(filename)
fn create_textwriter(filename: &str) -> Result<File> {
File::create(filename).with_context(|| "could not create text file")
}
fn create_zipwriter(outer_name: &str, inner_name: &str) -> Result<ZipWriter<File>, Error> {
fn create_zipwriter(outer_name: &str, inner_name: &str) -> Result<ZipWriter<File>> {
let mut writer = ZipWriter::new(create_textwriter(outer_name)?);
writer.start_file(
inner_name,
@@ -45,27 +49,27 @@ fn create_zipwriter(outer_name: &str, inner_name: &str) -> Result<ZipWriter<File
}
pub trait StringWriter {
fn write_line<S: AsRef<str>>(&mut self, line: &S) -> Result<(), Error>;
fn write_line<S: AsRef<str>>(&mut self, line: &S) -> Result<()>;
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]) -> Result<(), Error>;
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]) -> Result<()>;
fn write_for_coalition<S: AsRef<str>>(
&mut self,
lines: &[S],
coalition_per_line: &[Coalition],
coalition: Coalition,
) -> Result<(), Box<dyn std::error::Error>>;
coalition: &Coalition,
) -> Result<()>;
}
impl<T> StringWriter for T
where
T: Write,
{
fn write_line<S: AsRef<str>>(&mut self, line: &S) -> Result<(), Error> {
writeln!(self, "{}", line.as_ref())
fn write_line<S: AsRef<str>>(&mut self, line: &S) -> Result<()> {
writeln!(self, "{}", line.as_ref()).with_context(|| "could not write line")
}
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]) -> Result<(), Error> {
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]) -> Result<()> {
for line in lines {
self.write_line(line)?;
}
@@ -76,14 +80,14 @@ where
&mut self,
lines: &[S],
coalition_per_line: &[Coalition],
coalition: Coalition,
) -> Result<(), Box<dyn std::error::Error>> {
coalition: &Coalition,
) -> Result<()> {
assert_eq!(lines.len(), coalition_per_line.len());
let iter = lines
.iter()
.zip(coalition_per_line)
.filter(|(_, c)| **c == coalition || **c == Coalition::All)
.filter(|(_, c)| *c == coalition || **c == Coalition::All)
.map(|(l, _)| l);
for line in iter {
@@ -93,3 +97,73 @@ where
Ok(())
}
}
pub struct OutputData {
input_filename: Arc<String>,
is_zip: bool,
header: Arc<Vec<String>>,
body: Arc<Vec<String>>,
coalition_per_line: Arc<Vec<Coalition>>,
}
impl OutputData {
pub fn new(
input_filename: String,
is_zip: bool,
header: Vec<String>,
body: Vec<String>,
coalition_per_line: Vec<Coalition>,
) -> Self {
let body = Arc::new(body);
let coalition_per_line = Arc::new(coalition_per_line);
let header = Arc::new(header);
let input_filename = Arc::new(input_filename);
Self {
is_zip,
body,
coalition_per_line,
header,
input_filename,
}
}
// 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)]
pub fn save_to_disk(&self) -> Result<()> {
let coalitions = vec![Coalition::Blue, Coalition::Red, Coalition::Violet];
let handles: Vec<_> = coalitions
.iter()
.map(|coalition| {
let z = self.is_zip;
let b = self.body.clone();
let cpl = self.coalition_per_line.clone();
let c = coalition.clone();
let h = self.header.clone();
let i = self.input_filename.clone();
thread::spawn(move || {
let mut writer = create_writer(z, &*i.clone(), &c)
.with_context(|| format!("could not create writer for {c}"))
.unwrap();
writer
.write_strings(&*h)
.with_context(|| format!("could not write header for {c}"))
.unwrap();
writer
.write_for_coalition(&*b, &*cpl, &c)
.with_context(|| format!("could not write body for {c}"))
.unwrap();
})
})
.collect();
handles.into_iter().zip(coalitions).for_each(|(h, c)| {
h.join()
.unwrap_or_else(|_| println!("could not write data for {c}"))
});
Ok(())
}
}