start adding error handling (WIP)

This commit is contained in:
132nd-Professor
2022-08-11 20:56:49 +02:00
parent 84f1bf7efe
commit 7d66b1ff31
2 changed files with 32 additions and 26 deletions
+11 -1
View File
@@ -4,10 +4,19 @@ mod reader;
mod tacview;
mod writer;
use std::process;
use crate::{tacview::Coalition, writer::StringWriter};
fn main() {
let (input_filename, is_zip) = reader::find_input_file();
if let Err(err) = main_inner() {
eprintln!("{err}");
process::exit(1);
}
}
fn main_inner() -> Result<(), Box<dyn std::error::Error>> {
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);
@@ -25,4 +34,5 @@ fn main() {
let mut purple_writer = writer::create_writer(is_zip, &input_filename);
purple_writer.write_strings(header);
purple_writer.write_for_coalition(body, &coalition_per_line, Coalition::Purple);
Ok(())
}
+21 -25
View File
@@ -1,48 +1,44 @@
use crate::constants::{EXTENSION_TXT, EXTENSION_ZIP};
use std::fs::{self, File};
use std::io::{BufRead, BufReader};
use std::io::{BufRead, BufReader, Error, ErrorKind};
pub fn find_input_file() -> (String, bool) {
let read_dir = fs::read_dir(".").expect("Could not read current directory");
pub fn find_input_file() -> Result<(String, bool), Error> {
let read_dir = fs::read_dir(".")?;
for entry_result in read_dir {
let entry = entry_result.expect("Could not parse DirEntry");
let path_buf = entry.path();
let path_buf = entry_result?.path();
let filename = path_buf.to_string_lossy().to_string();
if filename.ends_with(EXTENSION_TXT) {
return (filename, false);
return Ok((filename, false));
} else if filename.ends_with(EXTENSION_ZIP) {
return (filename, true);
return Ok((filename, true));
}
}
println!("No tacview input file found in current directory.");
std::process::exit(1);
Err(Error::new(
ErrorKind::NotFound,
"No tacview input file found in current directory.",
))
}
pub fn read_data(filename: &str, is_zip: bool) -> Vec<String> {
let file = fs::File::open(filename).expect("Could not read from input file");
pub fn read_data(filename: &str, is_zip: bool) -> Result<Vec<String>, Error> {
let file = fs::File::open(filename)?;
let buf = BufReader::new(file);
if is_zip {
read_zip(buf)
Ok(read_zip(buf)?)
} else {
read_txt(buf)
Ok(read_txt(buf)?)
}
}
fn read_zip(buf: BufReader<File>) -> Vec<String> {
let mut archive = zip::ZipArchive::new(buf).expect("Could not read zip data");
let inner_file = archive
.by_index(0)
.expect("Could not read telemetry file from zip archive");
fn read_zip(buf: BufReader<File>) -> Result<Vec<String>, Error> {
let mut archive = zip::ZipArchive::new(buf)?;
let inner_file = archive.by_index(0)?;
let inner_buf = BufReader::new(inner_file);
read_txt(inner_buf)
Ok(read_txt(inner_buf)?)
}
fn read_txt<T: BufRead>(buf: T) -> Vec<String> {
let lines: Vec<String> = buf
.lines()
.map(|l| l.expect("Could not parse line"))
.collect();
lines
fn read_txt<T: BufRead>(buf: T) -> Result<Vec<String>, Error> {
let lines: Vec<String> = buf.lines().map(|l|).collect();
Ok(lines)
}