Crate dir_walker

Crate dir_walker 

Source
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

  • 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, 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_dotted to skip dotted files or directories during traversal.
  • The method skip_directories allows to skip directories.
  • Use max_depth to stop the traversal at a fixed depth.
  • Use 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.

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.
EntryItem
Helper type that is returned when iterating over an Entry.
Walker
Configure this builder and use the method walk_dir to traverse the root path. See Walker::new() for examples.