Expand description
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.
§Features
Entryis 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 to10kand the maximum depth of traversal to100. These limit can be changed with the methodsmax_entriesandmax_depth.Entrycan be used to build objects that can be serialized e.g. as Json, due to it being in-memory.- Symbolic links are skipped.
§Use
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:
- use the method
skip_dottedto skip dotted files or directories during traversal. - The method
skip_directoriesallows to skip directories. - Use
max_depthto stop the traversal at a fixed depth. - Use
max_entriesto 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.
To use this crate, add dir_walker as a dependency to your project’s
Cargo.toml:
[dependencies]
dir_walker = "0.1.9"§Example: Print the nested structure
let skip = ["./target"];
let max_entries = 8;
let max_depth = 3;
let entries = Walker::new("./")
.max_entries(max_entries) // optional
.max_depth(max_depth) // optional
.skip_directories(&skip) // optional
.skip_dotted() // optional
.walk_dir()
.unwrap();
// print the directory tree as nested objects
println!("entries:\n{:?}", &entries);
§Example: Get a flat list of entries
let max_entries = 4;
let entries = Walker::new("./")
.max_entries(max_entries)
.walk_dir()
.unwrap();
// into_iter() iterates over a flat "list" of entries.
// Print a depth first representation of the root directory
let items = entries.into_iter().inspect(|e| println!("{e:?}")).collect::<Vec<EntryItem>>();
Structs§
- Entry
- Represents a directory with its sub-directories and files. The depth field represents the depth of this entry with respect to the root path.
- Entry
Item - Helper type that is returned when iterating over an
Entry. - Walker
- Configure this builder and use the method
walk_dirto traverse the root path. SeeWalker::new()for examples.