From b0d889ce37e5067ed4e20be7f8d35f31108d02c4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 31 Oct 2019 14:01:02 +0100 Subject: [PATCH] use chaining instead loops for find_fortune_files --- src/utils/fortunes_reader.rs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/utils/fortunes_reader.rs b/src/utils/fortunes_reader.rs index c15e3fa..641ac2f 100644 --- a/src/utils/fortunes_reader.rs +++ b/src/utils/fortunes_reader.rs @@ -16,25 +16,18 @@ pub fn get_random_cookie() -> Option { } fn find_fortune_files() -> Vec { - let locations = get_paths(); - let mut all_files: Vec = vec![]; - - for loc in locations { - let full_path = path::Path::new(&loc); - if full_path.exists() { - if let Ok(files) = fs::read_dir(full_path.canonicalize().unwrap()) { - for file in files { - if let Ok(f) = file { - if is_fortfile(&f.path().to_str().unwrap()) { - all_files.push(f.path().to_str().unwrap().to_owned()); - } - } - } - } - } - } - - all_files + get_paths() + .into_iter() + .map(|loc| path::Path::new(&loc).to_owned()) + .filter(|loc| loc.exists()) + .filter_map(|full_path| fs::read_dir(full_path.canonicalize().unwrap()).ok()) + .flat_map(|files| { + files + .filter_map(|file| file.ok()) + .map(|file| file.path().to_str().unwrap().to_owned()) + }) + .filter(|file_path| is_fortfile(file_path)) + .collect::>() } fn is_fortfile(path: &str) -> bool {