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.1.0
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    60

    No dependencies

    #MoonBit Path Library

    A cross-platform path manipulation library for MoonBit that provides robust handling of both Unix and Windows file system paths.

    #Features

    • Cross-platform: Automatically detects and handles both Unix and Windows path formats
    • Type-safe: Strong typing with separate types for Unix and Windows paths
    • Comprehensive parsing: Handles drive letters, directories, files, and extensions
    • Path manipulation: Easy construction and deconstruction of path components
    • Standards compliant: Follows platform-specific path conventions

    #Quick Start

    ///|
    test "basic usage" {
    // Parse different path formats automatically
    let unix_path = @path.Path::parse("/home/user/document.txt")
    let windows_path = @path.Path::parse("C:\\Users\\John\\file.pdf")

    // The library automatically detects the format
    inspect(unix_path.to_string(), content="/home/user/document.txt")
    inspect(windows_path.to_string(), content="C:\\Users\\John\\file.pdf")
    }

    #API Overview

    #Main Types

    • Path: Union type representing either Unix or Windows paths
    • UnixPath: Unix-style paths (e.g., /home/user/file.txt)
    • WinPath: Windows-style paths (e.g., C:\\Users\\file.txt)
    • File[Component]: Represents a file with base name and extension
    • Directory[Component]: Represents a directory structure

    #Path Parsing

    ///|
    test "path parsing examples" {
    // Unix paths
    let unix_file = @path.Path::parse("/etc/config/settings.conf")
    let _unix_dir = @path.Path::parse("/home/user/documents")
    let _unix_root = @path.Path::parse("/")

    // Windows paths
    let win_file = @path.Path::parse("D:\\Documents\\readme.txt")
    let _win_dir = @path.Path::parse("C:\\Program Files\\MyApp")
    let _win_root = @path.Path::parse("E:")

    // Relative paths (treated as Unix by default)
    let relative = @path.Path::parse("relative/path/file.txt")
    inspect(unix_file.to_string(), content="/etc/config/settings.conf")
    inspect(win_file.to_string(), content="D:\\Documents\\readme.txt")
    inspect(relative.to_string(), content="/relative/path/file.txt")
    }

    #Working with Path Components

    ///|
    test "path components" {
    let path = @path.Path::parse("C:\\Users\\John\\document.pdf")
    match path {
    @path.WPath(win_path) => {
    // Access drive letter
    inspect(win_path.disk, content="C")

    // Access directory components
    inspect(win_path.directory.length(), content="2")
    inspect(win_path.directory[0].to_string(), content="Users")
    inspect(win_path.directory[1].to_string(), content="John")

    // Access file information
    match win_path.file {
    Some(file) => {
    inspect(file.base.to_string(), content="document")
    inspect(file.extension, content="pdf")
    }
    None => fail("Expected file")
    }
    }
    _ => fail("Expected Windows path")
    }
    }

    #Error Handling

    ///|
    test "error handling" {
    // Empty paths are invalid
    let result = try? @path.Path::parse("")
    match result {
    Err(_) => inspect("empty path rejected", content="empty path rejected")
    Ok(_) => fail("Empty path should be rejected")
    }
    }

    #Platform Detection

    The library automatically detects Windows paths by looking for drive letter patterns (e.g., "C:", "D:\"). All other paths are treated as Unix-style paths.

    ///|
    test "platform detection" {
    // These are detected as Windows paths
    let win1 = @path.Path::parse("C:")
    let _win2 = @path.Path::parse("D:\\folder")
    let _win3 = @path.Path::parse("z:\\file.txt")

    // These are detected as Unix paths
    let unix1 = @path.Path::parse("/")
    let _unix2 = @path.Path::parse("/home")
    let _unix3 = @path.Path::parse("relative/path")
    let _unix4 = @path.Path::parse("file.txt")

    // Verify detection
    match (win1, unix1) {
    (@path.WPath(_), @path.UPath(_)) =>
    inspect("detection works", content="detection works")
    _ => fail("Detection failed")
    }
    }

    #Use Cases

    • File system operations: Parse and manipulate file paths before system calls
    • Cross-platform applications: Handle paths consistently across different operating systems
    • Path validation: Ensure path strings are well-formed before processing
    • Path construction: Build paths programmatically with proper formatting
    • Configuration management: Parse file paths from configuration files

    #License

    Apache-2.0

    UnixDirectory

    typealias Directory[UnixPathComponent] as UnixDirectory

    UnixFile

    typealias File[UnixPathComponent] as UnixFile

    WinDirectory

    typealias Directory[WindowsPathComponent] as WinDirectory

    WinFile

    typealias File[WindowsPathComponent] as WinFile

    PlatformPath

    trait PlatformPath

    Directory

    type Directory[Component]

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

    Directory::from_array

    fn[T : PlatformPath] Directory::from_array(components : Array[String]) -> Directory[T]

    Directory::from_fixed_array

    fn[T : PlatformPath] Directory::from_fixed_array(components : FixedArray[String]) -> Directory[T]

    Directory::iter

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

    Directory::length

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

    Directory::op_get

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

    File

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

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

    File::parse

    fn[Component : PlatformPath] File::parse(s : StringView) -> File[Component] raise

    Parse a filename string into a File object Splits the filename at the last dot to separate base name and extension Examples:
    • "readme.txt" -> File { base: "readme", extension: "txt" }
    • "config" -> File { base: "config", extension: "" }
    • "archive.tar.gz" -> File { base: "archive.tar", extension: "gz" }
    • ".hidden" -> File { base: "", extension: "hidden" }

    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::parse

    fn Path::parse(s : StringView) -> Path raise

    UnixPath

    pub struct UnixPath {
    directory : Directory[UnixPathComponent]
    file : File[UnixPathComponent]?
    }

    impl Eq for UnixPath
    impl Hash for UnixPath
    impl Show for UnixPath
    impl ToJson for UnixPath

    UnixPath::from

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

    UnixPath::parse

    fn UnixPath::parse(s : StringView) -> UnixPath raise

    Parse a Unix path string into UnixPath Format: "/path/to/file.ext" or "/path/to/"

    UnixPathComponent

    type UnixPathComponent

    WinPath

    pub struct WinPath {
    disk : Char
    directory : Directory[WindowsPathComponent]
    file : File[WindowsPathComponent]?
    }

    impl Eq for WinPath
    impl Hash for WinPath
    impl Show for WinPath
    impl ToJson for WinPath

    WinPath::from

    fn WinPath::from(disk~ : Char, directory~ : Directory[WindowsPathComponent], file? : File[WindowsPathComponent]) -> WinPath raise

    disk should be valid drive letter (A-Z, a-z)

    WinPath::parse

    fn WinPath::parse(s : StringView) -> WinPath raise

    Parse a Windows path string into WinPath Format: "C:\path\to\file.ext" or "C:\path\to"

    WindowsPathComponent

    type WindowsPathComponent

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io