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.
///|
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")
}///|
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")
}///|
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")
}
}///|
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")
}
}///|
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")
}
}type Directory[Component]pub struct File[Component] {
base : Component
extension : String
}fn UnixPath::from(directory~ : Directory[UnixPathComponent], file? : File[UnixPathComponent]) -> UnixPathtype UnixPathComponentimpl PlatformPath for UnixPathComponentfn is_system_call_valid(s : String) -> Boolimpl Eq for UnixPathComponentimpl Hash for UnixPathComponentimpl Show for UnixPathComponentimpl ToJson for UnixPathComponentpub struct WinPath {
disk : Char
directory : Directory[WindowsPathComponent]
file : File[WindowsPathComponent]?
}fn WinPath::from(disk~ : Char, directory~ : Directory[WindowsPathComponent], file? : File[WindowsPathComponent]) -> WinPath raisetype WindowsPathComponentimpl PlatformPath for WindowsPathComponentfn is_system_call_valid(s : String) -> Boolimpl Eq for WindowsPathComponentimpl Hash for WindowsPathComponentimpl Show for WindowsPathComponentimpl ToJson for WindowsPathComponentInstall
Download zipA 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.