WalkOptions

Struct WalkOptions 

Source
pub struct WalkOptions { /* private fields */ }
Expand description

Structure encoding the desired walking options

§Example

use fs_walk;

let w = fs_walk::WalkOptions::new()
    // we want to walk only files
    .files()
    // we want files with .o extension
    .extension("o")
    .walk("./");

assert!(w.count() > 0);

Implementations§

Source§

impl WalkOptions

Source

pub fn new() -> Self

Create default walking options. The default behaviour is to return both files and directories.

§Example
use fs_walk;
use std::path::PathBuf;

let o = fs_walk::WalkOptions::new();

let paths: Vec<PathBuf> = o.walk("./").flatten().collect();

assert!(paths.iter().any(|p| p.is_dir()));
assert!(paths.iter().any(|p| p.is_file()));
Source

pub fn dirs(&mut self) -> &mut Self

Configure walking option to return only directories

§Example
use fs_walk::WalkOptions;

for p in WalkOptions::new().dirs().walk("./").flatten() {
    assert!(p.is_dir());
}
Source

pub fn files(&mut self) -> &mut Self

Configure walking option to return only files

§Example
use fs_walk::WalkOptions;

for p in WalkOptions::new().files().walk("./").flatten() {
    assert!(p.is_file());
}

Configures the walker to follow symbolic links during traversal.

By default, the walker does not follow symbolic links. When this option is enabled, the walker will recursively traverse into directories pointed to by symbolic links, as if they were real directories.

The walker is protected against infinite loops caused by cyclic symlinks. It uses the canonical path and a hash set of visited directories (via BLAKE3 hashing) to ensure each directory is only visited once, even if it is linked multiple times.

§Example
use fs_walk::WalkOptions;

// Create a walker that follows symlinks
let mut options = WalkOptions::new();
options.follow_symlink();

// Now symlinks to directories will be traversed
for entry in options.walk("./").flatten() {
    println!("{:?}", entry);
}
§Safety

While the walker is protected against symlink loops, be cautious when enabling this option in untrusted directories, as it may still expose your program to other symlink-based attacks.

Source

pub fn max_depth(&mut self, depth: u64) -> &mut Self

Configure a maximum depth for the walker. If no depth is specified the walker will walk through all directories in a BFS way.

§Example
use fs_walk::WalkOptions;
use std::path::Path;

for p in WalkOptions::new().max_depth(0).walk("./").flatten() {
    assert_eq!(p.parent(), Some(Path::new(".")));
}
Source

pub fn extension<S: AsRef<str>>(&mut self, ext: S) -> &mut Self

Configure walker to return only files matching file extension. For any file, if Path::extension is not None it will be checked against ext. This function can be called several times to return files matching one of the desired extension.

See Path::extension for the correct way to specify ext.

§Example
use fs_walk;
use std::path::PathBuf;
use std::ffi::OsStr;

let wk = fs_walk::WalkOptions::new()
    .files()
    .extension("o")
    .extension("rs")
    .walk("./");

let paths: Vec<PathBuf> = wk.flatten().collect();

assert!(paths.iter().any(|p| p.extension() == Some(OsStr::new("o"))));
assert!(paths.iter().any(|p| p.extension() == Some(OsStr::new("rs"))));
assert!(!paths.iter().any(|p| p.extension() == Some(OsStr::new("toml"))));
assert!(!paths.iter().any(|p| p.extension() == Some(OsStr::new("lock"))));
Source

pub fn ends_with<S: AsRef<str>>(&mut self, pat: S) -> &mut Self

Configure walker to return files ending with pattern pat For any file, if Path::to_string_lossy ends with pattern pat it is going to be returned.

This method can be used to match path with double extensions (i.e. .txt.gz) without having to do manual pattern matching on walker’s output.

See str::ends_with for more detail about matching

§Example
use fs_walk;
use std::path::PathBuf;
use std::ffi::OsStr;

let wk = fs_walk::WalkOptions::new()
    .files()
    .extension("o")
    // we can put . here not in extension
    // can be used to match path with double extensions
    .ends_with(".rs")
    .walk("./");

let paths: Vec<PathBuf> = wk.flatten().collect();

assert!(paths.iter().any(|p| p.extension() == Some(OsStr::new("o"))));
assert!(paths.iter().any(|p| p.extension() == Some(OsStr::new("rs"))));
assert!(!paths.iter().any(|p| p.extension() == Some(OsStr::new("toml"))));
assert!(!paths.iter().any(|p| p.extension() == Some(OsStr::new("lock"))));
Source

pub fn name<S: AsRef<str>>(&mut self, name: S) -> &mut Self

Configure walker to return only paths with Path::file_name matching name

§Example
use fs_walk;
use std::path::PathBuf;
use std::ffi::OsStr;

let wk = fs_walk::WalkOptions::new()
    .name("lib.rs")
    .walk("./");

let paths: Vec<PathBuf> = wk.flatten().collect();

assert!(paths.iter().all(|p| p.file_name() == Some(OsStr::new("lib.rs"))));
assert!(paths.iter().all(|p| p.is_file()));
Source

pub fn sort(&mut self, value: bool) -> &mut Self

Sorts entries at every directory listing

Source

pub fn walk<P: AsRef<Path>>(&self, p: P) -> Walker

Turns self into a Walker

Trait Implementations§

Source§

impl Clone for WalkOptions

Source§

fn clone(&self) -> WalkOptions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WalkOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WalkOptions

Source§

fn default() -> WalkOptions

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.