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.0
    License
    Apache-2.0
    Last updated
    7 months ago
    Downloads
    20

    Dependencies

    #moonrockz/directories

    Platform-specific config, cache, and data directory paths for MoonBit (XDG on Linux, conventions on macOS/Windows). 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
    base.config_dir() // e.g. /home/alice/.config
    base.cache_dir() // e.g. /home/alice/.cache
    }
    None => () // HOME not set
    }

    // ProjectDirs: app-specific paths
    match @directories.ProjectDirs::from("com", "Foo Corp", "Bar App") {
    Some(proj) => {
    proj.config_dir() // e.g. /home/alice/.config/barapp
    proj.cache_dir()
    }
    None => ()
    }

    #API

    • BaseDirs::new() → Option[BaseDirs]: home, 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, desktop, document, download, font, picture, public, template, video dirs.
    • ProjectDirs::from(qualifier, organization, application) → Option[ProjectDirs]: project-scoped cache, config, data, etc.

    v1 resolves paths from environment variables (HOME, XDG_* on Linux). 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?
    }

    BaseDirs: user-invisible standard directories (cache, config, data, etc.). Follows XDG on Linux; v1 uses env vars only.

    BaseDirs::cache_dir

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

    BaseDirs::config_dir

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

    BaseDirs::config_local_dir

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

    BaseDirs::data_dir

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

    BaseDirs::data_local_dir

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

    BaseDirs::executable_dir

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

    BaseDirs::home_dir

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

    BaseDirs::new

    fn BaseDirs::new() -> BaseDirs?

    Create BaseDirs from current environment. Returns None if home is not set. On Windows uses LOCALAPPDATA/APPDATA; on Unix uses XDG_* or $HOME subdirs.

    BaseDirs::preference_dir

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

    BaseDirs::runtime_dir

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

    BaseDirs::state_dir

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

    Platform

    pub enum Platform {
    Windows
    Linux
    Darwin
    Unknown
    }

    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?
    }

    ProjectDirs: project-scoped cache, config, data paths. Path segment rules: Linux (single lowercase segment), macOS (qualifier.Org-App), Windows (Org/App).

    ProjectDirs::cache_dir

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

    ProjectDirs::config_dir

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

    ProjectDirs::config_local_dir

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

    ProjectDirs::data_dir

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

    ProjectDirs::data_local_dir

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

    ProjectDirs::from

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

    Create ProjectDirs from qualifier, organization, application. Returns None if HOME (or base dirs) unavailable.

    ProjectDirs::preference_dir

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

    ProjectDirs::project_path

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

    ProjectDirs::runtime_dir

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

    ProjectDirs::state_dir

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

    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?
    }

    UserDirs: user-facing standard directories (Desktop, Documents, Music, etc.). v1 uses XDG_* env vars or $HOME subdirs.

    UserDirs::audio_dir

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

    UserDirs::desktop_dir

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

    UserDirs::document_dir

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

    UserDirs::download_dir

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

    UserDirs::font_dir

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

    UserDirs::home_dir

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

    UserDirs::new

    fn UserDirs::new() -> UserDirs?

    Create UserDirs from current environment. Returns None if home is not set. On Windows uses USERPROFILE and standard subdirs; on Unix uses XDG_* or $HOME subdirs.

    UserDirs::picture_dir

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

    UserDirs::public_dir

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

    UserDirs::template_dir

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

    UserDirs::video_dir

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

    is_windows

    fn is_windows() -> Bool

    Convenience: true if platform is Windows.

    join

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

    Join path segments for the current OS. Empty segments are skipped. Uses platform path separator (\ on Windows, / on Unix).

    path_sep

    fn path_sep() -> String

    Path separator for the current OS: "\" on Windows, "/" otherwise.

    platform

    fn platform() -> Platform

    Returns the current OS platform at runtime (Windows, Linux, Darwin, or Unknown). Implemented via FFI per target: JS uses process.platform; native uses C preprocessor macros.