From 11d8f54255d0b3c2764ca9c0ea15a95ddea287ba Mon Sep 17 00:00:00 2001 From: Cypher Pepe <125112044+cypherpepe@users.noreply.github.com> Date: Wed, 11 Sep 2024 20:29:23 +0300 Subject: [PATCH] Update helpers.rs (#6255) Simplified conditions and variables for better understanding and readability. ## Description ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: cypherpepe Co-authored-by: IGI-111 --- sway-utils/src/helpers.rs | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) 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)) } }) }