Expand description
Β§swdir
Swiftly traverse and scan directories in Rust.
swdir is a small, focused crate for walking and listing
directories. It supplies the raw material for CLIs and GUI
Directory Tree widgets β path listings, recursive walks, typed
entries β and deliberately stops there.
Β§Two entry points
| Use case | API |
|---|---|
| Recursive walk (batch tools, CLIs) | Swdir::walk |
| Lazy-loading one-folder scan (GUIs) | scan_dir / scan_dir_with_options |
Both share the same SortOrder when reproducible ordering matters.
Β§Quick start β recursive walk
use swdir::Swdir;
let report = Swdir::new().root_path("/some/path").walk();
// -> WalkReport { tree: DirNode, errors: Vec<WalkError> }
let paths = report.tree.flatten_paths();Β§Quick start β lazy loading (for Directory Tree widgets)
use std::path::Path;
use swdir::{ScanOptions, SortOrder, scan_dir_with_options};
let opts = ScanOptions::new(SortOrder::NameAscDirsFirst);
let entries = scan_dir_with_options(Path::new("/some/folder"), &opts)?;
for entry in &entries {
// entry.display_name() β &OsStr, no allocation
// entry.is_dir() β uses the cached FileType, no syscall
}Β§With a filter (default feature)
use swdir::{FilterRule, Recurse, Swdir, SwdirError};
let report = Swdir::new()
.root_path("/some/path")
.recurse(Recurse::Unlimited)
.filter(FilterRule::extension_allowlist(["md", "rs"])?)
.walk();Swdir::new() installs one rule by default (FilterRule::SkipHidden)
so dotfiles donβt appear in results. Call Swdir::clear_filters to
see them.
Β§Error handling
Swdir::walk returns a WalkReport with errors collected β
unreadable directories never cause panics or stderr noise:
let report = Swdir::new().root_path(".").walk();
for err in &report.errors {
eprintln!("warn: {err}");
}scan_dir / scan_dir_with_options are atomic instead: the
first I/O failure returns Err(ScanError::Io) without
partial results.
Β§Further reading
The crate ships a full set of guides and reference pages in
docs/ in the repository:
- Getting started β
docs/getting-started.md - Recursive walks β
docs/guides/recursive-walk.md - Lazy loading for GUIs β
docs/guides/lazy-loading.md - Filter rules (catalog +
include/descendmodel) βdocs/reference/filter-rules.md - Sort order β
docs/reference/sort-order.md - Error handling β
docs/reference/error-handling.md - Design notes β
docs/design-notes.md - Migration notes β
docs/migration/
StructsΒ§
- Decision
- What to do with a given entry.
- DirEntry
- An owned directory entry returned by
crate::scan_dir. - DirNode
- directory tree
- DirNode
Count - Filter
Context - Context supplied to a filter rule when evaluating a candidate entry.
- Scan
Options - Options for
crate::scan_dir_with_options. - Swdir
- Configures and runs a recursive directory scan.
- Walk
Report - Result of a
crate::Swdir::walkcall.
EnumsΒ§
- Entry
Kind - Kind of filesystem entry. Mirrors
std::fs::FileTypebut as a plain, pattern-matchable enum. - Filter
Rule - A single filter condition.
- Recurse
- Recursion policy controlling how deeply
crate::Swdir::walkdescends. - Scan
Error - Error returned by
crate::scan_dir. - Sort
Order - How entries are ordered in walk / scan results.
- Swdir
Error - Errors returned by builder-style constructors and validators.
- Walk
Error - An error that occurred while walking the tree.
FunctionsΒ§
- scan_
dir - Scan a single directory non-recursively and return its direct entries,
in the order the OSβs
readdiryields them. - scan_
dir_ with_ options - Scan a single directory non-recursively and return its direct entries,
ordered per
ScanOptions::sort_order.