diff --git a/src/validate.rs b/src/validate.rs index a9d3192..9cb7356 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -12,21 +12,10 @@ pub fn validate(verbosity: u8, config: util::config::Config) { let out_files: Vec = get_dir_files(config.output_path).expect("Failed to read the output directory contents"); let mismatch: Vec<(PathBuf, PathBuf)>; - let mut invalid_suffix_in: Vec = vec![]; - let mut invalid_suffix_out: Vec = vec![]; - // check if they end in .in or .out respectively - for file in in_files { - if !file.ends_with(in_suffix) { - invalid_suffix_in.push(file); - } - } - - for file in out_files { - if !file.ends_with(out_suffix) { - invalid_suffix_out.push(file); - } - } + // check the files for valid suffixes + let invalid_suffix_in: Vec = collect_invalid(&in_files, in_suffix); + let invalid_suffix_out: Vec = collect_invalid(&out_files, out_suffix); // print the delinquints if invalid_suffix_in.len() > 0 { @@ -40,6 +29,19 @@ pub fn validate(verbosity: u8, config: util::config::Config) { // make sure each has a match } +fn collect_invalid(files: &Vec, suf: &str) -> Vec { + let mut res: Vec = vec![]; + + for file in files { + let extension = file.extension().expect("Invalid file name for validation"); + if extension.to_str() != Some(suf) { + res.push(file.clone()); + } + } + + return res +} + fn get_dir_files(path: PathBuf) -> Result, &'static str> { let mut res: Vec = vec![]; @@ -64,14 +66,14 @@ fn get_dir_files(path: PathBuf) -> Result, &'static str> { fn compare_files(in_files: Vec, out_files: Vec) -> Vec { let mut res: Vec = vec![]; - res.append(&mut compare_list(in_files, out_files)); - res.append(&mut compare_list(out_files, in_files)); + res.append(&mut compare_list(&in_files, &out_files)); + res.append(&mut compare_list(&out_files, &in_files)); return res; } -fn compare_list(from: Vec, against: Vec) -> Vec { - let res: Vec = vec![]; +fn compare_list(from: &Vec, against: &Vec) -> Vec { + let mut res: Vec = vec![]; // Compare each input file to each output file for file in from { @@ -81,7 +83,7 @@ fn compare_list(from: Vec, against: Vec) -> Vec { break; } } - res.push(file); // if the file does not have a counterpart, then push to the lost files + res.push(file.clone()); // if the file does not have a counterpart, then push to the lost files } return res; @@ -96,11 +98,37 @@ fn print_invalid_suffixes(files: Vec, exp: String) { _ => panic!("Failed to load the file"), }; - println!("{} => {}", initial, file.set_extension(exp).to_string()); + println!("{} => {}", initial, file.clone().set_extension(&exp).to_string()); } } #[cfg(test)] mod tests { + use super::*; + #[test] + fn test_collect_invalid() { + let suffix = "suf"; + let valid_path1 = PathBuf::from("path/file.suf"); + let valid_path2 = PathBuf::from("path/file_stupid.suf"); + let valid_path3 = PathBuf::from("path/file/dead.suf"); + let invalid_path1 = PathBuf::from("path/file.sub"); + let invalid_path2 = PathBuf::from("path/file_stupid.sur"); + let invalid_path3 = PathBuf::from("path/file.bub"); + + let files: Vec = vec![valid_path1.clone(), valid_path2.clone(), valid_path3.clone(), + invalid_path1.clone(), invalid_path2.clone(), invalid_path3.clone()]; + + let invalid = collect_invalid(&files, suffix); + + println!("{:?}", invalid); + + assert!(invalid.contains(&invalid_path1)); + assert!(invalid.contains(&invalid_path2)); + assert!(invalid.contains(&invalid_path3)); + + assert!(!invalid.contains(&valid_path1)); + assert!(!invalid.contains(&valid_path2)); + assert!(!invalid.contains(&valid_path3)); + } }