diff --git a/sway-utils/src/helpers.rs b/sway-utils/src/helpers.rs index be67108ea35..45ca8603f0e 100644 --- a/sway-utils/src/helpers.rs +++ b/sway-utils/src/helpers.rs @@ -4,14 +4,15 @@ use std::{ fs, path::{Path, PathBuf}, }; +use walkdir::WalkDir; pub fn get_sway_files(path: PathBuf) -> Vec { let mut files = vec![]; let mut dir_entries = vec![path]; while let Some(next_dir) = dir_entries.pop() { - if let Ok(read_dir) = fs::read_dir(next_dir) { - for entry in read_dir.filter_map(std::result::Result::ok) { + if let Ok(read_dir) = fs::read_dir(&next_dir) { + for entry in read_dir.filter_map(Result::ok) { let path = entry.path(); if path.is_dir() { dir_entries.push(path); @@ -25,11 +26,10 @@ pub fn get_sway_files(path: PathBuf) -> Vec { } pub fn is_sway_file(file: &Path) -> bool { - let res = file.extension(); - file.is_file() && Some(OsStr::new(constants::SWAY_EXTENSION)) == res + file.is_file() && file.extension() == Some(OsStr::new(constants::SWAY_EXTENSION)) } -/// create an iterator over all prefixes in a slice, smallest first +/// Create an iterator over all prefixes in a slice, smallest first /// /// ``` /// # use sway_utils::iter_prefixes; @@ -39,7 +39,6 @@ pub fn is_sway_file(file: &Path) -> bool { /// assert_eq!(it.next(), Some([1, 2].as_slice())); /// assert_eq!(it.next(), Some([1, 2, 3].as_slice())); /// assert_eq!(it.next(), None); -/// /// ``` pub fn iter_prefixes(slice: &[T]) -> impl DoubleEndedIterator { (1..=slice.len()).map(move |len| &slice[..len]) @@ -54,7 +53,6 @@ pub fn find_nested_manifest_dir(starter_path: &Path) -> Option { /// /// Starts the search from child dirs of `starter_path`. pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option { - use walkdir::WalkDir; let starter_dir = if starter_path.is_dir() { starter_path } else { @@ -62,8 +60,7 @@ pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option }; WalkDir::new(starter_path).into_iter().find_map(|e| { let entry = e.ok()?; - if entry.path() != starter_dir.join(file_name) - && entry.file_name().to_string_lossy() == file_name + if entry.path() != starter_dir.join(file_name) && entry.file_name() == OsStr::new(file_name) { let mut entry = entry.path().to_path_buf(); entry.pop(); @@ -77,14 +74,13 @@ pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option /// Continually go up in the file tree until a specified file is found. /// /// Starts the search from `starter_path`. -#[allow(clippy::branches_sharing_code)] pub fn find_parent_dir_with_file>( starter_path: P, file_name: &str, ) -> Option { let mut path = std::fs::canonicalize(starter_path).ok()?; - let empty_path = PathBuf::from("/"); - while path != empty_path { + let root_path = PathBuf::from("/"); + while path != root_path { path.push(file_name); if path.exists() { path.pop(); @@ -95,28 +91,29 @@ pub fn find_parent_dir_with_file>( } None } + /// Continually go up in the file tree until a Forc manifest file is found. pub fn find_parent_manifest_dir>(starter_path: P) -> Option { find_parent_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME) } -/// Continually go up in the file tree until a Forc manifest file is found and given predicate +/// Continually go up in the file tree until a Forc manifest file is found and the given predicate /// returns true. pub fn find_parent_manifest_dir_with_check, F>( starter_path: T, - f: F, + check: F, ) -> Option where F: Fn(&Path) -> bool, { - find_parent_manifest_dir(starter_path).and_then(|manifest_dir| { - // If given check satisfies return current dir otherwise start searching from the parent. - if f(&manifest_dir) { + find_parent_manifest_dir(&starter_path).and_then(|manifest_dir| { + // If given check satisfies, return the current dir; otherwise, start searching from the parent. + if check(&manifest_dir) { Some(manifest_dir) - } else if let Some(parent_dir) = manifest_dir.parent() { - find_parent_manifest_dir_with_check(parent_dir, f) } else { - None + manifest_dir + .parent() + .and_then(|parent_dir| find_parent_manifest_dir_with_check(parent_dir, check)) } }) }