Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update helpers.rs #6255

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
})
}
Loading