Skip to content

Commit

Permalink
Update helpers.rs (#6255)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people authored Sep 11, 2024
1 parent d76a146 commit 11d8f54
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions sway-utils/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use std::{
fs,
path::{Path, PathBuf},
};
use walkdir::WalkDir;

pub fn get_sway_files(path: PathBuf) -> Vec<PathBuf> {
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);
Expand All @@ -25,11 +26,10 @@ pub fn get_sway_files(path: PathBuf) -> Vec<PathBuf> {
}

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;
Expand All @@ -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<T>(slice: &[T]) -> impl DoubleEndedIterator<Item = &[T]> {
(1..=slice.len()).map(move |len| &slice[..len])
Expand All @@ -54,16 +53,14 @@ pub fn find_nested_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
///
/// Starts the search from child dirs of `starter_path`.
pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option<PathBuf> {
use walkdir::WalkDir;
let starter_dir = if starter_path.is_dir() {
starter_path
} else {
starter_path.parent()?
};
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();
Expand All @@ -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<P: AsRef<Path>>(
starter_path: P,
file_name: &str,
) -> Option<PathBuf> {
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();
Expand All @@ -95,28 +91,29 @@ pub fn find_parent_dir_with_file<P: AsRef<Path>>(
}
None
}

/// Continually go up in the file tree until a Forc manifest file is found.
pub fn find_parent_manifest_dir<P: AsRef<Path>>(starter_path: P) -> Option<PathBuf> {
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<T: AsRef<Path>, F>(
starter_path: T,
f: F,
check: F,
) -> Option<PathBuf>
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))
}
})
}

0 comments on commit 11d8f54

Please sign in to comment.