From d9695eab021707c10f219f9cee771c5145fa74e8 Mon Sep 17 00:00:00 2001 From: 132nd-Professor <132nd-Professor> Date: Sat, 17 Jul 2021 20:09:51 +0200 Subject: [PATCH] replaced all unwrap with expect --- src/lib.rs | 7 ++++--- src/main.rs | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2aee6b0..2b73d89 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod lib { const ERR_CANNOT_WRITE_DATA: &str = "Could not write data"; const ERR_CANNOT_OPEN_OUTPUT: &str = "Could not open output file"; + const ERR_CANNOT_BEGIN_FILE: &str = "Could not begin file in zip archive"; pub struct IDs<'a> { pub blue: Vec<&'a str>, @@ -81,15 +82,15 @@ pub mod lib { let file = fs::File::create(&filenames.zip.blue).expect(ERR_CANNOT_OPEN_OUTPUT); let mut blue = zip::ZipWriter::new(file); - blue.start_file(&filenames.txt.blue, options).unwrap(); + blue.start_file(&filenames.txt.blue, options).expect(ERR_CANNOT_BEGIN_FILE); let file = fs::File::create(&filenames.zip.red).expect(ERR_CANNOT_OPEN_OUTPUT); let mut red = zip::ZipWriter::new(file); - red.start_file(&filenames.txt.red, options).unwrap(); + red.start_file(&filenames.txt.red, options).expect(ERR_CANNOT_BEGIN_FILE); let file = fs::File::create(&filenames.zip.violet).expect(ERR_CANNOT_OPEN_OUTPUT); let mut violet = zip::ZipWriter::new(file); - violet.start_file(&filenames.txt.violet, options).unwrap(); + violet.start_file(&filenames.txt.violet, options).expect(ERR_CANNOT_BEGIN_FILE); let descriptors = Descriptors{blue, red, violet}; descriptors diff --git a/src/main.rs b/src/main.rs index 9424f2b..b72713b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,10 +101,12 @@ fn will_line_continue(line: &String) -> bool { } fn get_id_from_line(line: &String) -> &str { - let id = line - .split_once(',') // TODO catch the None - .unwrap() - .0; + let result = line.split_once(','); + let split = match result { + Some(t) => t, + None => panic!("Could not get ID from line!") + }; + let id = split.0; id } @@ -141,17 +143,26 @@ fn get_output_filenames(input_filename: &String) -> lib::OutputFilenames { let red = input_filename.replace(".zip", "_red.zip"); let violet = input_filename.replace(".zip", "_violet.zip"); let output_filenames_zip = lib::FilenamesVariant{blue, red, violet}; + sanity_check_output_filenames(input_filename, &output_filenames_zip); - // TODO make sure the replace was successful let blue = input_filename.replace(".txt", "_blue.txt"); let red = input_filename.replace(".txt", "_red.txt"); let violet = input_filename.replace(".txt", "_violet.txt"); let output_filenames_txt = lib::FilenamesVariant{blue, red, violet}; + sanity_check_output_filenames(input_filename, &output_filenames_txt); let output_filenames = lib::OutputFilenames{txt: output_filenames_txt, zip: output_filenames_zip}; output_filenames } +fn sanity_check_output_filenames(input_filename: &String, output_filenames: &lib::FilenamesVariant) { + if input_filename == &output_filenames.blue || + input_filename == &output_filenames.red || + input_filename == &output_filenames.violet { + panic!("Output filenames were the same as input filenames") + } +} + fn find_input_file() -> (String, bool) { let read_dir = fs::read_dir(".").expect("Could not read current directory"); for entry_result in read_dir { @@ -174,7 +185,7 @@ fn read_data(filename: &String, is_zip: bool) -> Vec { let buf = BufReader::new(file); return if is_zip { let mut archive = zip::ZipArchive::new(buf).expect("Could not read zip data"); - let inner_file = archive.by_index(0).unwrap(); + let inner_file = archive.by_index(0).expect("Could not read telemetry file from zip archive"); let inner_buf = BufReader::new(inner_file); let lines: Vec = inner_buf.lines() .map(|l| l.expect("Could not parse line"))