mirror of
https://github.com/132nd-vWing/tacview-splitter.git
synced 2026-08-26 13:18:37 +02:00
finish error handling
This commit is contained in:
+34
-26
@@ -1,19 +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::Write;
|
||||
use std::io::{Error, Write};
|
||||
use zip::write::FileOptions;
|
||||
use zip::ZipWriter;
|
||||
|
||||
pub fn create_writer(is_zip: bool, filename: &str) -> Box<dyn Write> {
|
||||
pub fn create_writer(is_zip: bool, filename: &str) -> Result<Box<dyn Write>, Box<dyn ErrTrait>> {
|
||||
let base_name = remove_extension(filename, is_zip);
|
||||
if is_zip {
|
||||
Box::new(create_zipwriter(
|
||||
Ok(Box::new(create_zipwriter(
|
||||
&format!("{base_name}{EXTENSION_ZIP}"),
|
||||
&format!("{base_name}{EXTENSION_TXT}"),
|
||||
))
|
||||
)?))
|
||||
} else {
|
||||
Box::new(create_textwriter(base_name))
|
||||
Ok(Box::new(create_textwriter(base_name)?))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,45 +26,45 @@ fn remove_extension(filename: &str, is_zip: bool) -> &str {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_textwriter(filename: &str) -> File {
|
||||
File::create(filename).unwrap()
|
||||
fn create_textwriter(filename: &str) -> Result<File, Error> {
|
||||
File::create(filename)
|
||||
}
|
||||
|
||||
fn create_zipwriter(outer_name: &str, inner_name: &str) -> ZipWriter<File> {
|
||||
let mut writer = ZipWriter::new(create_textwriter(outer_name));
|
||||
writer
|
||||
.start_file(
|
||||
inner_name,
|
||||
FileOptions::default().compression_method(zip::CompressionMethod::Deflated),
|
||||
)
|
||||
.unwrap();
|
||||
writer
|
||||
fn create_zipwriter(outer_name: &str, inner_name: &str) -> Result<ZipWriter<File>, Error> {
|
||||
let mut writer = ZipWriter::new(create_textwriter(outer_name)?);
|
||||
writer.start_file(
|
||||
inner_name,
|
||||
FileOptions::default().compression_method(zip::CompressionMethod::Deflated),
|
||||
)?;
|
||||
Ok(writer)
|
||||
}
|
||||
|
||||
pub trait StringWriter {
|
||||
fn write_line<S: AsRef<str>>(&mut self, line: &S);
|
||||
fn write_line<S: AsRef<str>>(&mut self, line: &S) -> Result<(), Error>;
|
||||
|
||||
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]);
|
||||
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]) -> Result<(), Error>;
|
||||
|
||||
fn write_for_coalition<S: AsRef<str>>(
|
||||
&mut self,
|
||||
lines: &[S],
|
||||
coalition_per_line: &[Coalition],
|
||||
coalition: Coalition,
|
||||
);
|
||||
) -> Result<(), Box<dyn std::error::Error>>;
|
||||
}
|
||||
|
||||
impl<T> StringWriter for T
|
||||
where
|
||||
T: Write,
|
||||
{
|
||||
fn write_line<S: AsRef<str>>(&mut self, line: &S) {
|
||||
writeln!(self, "{}", line.as_ref()).unwrap();
|
||||
fn write_line<S: AsRef<str>>(&mut self, line: &S) -> Result<(), Error> {
|
||||
writeln!(self, "{}", line.as_ref())
|
||||
}
|
||||
|
||||
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]) {
|
||||
fn write_strings<S: AsRef<str>>(&mut self, lines: &[S]) -> Result<(), Error> {
|
||||
for line in lines {
|
||||
self.write_line(line);
|
||||
self.write_line(line)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_for_coalition<S: AsRef<str>>(
|
||||
@@ -71,12 +72,19 @@ where
|
||||
lines: &[S],
|
||||
coalition_per_line: &[Coalition],
|
||||
coalition: Coalition,
|
||||
) {
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
assert_eq!(lines.len(), coalition_per_line.len());
|
||||
lines
|
||||
|
||||
let iter = lines
|
||||
.iter()
|
||||
.zip(coalition_per_line)
|
||||
.filter(|(_, c)| **c == coalition || **c == Coalition::All)
|
||||
.for_each(|(l, _)| self.write_line(l));
|
||||
.map(|(l, _)| l);
|
||||
|
||||
for line in iter {
|
||||
self.write_line(line)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user