use chaining instead loops for find_fortune_files

This commit is contained in:
runebaas 2019-10-31 14:01:02 +01:00
parent 2ef42d31e2
commit b0d889ce37
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6

View file

@ -16,25 +16,18 @@ pub fn get_random_cookie() -> Option<Cookie> {
} }
fn find_fortune_files() -> Vec<String> { fn find_fortune_files() -> Vec<String> {
let locations = get_paths(); get_paths()
let mut all_files: Vec<String> = vec![]; .into_iter()
.map(|loc| path::Path::new(&loc).to_owned())
for loc in locations { .filter(|loc| loc.exists())
let full_path = path::Path::new(&loc); .filter_map(|full_path| fs::read_dir(full_path.canonicalize().unwrap()).ok())
if full_path.exists() { .flat_map(|files| {
if let Ok(files) = fs::read_dir(full_path.canonicalize().unwrap()) { files
for file in files { .filter_map(|file| file.ok())
if let Ok(f) = file { .map(|file| file.path().to_str().unwrap().to_owned())
if is_fortfile(&f.path().to_str().unwrap()) { })
all_files.push(f.path().to_str().unwrap().to_owned()); .filter(|file_path| is_fortfile(file_path))
} .collect::<Vec<_>>()
}
}
}
}
}
all_files
} }
fn is_fortfile(path: &str) -> bool { fn is_fortfile(path: &str) -> bool {