| Crates.io | dir_walker |
| lib.rs | dir_walker |
| version | 0.1.9 |
| created_at | 2023-11-28 18:44:27.101961+00 |
| updated_at | 2023-12-08 18:06:12.963832+00 |
| description | Traverse a directory recursively preserving compatbility with the standard library |
| homepage | |
| repository | https://github.com/gabrielecodes/dir_walker |
| max_upload_size | |
| id | 1052260 |
| size | 35,287 |
This crate provides a convenient way to traverse a directory recursively.
The objects in this crate can be used seamlessly with the standard library
types (std::fs::*) since Entry is based on std::fs::DirEntry. The
goal of this crate is to provide a file system representation with guaranteed
order and serializability allowing to send the serialized object over a network.
Entry is an in-memory recursive structure that guarantees the order of the paths
that have been found during traversal. The order is alphabetic, directories first,
files last. To limit memory consumption the default value for the maximum
number of visited entries is limited to 10k and the maximum depth of traversal to 100.
These limit can be changed with the methods max_entries and max_depth.Entry can be used to build objects that can be serialized e.g. as Json.The entry point of this crate is the Walker (builder) struct. Use the new function
passing the entry point of the traversal as input to configure the Walker.
Then several options can be specified:
skip_dotted to skip dotted files
or directories during traversal.skip_directories allows to skip directories.max_depth to stop the traversal at a fixed depth.max_entries to set the maximum number of visited entries during traversal.All of the above are optional. After setting the options use walk_dir
to traverse the file system starting from the root.
The result of the traversal is a recursively built Entry object that
exposes its information in its dirent field and lists its dependencies
in the children field.
Alternatively a flat list of entries is available to the iterator of the
Entry object.
Add this crate to your project:
[dependencies]
dir_walker = "0.1.9"
Usage examples are in the tests folder.
use dir_walker::Walker;
let root = "./";
let walker = Walker::new(root);
let entries = walker.walk_dir().unwrap();
// prints a depth first representation of the root directory
entries.into_iter().for_each(|e| println!("{e:?}"));
use dir_walker::Walker;
let root = "./";
let skip = ["./target"];
let entries = Walker::new(root)
.skip_directories(&skip)
.skip_dotted()
.walk_dir()
.unwrap();
entries.into_iter().for_each(|e| println!("{e:?}"));
prints:
EntryIterator { dirent: DirEntry("./src"), depth: 0 }
EntryIterator { dirent: DirEntry("./src/lib.rs"), depth: 1 }
EntryIterator { dirent: DirEntry("./tests"), depth: 0 }
EntryIterator { dirent: DirEntry("./tests/walkdir.rs"), depth: 1 }
EntryIterator { dirent: DirEntry("./Cargo.lock"), depth: 0 }
EntryIterator { dirent: DirEntry("./Cargo.toml"), depth: 0 }
EntryIterator { dirent: DirEntry("./README.md"), depth: 0 }