walkdir

    A deterministic walkdir library for MoonBit.

    walkdir
    filesystem
    fs
    Download zip
    Author
    Version
    0.1.4
    License
    MIT
    Last updated
    5 months ago
    Downloads
    152

    #walkdir

    Deterministic directory walking for MoonBit.

    ///|
    test "walkdir basic example" {
    let files = @walkdir.walk_files("testdata/walk_fixture", max_depth=1)
    assert_eq(files.length(), 2)
    }

    WalkError

    pub(all) suberror WalkError {
    NotFound(String)
    NotDirectory(String)
    } derive(
    Debug
    )

    Describes errors raised before traversal starts.
    impl Show for WalkError

    DirEntry

    pub(all) struct DirEntry {
    path : String
    depth : Int
    kind : EntryKind
    } derive(Eq, ToJson,
    Debug
    )

    Represents one item returned by directory traversal.

    path is the full path returned by the walker, depth is the nesting level relative to the requested root, and kind tells whether the entry is a file or directory.
    impl Show for DirEntry

    DirEntry::is_dir

    fn DirEntry::is_dir(entry : DirEntry) -> Bool

    Returns true when this entry represents a directory.

    Example

    test "DirEntry::is_dir example" {
    let entry = @walkdir.DirEntry::{ path: "demo", depth: 0, kind: Dir }
    assert_true(entry.is_dir())
    }

    DirEntry::is_file

    fn DirEntry::is_file(entry : DirEntry) -> Bool

    Returns true when this entry represents a file.

    Example

    test "DirEntry::is_file example" {
    let entry = @walkdir.DirEntry::{ path: "demo.txt", depth: 1, kind: File }
    assert_true(entry.is_file())
    }

    EntryKind

    pub(all) enum EntryKind {
    File
    Dir
    } derive(Eq, ToJson,
    Debug
    )

    Describes whether a walked entry is a file or a directory.
    impl Show for EntryKind

    walk

    fn walk(root : String, include_root? : Bool, include_dirs? : Bool, max_depth? : Int, sort? : Bool) -> Array[DirEntry] raise

    Walks a directory tree in deterministic depth-first order.

    Children are sorted lexicographically by default so repeated runs produce the same output. Set include_root=false to skip the root entry and include_dirs=false to emit only files. max_depth=0 keeps traversal at the root level.

    Parameters

    • root: The directory to traverse.
    • include_root: Whether the returned array should include the root itself.
    • include_dirs: Whether directories should be included in the returned array.
    • max_depth: The maximum depth to descend from root.
    • sort: Whether to sort each directory's children before traversal.

    Errors

    • Raises WalkError::NotFound(root) when root does not exist.
    • Raises WalkError::NotDirectory(root) when root is not a directory.

    Example

    test "walk example" {
    let entries = @walkdir.walk("testdata/walk_fixture", max_depth=1)
    assert_eq(entries.length(), 4)
    }

    walk_files

    fn walk_files(root : String, max_depth? : Int, sort? : Bool) -> Array[String] raise

    Walks a directory tree and returns only file paths.

    This is a convenience wrapper around walk(..., include_dirs=false,include_root=false) for file-oriented use cases such as indexing or filtering.

    Parameters

    • root: The directory to traverse.
    • max_depth: The maximum depth to descend from root.
    • sort: Whether to sort each directory's children before traversal.

    Errors

    Raises the same errors as walk.

    Example

    test "walk_files example" {
    let files = @walkdir.walk_files("testdata/walk_fixture")
    assert_eq(files.length(), 4)
    }

    Source Files