dir_walker

Crates.iodir_walker
lib.rsdir_walker
version0.1.9
sourcesrc
created_at2023-11-28 18:44:27.101961
updated_at2023-12-08 18:06:12.963832
descriptionTraverse a directory recursively preserving compatbility with the standard library
homepage
repositoryhttps://github.com/gabrielecodes/dir_walker
max_upload_size
id1052260
size35,287
Gabriele Costanza (gabrielecodes)

documentation

https://docs.rs/dir_walker/latest/dir_walker/

README

dir_walker

github build status crates.io docs.rs

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.
  • 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.

Add this crate to your project:

[dependencies]
dir_walker = "0.1.9"

Examples

Usage examples are in the tests folder.

Minimal Example

    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:?}"));

Using options

    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 }
Commit count: 38

cargo fmt