directories

    Platform-specific config, cache, and data directory paths for MoonBit (XDG on Linux, conventions on macOS/Windows)

    directories
    xdg
    config
    cache
    paths
    moonbit
    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    1 hour ago
    Downloads
    22

    #moonrockz/directories

    Platform-specific config, cache, and data directory paths for MoonBit. Port of the directories-rs API.

    Information only — this library does not create directories or check existence.

    #Installation

    moon add moonrockz/directories

    #Quick Start

    // BaseDirs: user-level cache, config, data paths
    match @directories.BaseDirs::new() {
    Some(base) => {
    base.home_dir() // e.g. /home/alice or C:\Users\alice
    base.config_dir() // e.g. /home/alice/.config or %APPDATA%
    base.cache_dir() // e.g. /home/alice/.cache or %LOCALAPPDATA%
    }
    None => () // HOME / USERPROFILE not set
    }

    // ProjectDirs: app-specific paths (qualifier, organization, application)
    match @directories.ProjectDirs::from("com", "Foo Corp", "Bar App") {
    Some(proj) => {
    proj.config_dir() // Linux: .config/bar-app, macOS: Preferences/com.Foo-Corp.Bar-App, Windows: %APPDATA%\Foo Corp\Bar App
    proj.cache_dir()
    }
    None => ()
    }

    // UserDirs: Desktop, Documents, Downloads, etc.
    match @directories.UserDirs::new() {
    Some(u) => u.download_dir()
    None => ()
    }

    #OS-specific behavior

    • Windows: Home from USERPROFILE (fallback HOME). Config/roaming from APPDATA; cache/data/local from LOCALAPPDATA. Path separator \. Project path segment: Organization\Application.
    • Unix (Linux/macOS): Home from HOME. Paths from XDG_* when set, else $HOME/.config, .cache, .local/share, etc. Path separator /. Linux project path: single lowercase segment; macOS: qualifier.Organization.Application.

    On Windows, config_dir() and config_local_dir() differ (roaming vs local); on Unix they are the same in v1.

    #Path utilities and platform

    @directories.path_sep() // "\\" on Windows, "/" on Unix
    @directories.join(["home", "alice", ".config"]) // OS-native path
    @directories.platform() // Platform::Windows | Linux | Darwin | Unknown
    @directories.is_windows() // true on Windows

    Use join() and path_sep() when building or displaying paths so they match the current OS.

    #API

    • BaseDirs::new() → Option[BaseDirs]: home_dir, cache_dir, config_dir, config_local_dir, data_dir, data_local_dir, preference_dir; optional executable_dir, runtime_dir, state_dir.
    • UserDirs::new() → Option[UserDirs]: home_dir plus optional audio_dir, desktop_dir, document_dir, download_dir, font_dir, picture_dir, public_dir, template_dir, video_dir.
    • ProjectDirs::from(qualifier, organization, application) → Option[ProjectDirs]: project_path plus cache_dir, config_dir, config_local_dir, data_dir, data_local_dir, preference_dir; optional runtime_dir, state_dir.
    • join(segments) → path string with OS separator; path_sep() → current separator; platform() / is_windows() / Platform for OS detection.

    v1 resolves paths from environment variables (HOME, USERPROFILE, APPDATA, LOCALAPPDATA, XDG_*). Native platform APIs may be added in a later version.

    #Development

    Tests run on all supported targets: mise run test:unit (wasm, wasm-gc, js, native). On Linux, the native target requires libbacktrace-dev (e.g. sudo apt install libbacktrace-dev on Ubuntu/Debian).

    BaseDirs

    pub struct BaseDirs {
    home_dir : String
    cache_dir : String
    config_dir : String
    config_local_dir : String
    data_dir : String
    data_local_dir : String
    executable_dir : String?
    preference_dir : String
    runtime_dir : String?
    state_dir : String?
    }

    Standard directories for cache, config, data, and related paths (user-level, not app-specific). On Windows: config/roaming from APPDATA, local/cache/data from LOCALAPPDATA. On Unix: XDG_* when set, else $HOME/.config, .cache, .local/share, .local/state.

    BaseDirs::cache_dir

    fn BaseDirs::cache_dir(self : BaseDirs) -> String

    Cache directory (e.g. .cache on Unix, LOCALAPPDATA on Windows).

    BaseDirs::config_dir

    fn BaseDirs::config_dir(self : BaseDirs) -> String

    Config directory (roaming on Windows: APPDATA; Unix: XDG_CONFIG_HOME or .config).

    BaseDirs::config_local_dir

    fn BaseDirs::config_local_dir(self : BaseDirs) -> String

    Local config directory (LOCALAPPDATA on Windows; same as config_dir on Unix).

    BaseDirs::data_dir

    fn BaseDirs::data_dir(self : BaseDirs) -> String

    Data directory (e.g. .local/share on Unix, LOCALAPPDATA on Windows).

    BaseDirs::data_local_dir

    fn BaseDirs::data_local_dir(self : BaseDirs) -> String

    Local data directory (same as data_dir on both platforms in v1).

    BaseDirs::executable_dir

    fn BaseDirs::executable_dir(self : BaseDirs) -> String?

    User executables directory (XDG_BIN_HOME or .local/bin on Unix); None on Windows.

    BaseDirs::home_dir

    fn BaseDirs::home_dir(self : BaseDirs) -> String

    User home directory (e.g. /home/alice or C:\Users\alice).

    BaseDirs::new

    fn BaseDirs::new() -> BaseDirs?

    Creates BaseDirs from the current environment. Returns None if home is not set (HOME on Unix, USERPROFILE or HOME on Windows).

    Example: match BaseDirs::new() { Some(base) => base.cache_dir() // e.g. /home/alice/.cache or %LOCALAPPDATA% None => ... }

    BaseDirs::preference_dir

    fn BaseDirs::preference_dir(self : BaseDirs) -> String

    Preference/settings directory (same as config_dir on Unix; APPDATA on Windows).

    BaseDirs::runtime_dir

    fn BaseDirs::runtime_dir(self : BaseDirs) -> String?

    Runtime directory (XDG_RUNTIME_DIR on Unix); None on Windows.

    BaseDirs::state_dir

    fn BaseDirs::state_dir(self : BaseDirs) -> String?

    State directory (XDG_STATE_HOME or .local/state on Unix); None on Windows.

    Platform

    pub enum Platform {
    Windows
    Linux
    Darwin
    Unknown
    }

    Runtime operating system: Windows, Linux, macOS (Darwin), or Unknown. Use with platform() to branch on OS (e.g. path rules, UI defaults).

    ProjectDirs

    pub struct ProjectDirs {
    project_path : String
    cache_dir : String
    config_dir : String
    config_local_dir : String
    data_dir : String
    data_local_dir : String
    preference_dir : String
    runtime_dir : String?
    state_dir : String?
    }

    Application-scoped directories: cache, config, data, etc. under a project path. Path segment: Linux = single lowercase segment; macOS = qualifier.Org-App; Windows = Org\\App.

    ProjectDirs::cache_dir

    fn ProjectDirs::cache_dir(self : ProjectDirs) -> String

    App cache directory (e.g. base cache_dir + project_path).

    ProjectDirs::config_dir

    fn ProjectDirs::config_dir(self : ProjectDirs) -> String

    App config directory (roaming on Windows).

    ProjectDirs::config_local_dir

    fn ProjectDirs::config_local_dir(self : ProjectDirs) -> String

    App local config directory.

    ProjectDirs::data_dir

    fn ProjectDirs::data_dir(self : ProjectDirs) -> String

    App data directory.

    ProjectDirs::data_local_dir

    fn ProjectDirs::data_local_dir(self : ProjectDirs) -> String

    App local data directory.

    ProjectDirs::from

    fn ProjectDirs::from(qualifier : String, organization : String, application : String) -> ProjectDirs?

    Creates ProjectDirs from qualifier, organization, and application names. Returns None if home/base dirs are unavailable. Example: ProjectDirs::from("com", "Acme", "MyApp") → cache/config/data under Acme\\MyApp (Windows) or com/acme.myapp (macOS) or myapp (Linux).

    ProjectDirs::preference_dir

    fn ProjectDirs::preference_dir(self : ProjectDirs) -> String

    App preference/settings directory.

    ProjectDirs::project_path

    fn ProjectDirs::project_path(self : ProjectDirs) -> String

    Project path segment used under base dirs (e.g. "Acme\MyApp" on Windows, "myapp" on Linux).

    ProjectDirs::runtime_dir

    fn ProjectDirs::runtime_dir(self : ProjectDirs) -> String?

    App runtime directory (Unix only); None on Windows.

    ProjectDirs::state_dir

    fn ProjectDirs::state_dir(self : ProjectDirs) -> String?

    App state directory (Unix only); None on Windows.

    UserDirs

    pub struct UserDirs {
    home_dir : String
    audio_dir : String?
    desktop_dir : String?
    document_dir : String?
    download_dir : String?
    font_dir : String?
    picture_dir : String?
    public_dir : String?
    template_dir : String?
    video_dir : String?
    }

    User-facing directories: home, Desktop, Documents, Downloads, Music, Pictures, etc. On Unix: XDG_* when set (e.g. XDG_DOCUMENTS_DIR), else $HOME/Desktop, $HOME/Documents, etc. On Windows: USERPROFILE plus Desktop, Documents, Downloads, Music, Pictures, etc.

    UserDirs::audio_dir

    fn UserDirs::audio_dir(self : UserDirs) -> String?

    Music/Audio directory (e.g. Music on Windows, XDG_MUSIC_DIR or Music on Unix).

    UserDirs::desktop_dir

    fn UserDirs::desktop_dir(self : UserDirs) -> String?

    Desktop directory.

    UserDirs::document_dir

    fn UserDirs::document_dir(self : UserDirs) -> String?

    Documents directory.

    UserDirs::download_dir

    fn UserDirs::download_dir(self : UserDirs) -> String?

    Downloads directory.

    UserDirs::font_dir

    fn UserDirs::font_dir(self : UserDirs) -> String?

    Fonts directory (Unix: .local/share/fonts); None on Windows.

    UserDirs::home_dir

    fn UserDirs::home_dir(self : UserDirs) -> String

    User home directory.

    UserDirs::new

    fn UserDirs::new() -> UserDirs?

    Creates UserDirs from the current environment. Returns None if home is not set.

    Example: match UserDirs::new() { Some(u) => u.download_dir() // e.g. /home/alice/Downloads or C:\Users\alice\Downloads None => ... }

    UserDirs::picture_dir

    fn UserDirs::picture_dir(self : UserDirs) -> String?

    Pictures directory.

    UserDirs::public_dir

    fn UserDirs::public_dir(self : UserDirs) -> String?

    Public share directory.

    UserDirs::template_dir

    fn UserDirs::template_dir(self : UserDirs) -> String?

    Templates directory.

    UserDirs::video_dir

    fn UserDirs::video_dir(self : UserDirs) -> String?

    Videos directory.

    is_windows

    fn is_windows() -> Bool

    Returns true when the current OS is Windows, false otherwise. Use for path separator, HOME vs USERPROFILE, or Windows-specific APIs.

    Example: if is_windows() { "\\" } else { "/" }

    join

    fn join(segments : Array[String]) -> String

    Joins path segments using the current OS separator. Empty segments are skipped. On Windows uses \\; on Unix uses /.

    Example: join(["home", "alice", ".config"]) → "home/alice/.config" on Unix, "home\\alice\\.config" on Windows.

    path_sep

    fn path_sep() -> String

    Returns the path separator for the current OS: "\\" on Windows, "/" on Unix. Use when building or displaying OS-native paths.

    Example: path_sep() → "\\" on Windows, "/" on Linux/macOS.

    platform

    fn platform() -> Platform

    Returns the current OS at runtime: Platform::Windows, Linux, Darwin, or Unknown. JS target uses process.platform; native uses C preprocessor (_WIN32, APPLE, linux).

    Example: match platform() { Platform::Windows => ... ; Platform::Darwin => ... ; _ => ... }