From 279919230d1687c090ac1604779e28dc29e6588e Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Tue, 26 Mar 2024 13:33:05 -0600 Subject: Don't error on dir read errors Previously, if a directory could not be read, dwvsh would panic. Instead of panicking, dwvsh should continue processing the other directories on the $PATH. --- src/path.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/path.rs b/src/path.rs index 412489b..2953982 100644 --- a/src/path.rs +++ b/src/path.rs @@ -7,7 +7,7 @@ use std::path::Path; /// specified. /// /// # Arguments -/// * `paths` - A reference to a vector that holds a list to the shell $PATHs +/// * `path` - A reference to a vector that holds a list to the shell $PATHs /// /// # Returns /// * `bins: Vec` - A new cache of all valid file paths in $PATH @@ -16,25 +16,19 @@ use std::path::Path; /// ``` /// let path = vec!["/bin"]; /// let path = path.into_iter().map(Path::new).collect(); -/// let mut bins = prefresh(&path); /// let mut bins = path::refresh(&path); /// ... /// // A situation occurs where the $PATH needs to be refreshed -/// bins = prefresh(&path) /// bins = path::refresh(&path) /// ``` -pub fn prefresh(path: &Vec<&Path>) -> Vec { pub fn refresh(path: &Vec<&Path>) -> Vec { let mut bins: Vec = Vec::new(); for p in path { - let files = fs::read_dir(p).expect( - format!( - "dwvsh: error: unable to read the contents of {}", - p.display().to_string() - ) - .as_str(), - ); + let files = match fs::read_dir(p) { + Ok(files) => files, + Err(_) => continue, + }; for file in files { bins.push(file.unwrap().path().display().to_string()); -- cgit v1.2.3