diff --git a/src/main.rs b/src/main.rs index af63d18..de94603 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,15 +39,24 @@ fn find_subs(root: &Path, match_str: &OsStr) -> io::Result> { Ok(None) } +macro_rules! nested_try { + ($ex:expr) => { + match $ex { + Ok(val) => val, + Err(e) => return Some(Err(e)), + } + } +} + fn find_files( root: &Path, extension: &OsStr, ) -> io::Result>> { - struct FindFonts { + struct FindFilesIter { read_dirs: Vec, extension: OsString, } - impl Iterator for FindFonts { + impl Iterator for FindFilesIter { type Item = io::Result; fn next(&mut self) -> Option { @@ -56,22 +65,12 @@ fn find_files( self.read_dirs.pop(); continue; }; - let entry = match next { - Ok(entry) => entry, - Err(e) => return Some(Err(e)), - }; - let file_type = match entry.file_type() { - Err(e) => return Some(Err(e)), - Ok(file_type) => file_type, - }; + let entry = nested_try!(next); + let file_type = nested_try!(entry.file_type()); if file_type.is_dir() { - match entry.path().read_dir() { - Err(e) => return Some(Err(e)), - Ok(read_dir) => { - self.read_dirs.push(read_dir); - continue; - } - } + let read_dir = nested_try!(entry.path().read_dir()); + self.read_dirs.push(read_dir); + continue; } else { let path = entry.path(); if path.extension().is_some_and(|ex| ex == self.extension) { @@ -83,7 +82,7 @@ fn find_files( } } } - Ok(FindFonts { + Ok(FindFilesIter { read_dirs: vec![root.read_dir()?], extension: extension.to_owned(), })