Sign in

    path

    A cross-platform path manipulation library for MoonBit that provides robust handling of both Unix and Windows file system paths with automatic format detection and type-safe path operations.

    path
    filepath
    filesystem
    cross-platform
    unix
    windows
    parsing
    file-handling
    path-manipulation
    directory
    Download zip
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    12 months ago
    Downloads
    60

    No dependencies

    #illusory0x0/path

    A MoonBit library for cross-platform path manipulation, supporting both Unix and Windows path styles with type-safe operations.

    #Examples

    #PathBuilder Usage

    Demonstrates building paths using PathBuilder from a relative path.

    ///|
    test {
    let path = @path.Path::parse("./projects/")
    let builder = @path.PathBuilder::from_path(path)
    builder.set_extension("md")
    builder.set_basename("README")
    inspect(builder, content="projects/README.md")
    builder.add_directory("proj_1")

    // pop last file, and add new directory
    inspect(builder, content="projects/proj_1/")
    }

    Demonstrates building paths using PathBuilder from a Windows absolute path.

    ///|
    test {
    let path = @path.Path::parse("C:\\projects\\")
    let builder = @path.PathBuilder::from_path(path)
    builder.set_file(base="README", extension="md")
    inspect(builder, content="C:\\projects\\README.md")
    builder.add_directory("proj_1")

    // pop last file, and add new directory
    inspect(builder, content="C:\\projects\\proj_1\\")
    }

    #Simple Usage

    Shows basic path operations like extracting file name, directory, and prefix.

    ///|
    test "Simple String Operations" {
    let path = @path.Path::parse("C:\\Users\\username\\Documents\\file.txt")
    inspect(path.file(), content="Some(file.txt)")
    inspect(path.directory()[:].join("\\"), content="Users\\username\\Documents")
    inspect(path.prefix(), content="C:\\")
    }

    #Type Safe Operations

    Illustrates type-safe access to directory components for Unix paths.

    ///|
    test "type safe directory components" {
    let path = @path.Path::parse("/home/username/proj/src/")
    guard path is UPath(path) else { panic() }
    let parent_dir : ArrayView[@path.UnixPathComponent] = path.directory[0:path.directory.length() -
    1]
    @json.inspect(parent_dir, content=[
    ["UnixPathComponent", "home"],
    ["UnixPathComponent", "username"],
    ["UnixPathComponent", "proj"],
    ])
    }

    #Path Kinds Examples

    Examples of different path kinds: relative and absolute for Unix and Windows.

    ///|
    test "Unix relative path" {
    let path = @path.Path::parse("home/username/proj/src/README.mbt.md")
    inspect(path.directory(), content="home/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="false")
    inspect(path.is_relative(), content="true")
    inspect(path.is_root_path(), content="false")
    }

    ///|
    test "Unix absolute path" {
    let path = @path.Path::parse("/home/username/proj/src/README.mbt.md")
    inspect(path.directory(), content="home/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="true")
    inspect(path.is_relative(), content="false")
    inspect(path.is_root_path(), content="true")
    }

    ///|
    test "Windows relative path" {
    let path = @path.Path::parse("Users\\username\\proj\\src\\README.mbt.md")
    // This is used for Debug output
    inspect(path.directory(), content="Users/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="false")
    inspect(path.is_relative(), content="true")
    inspect(path.is_root_path(), content="false")
    }

    ///|
    test "Windows absolute path" {
    let path = @path.Path::parse("C:\\Users\\username\\proj\\src\\README.mbt.md")
    // This is used for Debug output
    inspect(path.directory(), content="Users/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="true")
    inspect(path.is_relative(), content="false")
    inspect(path.is_root_path(), content="true")
    }

    ///|
    test "Windows relative root path" {
    let path = @path.Path::parse("\\Users\\username\\proj\\src\\README.mbt.md")
    // This is used for Debug output
    inspect(path.directory(), content="Users/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="false")
    inspect(path.is_relative(), content="true")
    inspect(path.is_root_path(), content="true")
    }

    #illusory0x0/path

    A MoonBit library for cross-platform path manipulation, supporting both Unix and Windows path styles with type-safe operations.

    #Examples

    #PathBuilder Usage

    Demonstrates building paths using PathBuilder from a relative path.

    ///|
    test {
    let path = @path.Path::parse("./projects/")
    let builder = @path.PathBuilder::from_path(path)
    builder.set_extension("md")
    builder.set_basename("README")
    inspect(builder, content="projects/README.md")
    builder.add_directory("proj_1")

    // pop last file, and add new directory
    inspect(builder, content="projects/proj_1/")
    }

    Demonstrates building paths using PathBuilder from a Windows absolute path.

    ///|
    test {
    let path = @path.Path::parse("C:\\projects\\")
    let builder = @path.PathBuilder::from_path(path)
    builder.set_file(base="README", extension="md")
    inspect(builder, content="C:\\projects\\README.md")
    builder.add_directory("proj_1")

    // pop last file, and add new directory
    inspect(builder, content="C:\\projects\\proj_1\\")
    }

    #Simple Usage

    Shows basic path operations like extracting file name, directory, and prefix.

    ///|
    test "Simple String Operations" {
    let path = @path.Path::parse("C:\\Users\\username\\Documents\\file.txt")
    inspect(path.file(), content="Some(file.txt)")
    inspect(path.directory()[:].join("\\"), content="Users\\username\\Documents")
    inspect(path.prefix(), content="C:\\")
    }

    #Type Safe Operations

    Illustrates type-safe access to directory components for Unix paths.

    ///|
    test "type safe directory components" {
    let path = @path.Path::parse("/home/username/proj/src/")
    guard path is UPath(path) else { panic() }
    let parent_dir : ArrayView[@path.UnixPathComponent] = path.directory[0:path.directory.length() -
    1]
    @json.inspect(parent_dir, content=[
    ["UnixPathComponent", "home"],
    ["UnixPathComponent", "username"],
    ["UnixPathComponent", "proj"],
    ])
    }

    #Path Kinds Examples

    Examples of different path kinds: relative and absolute for Unix and Windows.

    ///|
    test "Unix relative path" {
    let path = @path.Path::parse("home/username/proj/src/README.mbt.md")
    inspect(path.directory(), content="home/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="false")
    inspect(path.is_relative(), content="true")
    inspect(path.is_root_path(), content="false")
    }

    ///|
    test "Unix absolute path" {
    let path = @path.Path::parse("/home/username/proj/src/README.mbt.md")
    inspect(path.directory(), content="home/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="true")
    inspect(path.is_relative(), content="false")
    inspect(path.is_root_path(), content="true")
    }

    ///|
    test "Windows relative path" {
    let path = @path.Path::parse("Users\\username\\proj\\src\\README.mbt.md")
    // This is used for Debug output
    inspect(path.directory(), content="Users/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="false")
    inspect(path.is_relative(), content="true")
    inspect(path.is_root_path(), content="false")
    }

    ///|
    test "Windows absolute path" {
    let path = @path.Path::parse("C:\\Users\\username\\proj\\src\\README.mbt.md")
    // This is used for Debug output
    inspect(path.directory(), content="Users/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="true")
    inspect(path.is_relative(), content="false")
    inspect(path.is_root_path(), content="true")
    }

    ///|
    test "Windows relative root path" {
    let path = @path.Path::parse("\\Users\\username\\proj\\src\\README.mbt.md")
    // This is used for Debug output
    inspect(path.directory(), content="Users/username/proj/src/")
    inspect(path.file().unwrap(), content="README.mbt.md")
    inspect(path.is_absolute(), content="false")
    inspect(path.is_relative(), content="true")
    inspect(path.is_root_path(), content="true")
    }

    UnixDirectory

    typealias Directory[UnixPathComponent] as UnixDirectory

    UnixFile

    typealias File[UnixPathComponent] as UnixFile

    WinDirectory

    typealias Directory[WinPathComponent] as WinDirectory

    WinFile

    typealias File[WinPathComponent] as WinFile

    IsString

    trait IsString

    impl IsString for String

    PlatformPath

    trait PlatformPath

    PathError

    pub(all) suberror PathError {
    PathError(Int, String)
    }

    impl Show for PathError

    Directory

    type Directory[Component]

    impl Eq for Directory[Component]
    impl Hash for Directory[Component]
    impl Show for Directory[A]
    impl ToJson for Directory[Component]

    Directory::iter

    fn[T] Directory::iter(self : Directory[T]) -> Iter[T]

    Directory::length

    fn[T] Directory::length(self : Directory[T]) -> Int

    Directory::new

    fn[A : PlatformPath] Directory::new(components : ArrayView[StringView]) -> Directory[A] raise PathError

    Directory::op_get

    fn[T] Directory::op_get(self : Directory[T], index : Int) -> T

    Directory::sub

    #alias("_[_:_]")
    fn[T] Directory::sub(self : Directory[T], start? : Int, end? : Int) -> ArrayView[T]

    File

    pub struct File[Component] {
    base : Component
    extension : Component?
    }

    impl Eq for File[Component]
    impl Hash for File[Component]
    impl Show for File[A]
    impl ToJson for File[Component]

    File::extension

    fn[A : IsString] File::extension(self : File[A]) -> String?

    File::new

    fn[A : PlatformPath] File::new(base~ : StringView, extension? : StringView) -> File[A] raise PathError

    Path

    pub(all) enum Path {
    WPath(WinPath)
    UPath(UnixPath)
    }

    impl Eq for Path
    impl Hash for Path
    impl Show for Path
    impl ToJson for Path

    Path::directory

    fn Path::directory(self : Path) -> Directory[String]

    Path::file

    fn Path::file(self : Path) -> File[String]?

    Path::is_absolute

    fn Path::is_absolute(self : Path) -> Bool

    Path::is_relative

    fn Path::is_relative(self : Path) -> Bool

    Path::is_root_path

    fn Path::is_root_path(self : Path) -> Bool

    Path::parse

    fn Path::parse(path : StringView) -> Path raise PathError

    Parsing path from string used prefix based detection to determine the path format style (Windows/Unix)

    Path::prefix

    fn Path::prefix(self : Path) -> String

    PathBuilder

    type PathBuilder

    impl Show for PathBuilder

    PathBuilder::add_directory

    fn PathBuilder::add_directory(self : PathBuilder, directory : StringView) -> Unit raise PathError

    When PathBuilder contains filename add_directory would pop this filename, and then add directory in its parent directory

    PathBuilder::from_path

    fn PathBuilder::from_path(path : Path) -> PathBuilder

    PathBuilder::set_basename

    fn PathBuilder::set_basename(self : PathBuilder, basename : StringView) -> Unit raise PathError

    PathBuilder::set_extension

    fn PathBuilder::set_extension(self : PathBuilder, extension : StringView) -> Unit raise PathError

    PathBuilder::set_file

    fn PathBuilder::set_file(self : PathBuilder, base~ : StringView, extension~ : StringView) -> Unit raise PathError

    PathBuilder::to_path

    fn PathBuilder::to_path(self : PathBuilder) -> Path

    UnixPath

    pub struct UnixPath {
    is_absolute : Bool
    directory : Directory[UnixPathComponent]
    file : File[UnixPathComponent]?
    }
    impl Eq for UnixPath
    impl Hash for UnixPath
    impl Show for UnixPath
    impl ToJson for UnixPath

    UnixPath::is_absolute

    #alias(is_root_path)
    fn UnixPath::is_absolute(self : UnixPath) -> Bool

    UnixPath::is_relative

    fn UnixPath::is_relative(self : UnixPath) -> Bool

    UnixPath::new

    fn UnixPath::new(is_absolute? : Bool, directory~ : Directory[UnixPathComponent], file? : File[UnixPathComponent]) -> UnixPath

    UnixPathComponent

    type UnixPathComponent

    WinPath

    pub struct WinPath {
    prefix : WinPrefix
    directory : Directory[WinPathComponent]
    file : File[WinPathComponent]?
    }
    impl Eq for WinPath
    impl Hash for WinPath
    impl Show for WinPath
    impl ToJson for WinPath

    WinPath::is_absolute

    fn WinPath::is_absolute(self : WinPath) -> Bool

    WinPath::is_relative

    fn WinPath::is_relative(self : WinPath) -> Bool

    WinPath::is_root_path

    fn WinPath::is_root_path(self : WinPath) -> Bool

    WinPath::new

    fn WinPath::new(prefix? : WinPrefix, directory~ : Directory[WinPathComponent], file? : File[WinPathComponent]) -> WinPath

    WinPathComponent

    type WinPathComponent

    WinPrefix

    pub(all) enum WinPrefix {
    Root
    VerbatimSymlink(String)
    VerbatimUNC(String, String)
    VerbatimVolumeLetter(Char)
    VerbatimVolumeGUID(String)
    DeviceNS(String)
    UNC(String, String)
    VolumeLetter(Char)
    None
    }

    Verbatim Prefix

    For file I/O, the "\?" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system.

    Windows Encoding

    There is no need to perform any Unicode normalization on path and file name strings for use by the Windows file I/O API functions because the file system treats path and file names as an opaque sequence of WCHARs. Any normalization that your application requires should be performed with this in mind, external of any calls to related Windows file I/O API functions.

    Relative Paths Cannot use Verbatim Prefix

    Because you cannot use the "\?" prefix with a relative path, relative paths are always limited to a total of MAX_PATH characters.

    impl Eq for WinPrefix
    impl Hash for WinPrefix
    impl Show for WinPrefix
    impl ToJson for WinPrefix

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io