A small Rust library for obtaining platform dependant directory paths for application and user directories.
Note that the given directory paths are not guaranteed to exist.
Allows for specifying if an application is CLI or GUI based, since on macOS, CLI applications are expected to conform to the XDG spec, while GUI applications are expected to conform to the [Standard Directories] guidelines.
Uses the following standards: - Unix (excluding macOS): [XDG Base Directories] and [XDG User Directories] - macOS - GUI apps: [Standard Directories] - CLI apps: [Standard Directories] for user directories and [XDG Base Directories] for application directories - Windows: [Known Folder]
Add the following to Cargo.toml:
toml
[dependencies]
platform-dirs = "0.2.0"
```rust use platform_dirs::{AppDirs, AppUI, UserDirs};
fn main() { let appdirs = AppDirs::new(None, AppUI::Graphical).unwrap(); dbg!(&appdirs); // AppDirs { // cachedir: "/home/cjbassi/.cache", // configdir: "/home/cjbassi/.config", // datadir: "/home/cjbassi/.local/share", // statedir: "/home/cjbassi/.local/state" // }
let app_dirs = AppDirs::new(Some("program-name"), AppUI::CommandLine).unwrap();
dbg!(&app_dirs);
// AppDirs {
// cache_dir: "/home/cjbassi/.cache/program-name",
// config_dir: "/home/cjbassi/.config/program-name",
// data_dir: "/home/cjbassi/.local/share/program-name",
// state_dir: "/home/cjbassi/.local/state/program-name"
// }
let user_dirs = UserDirs::new().unwrap();
dbg!(&user_dirs);
// UserDirs {
// desktop_dir: "/home/cjbassi/Desktop",
// document_dir: "/home/cjbassi/Documents",
// download_dir: "/home/cjbassi/Downloads",
// music_dir: "/home/cjbassi/Music",
// picture_dir: "/home/cjbassi/Pictures",
// public_dir: "/home/cjbassi/Public",
// video_dir: "/home/cjbassi/Videos"
// }
let home_dir = platform_dirs::home_dir().unwrap();
dbg!(&home_dir);
// "/home/cjbassi"
} ```
```rust use std::fs::{self, File};
use platform_dirs::{AppDirs, AppUI};
fn main() { let app_dirs = AppDirs::new(Some("program-name"), AppUI::CommandLine).unwrap();
fs::create_dir_all(&app_dirs.config_dir).unwrap();
let config_file_path = app_dirs.config_dir.join("config-file");
let f = if config_file_path.exists() {
File::open(config_file_path).unwrap()
} else {
File::create(config_file_path).unwrap()
};
} ```
Directory | Windows | Unix (excluding macOS GUI apps) | macOS (GUI apps)
-----------|--------------------------------------------------------|------------------------------------------|------------------------------------
cachedir | %LOCALAPPDATA%
(C:\Users\%USERNAME%\AppData\Local
) | $XDG_CACHE_HOME
($HOME/.cache
) | $HOME/Library/Caches
configdir | %APPDATA%
(C:\Users\%USERNAME%\AppData\Roaming
) | $XDG_CONFIG_HOME
($HOME/.config
) | $HOME/Library/Application Support
datadir | %LOCALAPPDATA%
(C:\Users\%USERNAME%\AppData\Local
) | $XDG_DATA_HOME
($HOME/.local/share
) | $HOME/Library/Application Support
statedir | %LOCALAPPDATA%
(C:\Users\%USERNAME%\AppData\Local
) | $XDG_STATE_HOME
($HOME/.local/state
) | $HOME/Library/Application Support
Directory | Windows | Unix (excluding macOS) | macOS
-------------|-----------------------------------------------------------|-----------------------------------------|------------------
desktopdir | {FOLDERID_Desktop}
(C:\Users\%USERNAME%\Desktop
) | XDG_DESKTOP_DIR
($HOME/Desktop
) | $HOME/Desktop
documentdir | {FOLDERID_Documents}
(C:\Users\%USERNAME%\Documents
) | XDG_DOCUMENTS_DIR
($HOME/Documents
) | $HOME/Documents
downloaddir | {FOLDERID_Downloads}
(C:\Users\%USERNAME%\Downloads
) | XDG_DOWNLOAD_DIR
($HOME/Downloads
) | $HOME/Downloads
musicdir | {FOLDERID_Music}
(C:\Users\%USERNAME%\Music
) | XDG_MUSIC_DIR
($HOME/Music
) | $HOME/Music
picturedir | {FOLDERID_Pictures}
(C:\Users\%USERNAME%\Pictures
) | XDG_PICTURES_DIR
($HOME/Pictures
) | $HOME/Pictures
publicdir | {FOLDERID_Public}
(C:\Users\%USERNAME%\Public
) | XDG_PUBLICSHARE_DIR
($HOME/Public
) | $HOME/Public
video_dir | {FOLDERID_Videos}
(C:\Users\%USERNAME%\Videos
) | XDG_VIDEOS_DIR
($HOME/Videos
) | $HOME/Movies
platform-dirs differs from dirs-rs and directories-rs in several ways:
Library/Preferences
to Library/Application Support
Library/Preferences
is supposed to be used for macOS unique plist preferences: infodata_local_dir
runtime_dir
, executable_dir
state_dir
to AppDirs