Implemented a function to collect invalid suffixes
parent
8d4b5b3f1c
commit
c798b44f45
|
@ -12,21 +12,10 @@ pub fn validate(verbosity: u8, config: util::config::Config) {
|
|||
let out_files: Vec<PathBuf> = 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<PathBuf> = vec![];
|
||||
let mut invalid_suffix_out: Vec<PathBuf> = 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<PathBuf> = collect_invalid(&in_files, in_suffix);
|
||||
let invalid_suffix_out: Vec<PathBuf> = 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<PathBuf>, suf: &str) -> Vec<PathBuf> {
|
||||
let mut res: Vec<PathBuf> = 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<Vec<PathBuf>, &'static str> {
|
||||
let mut res: Vec<PathBuf> = vec![];
|
||||
|
||||
|
@ -64,14 +66,14 @@ fn get_dir_files(path: PathBuf) -> Result<Vec<PathBuf>, &'static str> {
|
|||
fn compare_files(in_files: Vec<PathBuf>, out_files: Vec<PathBuf>) -> Vec<PathBuf> {
|
||||
let mut res: Vec<PathBuf> = 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<PathBuf>, against: Vec<PathBuf>) -> Vec<PathBuf> {
|
||||
let res: Vec<PathBuf> = vec![];
|
||||
fn compare_list(from: &Vec<PathBuf>, against: &Vec<PathBuf>) -> Vec<PathBuf> {
|
||||
let mut res: Vec<PathBuf> = vec![];
|
||||
|
||||
// Compare each input file to each output file
|
||||
for file in from {
|
||||
|
@ -81,7 +83,7 @@ fn compare_list(from: Vec<PathBuf>, against: Vec<PathBuf>) -> Vec<PathBuf> {
|
|||
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<PathBuf>, 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<PathBuf> = 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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue