summaryrefslogtreecommitdiffstats
path: root/third_party/rust/dirs/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/dirs/src
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/dirs/src')
-rw-r--r--third_party/rust/dirs/src/lib.rs272
-rw-r--r--third_party/rust/dirs/src/lin.rs34
-rw-r--r--third_party/rust/dirs/src/mac.rs20
-rw-r--r--third_party/rust/dirs/src/wasm.rs20
-rw-r--r--third_party/rust/dirs/src/win.rs20
5 files changed, 366 insertions, 0 deletions
diff --git a/third_party/rust/dirs/src/lib.rs b/third_party/rust/dirs/src/lib.rs
new file mode 100644
index 0000000000..96f66a818c
--- /dev/null
+++ b/third_party/rust/dirs/src/lib.rs
@@ -0,0 +1,272 @@
+//! The _dirs_ crate is
+//!
+//! - a tiny library with a minimal API (16 functions)
+//! - that provides the platform-specific, user-accessible locations
+//! - for finding and storing configuration, cache and other data
+//! - on Linux, Redox, Windows (≥ Vista) and macOS.
+//!
+//! The library provides the location of these directories by leveraging the mechanisms defined by
+//!
+//! - the [XDG base directory](https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) and the [XDG user directory](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/) specifications on Linux,
+//! - the [Known Folder](https://msdn.microsoft.com/en-us/library/windows/desktop/bb776911(v=vs.85).aspx) system on Windows, and
+//! - the [Standard Directories](https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW6) on macOS.
+
+#![deny(missing_docs)]
+
+#[macro_use]
+extern crate cfg_if;
+
+use std::path::PathBuf;
+
+cfg_if! {
+ if #[cfg(target_os = "windows")] {
+ mod win;
+ use win as sys;
+ } else if #[cfg(any(target_os = "macos", target_os = "ios"))] {
+ mod mac;
+ use mac as sys;
+ } else if #[cfg(target_arch = "wasm32")] {
+ mod wasm;
+ use wasm as sys;
+ } else {
+ mod lin;
+ use lin as sys;
+ }
+}
+
+/// Returns the path to the user's home directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | -------------------- | -------------- |
+/// | Linux | `$HOME` | /home/alice |
+/// | macOS | `$HOME` | /Users/Alice |
+/// | Windows | `{FOLDERID_Profile}` | C:\Users\Alice |
+///
+/// ### Linux and macOS:
+///
+/// - Use `$HOME` if it is set and not empty.
+/// - If `$HOME` is not set or empty, then the function `getpwuid_r` is used to determine
+/// the home directory of the current user.
+/// - If `getpwuid_r` lacks an entry for the current user id or the home directory field is empty,
+/// then the function returns `None`.
+///
+/// ### Windows:
+///
+/// This function retrieves the user profile folder using `SHGetKnownFolderPath`.
+///
+/// All the examples on this page mentioning `$HOME` use this behavior.
+///
+/// _Note:_ This function's behavior differs from [`std::env::home_dir`],
+/// which works incorrectly on Linux, macOS and Windows.
+///
+/// [`std::env::home_dir`]: https://doc.rust-lang.org/std/env/fn.home_dir.html
+pub fn home_dir() -> Option<PathBuf> {
+ sys::home_dir()
+}
+/// Returns the path to the user's cache directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ----------------------------------- | ---------------------------- |
+/// | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache |
+/// | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches |
+/// | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
+pub fn cache_dir() -> Option<PathBuf> {
+ sys::cache_dir()
+}
+/// Returns the path to the user's config directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ------------------------------------- | -------------------------------- |
+/// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
+/// | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences |
+/// | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
+pub fn config_dir() -> Option<PathBuf> {
+ sys::config_dir()
+}
+/// Returns the path to the user's data directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ---------------------------------------- | ---------------------------------------- |
+/// | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
+/// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
+/// | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
+pub fn data_dir() -> Option<PathBuf> {
+ sys::data_dir()
+}
+/// Returns the path to the user's local data directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ---------------------------------------- | ---------------------------------------- |
+/// | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
+/// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
+/// | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
+pub fn data_local_dir() -> Option<PathBuf> {
+ sys::data_local_dir()
+}
+/// Returns the path to the user's executable directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ---------------------------------------------------------------- | ---------------------- |
+/// | Linux | `$XDG_BIN_HOME` or `$XDG_DATA_HOME`/../bin or `$HOME`/.local/bin | /home/alice/.local/bin |
+/// | macOS | – | – |
+/// | Windows | – | – |
+pub fn executable_dir() -> Option<PathBuf> {
+ sys::executable_dir()
+}
+/// Returns the path to the user's runtime directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ------------------ | --------------- |
+/// | Linux | `$XDG_RUNTIME_DIR` | /run/user/1001/ |
+/// | macOS | – | – |
+/// | Windows | – | – |
+pub fn runtime_dir() -> Option<PathBuf> {
+ sys::runtime_dir()
+}
+
+/// Returns the path to the user's audio directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ------------------ | -------------------- |
+/// | Linux | `XDG_MUSIC_DIR` | /home/alice/Music |
+/// | macOS | `$HOME`/Music | /Users/Alice/Music |
+/// | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music |
+pub fn audio_dir() -> Option<PathBuf> {
+ sys::audio_dir()
+}
+/// Returns the path to the user's desktop directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | -------------------- | ---------------------- |
+/// | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop |
+/// | macOS | `$HOME`/Desktop | /Users/Alice/Desktop |
+/// | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop |
+pub fn desktop_dir() -> Option<PathBuf> {
+ sys::desktop_dir()
+}
+/// Returns the path to the user's document directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ---------------------- | ------------------------ |
+/// | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents |
+/// | macOS | `$HOME`/Documents | /Users/Alice/Documents |
+/// | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents |
+pub fn document_dir() -> Option<PathBuf> {
+ sys::document_dir()
+}
+/// Returns the path to the user's download directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ---------------------- | ------------------------ |
+/// | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads |
+/// | macOS | `$HOME`/Downloads | /Users/Alice/Downloads |
+/// | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads |
+pub fn download_dir() -> Option<PathBuf> {
+ sys::download_dir()
+}
+/// Returns the path to the user's font directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ---------------------------------------------------- | ------------------------------ |
+/// | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts |
+/// | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts |
+/// | Windows | – | – |
+pub fn font_dir() -> Option<PathBuf> {
+ sys::font_dir()
+}
+/// Returns the path to the user's picture directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | --------------------- | ----------------------- |
+/// | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures |
+/// | macOS | `$HOME`/Pictures | /Users/Alice/Pictures |
+/// | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures |
+pub fn picture_dir() -> Option<PathBuf> {
+ sys::picture_dir()
+}
+/// Returns the path to the user's public directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | --------------------- | ------------------- |
+/// | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public |
+/// | macOS | `$HOME`/Public | /Users/Alice/Public |
+/// | Windows | `{FOLDERID_Public}` | C:\Users\Public |
+pub fn public_dir() -> Option<PathBuf> {
+ sys::public_dir()
+}
+/// Returns the path to the user's template directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ---------------------- | ---------------------------------------------------------- |
+/// | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates |
+/// | macOS | – | – |
+/// | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates |
+pub fn template_dir() -> Option<PathBuf> {
+ sys::template_dir()
+}
+
+/// Returns the path to the user's video directory.
+///
+/// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
+///
+/// |Platform | Value | Example |
+/// | ------- | ------------------- | --------------------- |
+/// | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos |
+/// | macOS | `$HOME`/Movies | /Users/Alice/Movies |
+/// | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos |
+pub fn video_dir() -> Option<PathBuf> {
+ sys::video_dir()
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn test_dirs() {
+ println!("home_dir: {:?}", ::home_dir());
+ println!("cache_dir: {:?}", ::cache_dir());
+ println!("config_dir: {:?}", ::config_dir());
+ println!("data_dir: {:?}", ::data_dir());
+ println!("data_local_dir: {:?}", ::data_local_dir());
+ println!("executable_dir: {:?}", ::executable_dir());
+ println!("runtime_dir: {:?}", ::runtime_dir());
+ println!("audio_dir: {:?}", ::audio_dir());
+ println!("home_dir: {:?}", ::desktop_dir());
+ println!("cache_dir: {:?}", ::document_dir());
+ println!("config_dir: {:?}", ::download_dir());
+ println!("font_dir: {:?}", ::font_dir());
+ println!("picture_dir: {:?}", ::picture_dir());
+ println!("public_dir: {:?}", ::public_dir());
+ println!("template_dir: {:?}", ::template_dir());
+ println!("video_dir: {:?}", ::video_dir());
+ }
+}
diff --git a/third_party/rust/dirs/src/lin.rs b/third_party/rust/dirs/src/lin.rs
new file mode 100644
index 0000000000..74ac610bfd
--- /dev/null
+++ b/third_party/rust/dirs/src/lin.rs
@@ -0,0 +1,34 @@
+extern crate dirs_sys;
+
+use std::env;
+use std::path::PathBuf;
+
+pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }
+pub fn cache_dir() -> Option<PathBuf> { env::var_os("XDG_CACHE_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".cache"))) }
+pub fn config_dir() -> Option<PathBuf> { env::var_os("XDG_CONFIG_HOME").and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".config"))) }
+pub fn data_dir() -> Option<PathBuf> { env::var_os("XDG_DATA_HOME") .and_then(dirs_sys::is_absolute_path).or_else(|| home_dir().map(|h| h.join(".local/share"))) }
+pub fn data_local_dir() -> Option<PathBuf> { data_dir().clone() }
+pub fn runtime_dir() -> Option<PathBuf> { env::var_os("XDG_RUNTIME_DIR").and_then(dirs_sys::is_absolute_path) }
+pub fn executable_dir() -> Option<PathBuf> {
+ env::var_os("XDG_BIN_HOME").and_then(dirs_sys::is_absolute_path).or_else(|| {
+ data_dir().map(|mut e| { e.pop(); e.push("bin"); e })
+ })
+}
+pub fn audio_dir() -> Option<PathBuf> { dirs_sys::user_dir("MUSIC") }
+pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::user_dir("DESKTOP") }
+pub fn document_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOCUMENTS") }
+pub fn download_dir() -> Option<PathBuf> { dirs_sys::user_dir("DOWNLOAD") }
+pub fn font_dir() -> Option<PathBuf> { data_dir().map(|d| d.join("fonts")) }
+pub fn picture_dir() -> Option<PathBuf> { dirs_sys::user_dir("PICTURES") }
+pub fn public_dir() -> Option<PathBuf> { dirs_sys::user_dir("PUBLICSHARE") }
+pub fn template_dir() -> Option<PathBuf> { dirs_sys::user_dir("TEMPLATES") }
+pub fn video_dir() -> Option<PathBuf> { dirs_sys::user_dir("VIDEOS") }
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn test_file_user_dirs_exists() {
+ let user_dirs_file = ::config_dir().unwrap().join("user-dirs.dirs");
+ println!("{:?} exists: {:?}", user_dirs_file, user_dirs_file.exists());
+ }
+}
diff --git a/third_party/rust/dirs/src/mac.rs b/third_party/rust/dirs/src/mac.rs
new file mode 100644
index 0000000000..ae7f9c9fe2
--- /dev/null
+++ b/third_party/rust/dirs/src/mac.rs
@@ -0,0 +1,20 @@
+extern crate dirs_sys;
+
+use std::path::PathBuf;
+
+pub fn home_dir() -> Option<PathBuf> { dirs_sys::home_dir() }
+pub fn cache_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Caches")) }
+pub fn config_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Preferences")) }
+pub fn data_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Application Support")) }
+pub fn data_local_dir() -> Option<PathBuf> { data_dir() }
+pub fn executable_dir() -> Option<PathBuf> { None }
+pub fn runtime_dir() -> Option<PathBuf> { None }
+pub fn audio_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Music")) }
+pub fn desktop_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Desktop")) }
+pub fn document_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Documents")) }
+pub fn download_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Downloads")) }
+pub fn font_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Library/Fonts")) }
+pub fn picture_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Pictures")) }
+pub fn public_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Public")) }
+pub fn template_dir() -> Option<PathBuf> { None }
+pub fn video_dir() -> Option<PathBuf> { home_dir().map(|h| h.join("Movies")) }
diff --git a/third_party/rust/dirs/src/wasm.rs b/third_party/rust/dirs/src/wasm.rs
new file mode 100644
index 0000000000..2ead7201b6
--- /dev/null
+++ b/third_party/rust/dirs/src/wasm.rs
@@ -0,0 +1,20 @@
+// Stub definitions to make things *compile*.
+
+use std::path::PathBuf;
+
+pub fn home_dir() -> Option<PathBuf> { None }
+pub fn cache_dir() -> Option<PathBuf> { None }
+pub fn config_dir() -> Option<PathBuf> { None }
+pub fn data_dir() -> Option<PathBuf> { None }
+pub fn data_local_dir() -> Option<PathBuf> { None }
+pub fn runtime_dir() -> Option<PathBuf> { None }
+pub fn executable_dir() -> Option<PathBuf> { None }
+pub fn audio_dir() -> Option<PathBuf> { None }
+pub fn desktop_dir() -> Option<PathBuf> { None }
+pub fn document_dir() -> Option<PathBuf> { None }
+pub fn download_dir() -> Option<PathBuf> { None }
+pub fn font_dir() -> Option<PathBuf> { None }
+pub fn picture_dir() -> Option<PathBuf> { None }
+pub fn public_dir() -> Option<PathBuf> { None }
+pub fn template_dir() -> Option<PathBuf> { None }
+pub fn video_dir() -> Option<PathBuf> { None }
diff --git a/third_party/rust/dirs/src/win.rs b/third_party/rust/dirs/src/win.rs
new file mode 100644
index 0000000000..a62e95bbfb
--- /dev/null
+++ b/third_party/rust/dirs/src/win.rs
@@ -0,0 +1,20 @@
+extern crate dirs_sys;
+
+use std::path::PathBuf;
+
+pub fn home_dir() -> Option<PathBuf> { dirs_sys::known_folder_profile() }
+pub fn data_dir() -> Option<PathBuf> { dirs_sys::known_folder_roaming_app_data() }
+pub fn data_local_dir() -> Option<PathBuf> { dirs_sys::known_folder_local_app_data() }
+pub fn cache_dir() -> Option<PathBuf> { data_local_dir() }
+pub fn config_dir() -> Option<PathBuf> { data_dir() }
+pub fn executable_dir() -> Option<PathBuf> { None }
+pub fn runtime_dir() -> Option<PathBuf> { None }
+pub fn audio_dir() -> Option<PathBuf> { dirs_sys::known_folder_music() }
+pub fn desktop_dir() -> Option<PathBuf> { dirs_sys::known_folder_desktop() }
+pub fn document_dir() -> Option<PathBuf> { dirs_sys::known_folder_documents() }
+pub fn download_dir() -> Option<PathBuf> { dirs_sys::known_folder_downloads() }
+pub fn font_dir() -> Option<PathBuf> { None }
+pub fn picture_dir() -> Option<PathBuf> { dirs_sys::known_folder_pictures() }
+pub fn public_dir() -> Option<PathBuf> { dirs_sys::known_folder_public()}
+pub fn template_dir() -> Option<PathBuf> { dirs_sys::known_folder_templates() }
+pub fn video_dir() -> Option<PathBuf> { dirs_sys::known_folder_videos() }