recv-dir

Crates.iorecv-dir
lib.rsrecv-dir
version0.3.1
sourcesrc
created_at2022-05-31 04:59:57.090661
updated_at2024-04-14 10:56:09.031896
descriptionRecursive directory walker
homepagehttps://gitlab.com/Kores/rust-experiments/recv-dir
repositoryhttps://gitlab.com/Kores/rust-experiments/recv-dir
max_upload_size
id597247
size57,660
Jonathan (JonathanxD)

documentation

https://docs.rs/recv-dir/

README

recv-dir

Simple recursive directory traversal.

Description

Traverse directories recursively and conditionally using iterators.

Example

Visit all directories

fn main() {
    use recv_dir::RecursiveDirIterator;
    let dir = RecursiveDirIterator::from_root("test_dir").unwrap();

    for entry in dir {
        println!("{:?}", entry);
    }
}

Ignore symlinks

fn main() {
    use std::path::Path;
    use recv_dir::RecursiveDirIterator;
    let dir = RecursiveDirIterator::with_closure_filter("test_dir", |dir: &Path| !dir.is_symlink()).unwrap();

    for entry in dir {
        println!("{:?}", entry);
    }
}

Control depth

Visits only the files in test_dir:

fn main() {
    use std::path::{Path, PathBuf};
    use recv_dir::RecursiveDirIterator;
    let root = PathBuf::from("test_dir");
    let ancestors = root.ancestors().count();
    let dir = RecursiveDirIterator::with_closure_filter(root, |dir: &Path| dir.ancestors().count() - ancestors <= 1).unwrap();

    for entry in dir {
        println!("{:?}", entry);
    }
}

Visits the files in test_dir and in first-level subdirectories:

fn main() {
    use std::path::{Path, PathBuf};
    use recv_dir::RecursiveDirIterator;
    let root = PathBuf::from("test_dir");
    let ancestors = root.ancestors().count();
    let dir = RecursiveDirIterator::with_closure_filter(root, |dir: &Path| dir.ancestors().count() - ancestors <= 2).unwrap();

    for entry in dir {
        println!("{:?}", entry);
    }
}

You can also compose the filters:

fn main() {
    use std::num::NonZeroUsize;
    use std::path::{Path, PathBuf};
    use recv_dir::{Filter, MaxDepth, NoSymlink, RecursiveDirIterator};
    let root = PathBuf::from("test_dir");
    let dir = RecursiveDirIterator::with_filter(
        &root,
        NoSymlink.and(MaxDepth::new(&root, NonZeroUsize::new(2).unwrap())),
    ).unwrap();

    for entry in dir {
        println!("{:?}", entry);
    }
}

Read the documentation for more information

Commit count: 17

cargo fmt