mirror of
https://github.com/132nd-vWing/tacview-splitter.git
synced 2026-08-26 06:48:36 +02:00
start adding error handling (WIP)
This commit is contained in:
+11
-1
@@ -4,10 +4,19 @@ mod reader;
|
|||||||
mod tacview;
|
mod tacview;
|
||||||
mod writer;
|
mod writer;
|
||||||
|
|
||||||
|
use std::process;
|
||||||
|
|
||||||
use crate::{tacview::Coalition, writer::StringWriter};
|
use crate::{tacview::Coalition, writer::StringWriter};
|
||||||
|
|
||||||
fn main() {
|
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);
|
println!("Processing {}", input_filename);
|
||||||
let lines = reader::read_data(&input_filename, is_zip);
|
let lines = reader::read_data(&input_filename, is_zip);
|
||||||
let (header, body) = processor::split_into_header_and_body(&lines);
|
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);
|
let mut purple_writer = writer::create_writer(is_zip, &input_filename);
|
||||||
purple_writer.write_strings(header);
|
purple_writer.write_strings(header);
|
||||||
purple_writer.write_for_coalition(body, &coalition_per_line, Coalition::Purple);
|
purple_writer.write_for_coalition(body, &coalition_per_line, Coalition::Purple);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-25
@@ -1,48 +1,44 @@
|
|||||||
use crate::constants::{EXTENSION_TXT, EXTENSION_ZIP};
|
use crate::constants::{EXTENSION_TXT, EXTENSION_ZIP};
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
||||||
|
|
||||||
pub fn find_input_file() -> (String, bool) {
|
pub fn find_input_file() -> Result<(String, bool), Error> {
|
||||||
let read_dir = fs::read_dir(".").expect("Could not read current directory");
|
let read_dir = fs::read_dir(".")?;
|
||||||
|
|
||||||
for entry_result in read_dir {
|
for entry_result in read_dir {
|
||||||
let entry = entry_result.expect("Could not parse DirEntry");
|
let path_buf = entry_result?.path();
|
||||||
let path_buf = entry.path();
|
|
||||||
let filename = path_buf.to_string_lossy().to_string();
|
let filename = path_buf.to_string_lossy().to_string();
|
||||||
|
|
||||||
if filename.ends_with(EXTENSION_TXT) {
|
if filename.ends_with(EXTENSION_TXT) {
|
||||||
return (filename, false);
|
return Ok((filename, false));
|
||||||
} else if filename.ends_with(EXTENSION_ZIP) {
|
} else if filename.ends_with(EXTENSION_ZIP) {
|
||||||
return (filename, true);
|
return Ok((filename, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("No tacview input file found in current directory.");
|
Err(Error::new(
|
||||||
std::process::exit(1);
|
ErrorKind::NotFound,
|
||||||
|
"No tacview input file found in current directory.",
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_data(filename: &str, is_zip: bool) -> Vec<String> {
|
pub fn read_data(filename: &str, is_zip: bool) -> Result<Vec<String>, Error> {
|
||||||
let file = fs::File::open(filename).expect("Could not read from input file");
|
let file = fs::File::open(filename)?;
|
||||||
let buf = BufReader::new(file);
|
let buf = BufReader::new(file);
|
||||||
if is_zip {
|
if is_zip {
|
||||||
read_zip(buf)
|
Ok(read_zip(buf)?)
|
||||||
} else {
|
} else {
|
||||||
read_txt(buf)
|
Ok(read_txt(buf)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_zip(buf: BufReader<File>) -> Vec<String> {
|
fn read_zip(buf: BufReader<File>) -> Result<Vec<String>, Error> {
|
||||||
let mut archive = zip::ZipArchive::new(buf).expect("Could not read zip data");
|
let mut archive = zip::ZipArchive::new(buf)?;
|
||||||
let inner_file = archive
|
let inner_file = archive.by_index(0)?;
|
||||||
.by_index(0)
|
|
||||||
.expect("Could not read telemetry file from zip archive");
|
|
||||||
let inner_buf = BufReader::new(inner_file);
|
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> {
|
fn read_txt<T: BufRead>(buf: T) -> Result<Vec<String>, Error> {
|
||||||
let lines: Vec<String> = buf
|
let lines: Vec<String> = buf.lines().map(|l|).collect();
|
||||||
.lines()
|
Ok(lines)
|
||||||
.map(|l| l.expect("Could not parse line"))
|
|
||||||
.collect();
|
|
||||||
lines
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user