Crates.io | recv-dir |
lib.rs | recv-dir |
version | 0.3.1 |
source | src |
created_at | 2022-05-31 04:59:57.090661 |
updated_at | 2024-04-14 10:56:09.031896 |
description | Recursive directory walker |
homepage | https://gitlab.com/Kores/rust-experiments/recv-dir |
repository | https://gitlab.com/Kores/rust-experiments/recv-dir |
max_upload_size | |
id | 597247 |
size | 57,660 |
Simple recursive directory traversal.
Traverse directories recursively and conditionally using iterators.
fn main() {
use recv_dir::RecursiveDirIterator;
let dir = RecursiveDirIterator::from_root("test_dir").unwrap();
for entry in dir {
println!("{:?}", entry);
}
}
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);
}
}
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