Skip to content

Commit 11d8f54

Browse files
cypherpepecypherpepeIGI-111
authored
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 <cypherfrog@skiff.com> Co-authored-by: IGI-111 <igi-111@protonmail.com>
1 parent d76a146 commit 11d8f54

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

sway-utils/src/helpers.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use std::{
44
fs,
55
path::{Path, PathBuf},
66
};
7+
use walkdir::WalkDir;
78

89
pub fn get_sway_files(path: PathBuf) -> Vec<PathBuf> {
910
let mut files = vec![];
1011
let mut dir_entries = vec![path];
1112

1213
while let Some(next_dir) = dir_entries.pop() {
13-
if let Ok(read_dir) = fs::read_dir(next_dir) {
14-
for entry in read_dir.filter_map(std::result::Result::ok) {
14+
if let Ok(read_dir) = fs::read_dir(&next_dir) {
15+
for entry in read_dir.filter_map(Result::ok) {
1516
let path = entry.path();
1617
if path.is_dir() {
1718
dir_entries.push(path);
@@ -25,11 +26,10 @@ pub fn get_sway_files(path: PathBuf) -> Vec<PathBuf> {
2526
}
2627

2728
pub fn is_sway_file(file: &Path) -> bool {
28-
let res = file.extension();
29-
file.is_file() && Some(OsStr::new(constants::SWAY_EXTENSION)) == res
29+
file.is_file() && file.extension() == Some(OsStr::new(constants::SWAY_EXTENSION))
3030
}
3131

32-
/// create an iterator over all prefixes in a slice, smallest first
32+
/// Create an iterator over all prefixes in a slice, smallest first
3333
///
3434
/// ```
3535
/// # use sway_utils::iter_prefixes;
@@ -39,7 +39,6 @@ pub fn is_sway_file(file: &Path) -> bool {
3939
/// assert_eq!(it.next(), Some([1, 2].as_slice()));
4040
/// assert_eq!(it.next(), Some([1, 2, 3].as_slice()));
4141
/// assert_eq!(it.next(), None);
42-
///
4342
/// ```
4443
pub fn iter_prefixes<T>(slice: &[T]) -> impl DoubleEndedIterator<Item = &[T]> {
4544
(1..=slice.len()).map(move |len| &slice[..len])
@@ -54,16 +53,14 @@ pub fn find_nested_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
5453
///
5554
/// Starts the search from child dirs of `starter_path`.
5655
pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option<PathBuf> {
57-
use walkdir::WalkDir;
5856
let starter_dir = if starter_path.is_dir() {
5957
starter_path
6058
} else {
6159
starter_path.parent()?
6260
};
6361
WalkDir::new(starter_path).into_iter().find_map(|e| {
6462
let entry = e.ok()?;
65-
if entry.path() != starter_dir.join(file_name)
66-
&& entry.file_name().to_string_lossy() == file_name
63+
if entry.path() != starter_dir.join(file_name) && entry.file_name() == OsStr::new(file_name)
6764
{
6865
let mut entry = entry.path().to_path_buf();
6966
entry.pop();
@@ -77,14 +74,13 @@ pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option
7774
/// Continually go up in the file tree until a specified file is found.
7875
///
7976
/// Starts the search from `starter_path`.
80-
#[allow(clippy::branches_sharing_code)]
8177
pub fn find_parent_dir_with_file<P: AsRef<Path>>(
8278
starter_path: P,
8379
file_name: &str,
8480
) -> Option<PathBuf> {
8581
let mut path = std::fs::canonicalize(starter_path).ok()?;
86-
let empty_path = PathBuf::from("/");
87-
while path != empty_path {
82+
let root_path = PathBuf::from("/");
83+
while path != root_path {
8884
path.push(file_name);
8985
if path.exists() {
9086
path.pop();
@@ -95,28 +91,29 @@ pub fn find_parent_dir_with_file<P: AsRef<Path>>(
9591
}
9692
None
9793
}
94+
9895
/// Continually go up in the file tree until a Forc manifest file is found.
9996
pub fn find_parent_manifest_dir<P: AsRef<Path>>(starter_path: P) -> Option<PathBuf> {
10097
find_parent_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME)
10198
}
10299

103-
/// Continually go up in the file tree until a Forc manifest file is found and given predicate
100+
/// Continually go up in the file tree until a Forc manifest file is found and the given predicate
104101
/// returns true.
105102
pub fn find_parent_manifest_dir_with_check<T: AsRef<Path>, F>(
106103
starter_path: T,
107-
f: F,
104+
check: F,
108105
) -> Option<PathBuf>
109106
where
110107
F: Fn(&Path) -> bool,
111108
{
112-
find_parent_manifest_dir(starter_path).and_then(|manifest_dir| {
113-
// If given check satisfies return current dir otherwise start searching from the parent.
114-
if f(&manifest_dir) {
109+
find_parent_manifest_dir(&starter_path).and_then(|manifest_dir| {
110+
// If given check satisfies, return the current dir; otherwise, start searching from the parent.
111+
if check(&manifest_dir) {
115112
Some(manifest_dir)
116-
} else if let Some(parent_dir) = manifest_dir.parent() {
117-
find_parent_manifest_dir_with_check(parent_dir, f)
118113
} else {
119-
None
114+
manifest_dir
115+
.parent()
116+
.and_then(|parent_dir| find_parent_manifest_dir_with_check(parent_dir, check))
120117
}
121118
})
122119
}

0 commit comments

Comments
 (0)