├── .gitignore ├── src ├── wasm.rs ├── mac.rs ├── win.rs ├── lin.rs └── lib.rs ├── Cargo.toml ├── benches └── constructors.rs ├── .github └── workflows │ └── rust.yml ├── LICENSE-MIT ├── LICENSE-APACHE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | Cargo.lock 2 | /target 3 | **/*.rs.bk 4 | .idea -------------------------------------------------------------------------------- /src/wasm.rs: -------------------------------------------------------------------------------- 1 | // Stub definitions to make things *compile*. 2 | 3 | use std::path::PathBuf; 4 | 5 | use BaseDirs; 6 | use UserDirs; 7 | use ProjectDirs; 8 | 9 | pub fn base_dirs() -> Option { None } 10 | pub fn user_dirs() -> Option { None } 11 | pub fn project_dirs_from_path(project_path: PathBuf) -> Option { None } 12 | pub fn project_dirs_from(qualifier: &str, organization: &str, application: &str) -> Option { None } 13 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "directories" 3 | version = "6.0.0" 4 | authors = ["Simon Ochsenreither "] 5 | description = "A tiny mid-level library that provides platform-specific standard locations of directories for config, cache and other data on Linux, Windows and macOS by leveraging the mechanisms defined by the XDG base/user directory specifications on Linux, the Known Folder API on Windows, and the Standard Directory guidelines on macOS." 6 | readme = "README.md" 7 | license = "MIT OR Apache-2.0" 8 | repository = "https://github.com/dirs-dev/directories-rs" 9 | maintenance = { status = "actively-developed" } 10 | keywords = ["xdg", "basedir", "app_dirs", "path", "folder"] 11 | 12 | [dependencies] 13 | dirs-sys = "0.5.0" 14 | 15 | [dev-dependencies] 16 | bencher = "0.1.5" 17 | 18 | [[bench]] 19 | name = "constructors" 20 | harness = false 21 | -------------------------------------------------------------------------------- /benches/constructors.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate bencher; 3 | extern crate directories; 4 | 5 | use bencher::Bencher; 6 | use bencher::black_box; 7 | use directories::BaseDirs; 8 | use directories::ProjectDirs; 9 | use directories::UserDirs; 10 | 11 | fn base_dirs(b: &mut Bencher) { 12 | b.iter(|| { 13 | let _ = black_box(BaseDirs::new()); 14 | }); 15 | } 16 | 17 | fn user_dirs(b: &mut Bencher) { 18 | b.iter(|| { 19 | let _ = black_box(UserDirs::new()); 20 | }); 21 | } 22 | 23 | fn project_dirs_from_path(b: &mut Bencher) { 24 | b.iter(|| { 25 | let _ = black_box(ProjectDirs::from_path(Default::default())); 26 | }); 27 | } 28 | 29 | fn project_dirs(b: &mut Bencher) { 30 | b.iter(|| { 31 | let _ = black_box(ProjectDirs::from("org", "foo", "Bar App")); 32 | }); 33 | } 34 | 35 | benchmark_group!(constructors, 36 | base_dirs, 37 | user_dirs, 38 | project_dirs_from_path, 39 | project_dirs, 40 | ); 41 | benchmark_main!(constructors); 42 | -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | build-and-test-on-linux: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v3 17 | - name: Build 18 | run: cargo build --verbose 19 | - name: Test 20 | run: RUST_BACKTRACE=full cargo test --verbose -- --nocapture 21 | build-and-test-on-windows: 22 | runs-on: windows-latest 23 | steps: 24 | - uses: actions/checkout@v3 25 | - name: Build 26 | run: cargo build --verbose 27 | - name: Test 28 | run: $env:RUST_BACKTRACE="full"; cargo test --verbose -- --nocapture 29 | build-and-test-on-macos: 30 | runs-on: macos-latest 31 | steps: 32 | - uses: actions/checkout@v3 33 | - name: Build 34 | run: cargo build --verbose 35 | - name: Test 36 | run: RUST_BACKTRACE=full cargo test --verbose -- --nocapture 37 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 directories-rs contributors 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /src/mac.rs: -------------------------------------------------------------------------------- 1 | extern crate dirs_sys; 2 | 3 | use std::path::PathBuf; 4 | 5 | use BaseDirs; 6 | use UserDirs; 7 | use ProjectDirs; 8 | 9 | pub fn base_dirs() -> Option { 10 | if let Some(home_dir) = dirs_sys::home_dir() { 11 | let cache_dir = home_dir.join("Library/Caches"); 12 | let config_dir = home_dir.join("Library/Application Support"); 13 | let config_local_dir = config_dir.clone(); 14 | let data_dir = config_dir.clone(); 15 | let data_local_dir = config_dir.clone(); 16 | let preference_dir = home_dir.join("Library/Preferences"); 17 | 18 | let base_dirs = BaseDirs { 19 | home_dir: home_dir, 20 | cache_dir: cache_dir, 21 | config_dir: config_dir, 22 | config_local_dir: config_local_dir, 23 | data_dir: data_dir, 24 | data_local_dir: data_local_dir, 25 | executable_dir: None, 26 | preference_dir: preference_dir, 27 | runtime_dir: None, 28 | state_dir: None 29 | }; 30 | Some(base_dirs) 31 | } else { 32 | None 33 | } 34 | } 35 | 36 | pub fn user_dirs() -> Option { 37 | if let Some(home_dir) = dirs_sys::home_dir() { 38 | let audio_dir = home_dir.join("Music"); 39 | let desktop_dir = home_dir.join("Desktop"); 40 | let document_dir = home_dir.join("Documents"); 41 | let download_dir = home_dir.join("Downloads"); 42 | let picture_dir = home_dir.join("Pictures"); 43 | let public_dir = home_dir.join("Public"); 44 | let video_dir = home_dir.join("Movies"); 45 | let font_dir = home_dir.join("Library/Fonts"); 46 | 47 | let user_dirs = UserDirs { 48 | home_dir: home_dir, 49 | audio_dir: Some(audio_dir), 50 | desktop_dir: Some(desktop_dir), 51 | document_dir: Some(document_dir), 52 | download_dir: Some(download_dir), 53 | font_dir: Some(font_dir), 54 | picture_dir: Some(picture_dir), 55 | public_dir: Some(public_dir), 56 | template_dir: None, 57 | video_dir: Some(video_dir) 58 | }; 59 | Some(user_dirs) 60 | } else { 61 | None 62 | } 63 | } 64 | 65 | pub fn project_dirs_from_path(project_path: PathBuf) -> Option { 66 | if let Some(home_dir) = dirs_sys::home_dir() { 67 | let cache_dir = home_dir.join("Library/Caches").join(&project_path); 68 | let config_dir = home_dir.join("Library/Application Support").join(&project_path); 69 | let config_local_dir = config_dir.clone(); 70 | let data_dir = config_dir.clone(); 71 | let data_local_dir = config_dir.clone(); 72 | let preference_dir = home_dir.join("Library/Preferences").join(&project_path); 73 | 74 | let project_dirs = ProjectDirs { 75 | project_path: project_path, 76 | cache_dir: cache_dir, 77 | config_dir: config_dir, 78 | config_local_dir: config_local_dir, 79 | data_dir: data_dir, 80 | data_local_dir: data_local_dir, 81 | preference_dir: preference_dir, 82 | runtime_dir: None, 83 | state_dir: None 84 | }; 85 | Some(project_dirs) 86 | } else { 87 | None 88 | } 89 | } 90 | 91 | pub fn project_dirs_from(qualifier: &str, organization: &str, application: &str) -> Option { 92 | // we should replace more characters, according to RFC1034 identifier rules 93 | let organization = organization.replace(" ", "-"); 94 | let application = application.replace(" ", "-"); 95 | let mut parts = vec![qualifier, &organization, &application]; parts.retain(|e| !e.is_empty()); 96 | let bundle_id = parts.join("."); 97 | ProjectDirs::from_path(PathBuf::from(bundle_id)) 98 | } 99 | -------------------------------------------------------------------------------- /src/win.rs: -------------------------------------------------------------------------------- 1 | extern crate dirs_sys; 2 | 3 | use std::path::PathBuf; 4 | use std::iter::FromIterator; 5 | 6 | use BaseDirs; 7 | use UserDirs; 8 | use ProjectDirs; 9 | 10 | pub fn base_dirs() -> Option { 11 | let home_dir = dirs_sys::known_folder_profile(); 12 | let data_dir = dirs_sys::known_folder_roaming_app_data(); 13 | let data_local_dir = dirs_sys::known_folder_local_app_data(); 14 | if let (Some(home_dir), Some(data_dir), Some(data_local_dir)) = (home_dir, data_dir, data_local_dir) { 15 | let cache_dir = data_local_dir.clone(); 16 | let config_dir = data_dir.clone(); 17 | let config_local_dir = data_local_dir.clone(); 18 | let preference_dir = data_dir.clone(); 19 | 20 | let base_dirs = BaseDirs { 21 | home_dir: home_dir, 22 | cache_dir: cache_dir, 23 | config_dir: config_dir, 24 | config_local_dir: config_local_dir, 25 | data_dir: data_dir, 26 | data_local_dir: data_local_dir, 27 | executable_dir: None, 28 | preference_dir: preference_dir, 29 | runtime_dir: None, 30 | state_dir: None 31 | }; 32 | Some(base_dirs) 33 | } else { 34 | None 35 | } 36 | } 37 | 38 | pub fn user_dirs() -> Option { 39 | if let Some(home_dir) = dirs_sys::known_folder_profile() { 40 | let audio_dir = dirs_sys::known_folder_music(); 41 | let desktop_dir = dirs_sys::known_folder_desktop(); 42 | let document_dir = dirs_sys::known_folder_documents(); 43 | let download_dir = dirs_sys::known_folder_downloads(); 44 | let picture_dir = dirs_sys::known_folder_pictures(); 45 | let public_dir = dirs_sys::known_folder_public(); 46 | let template_dir = dirs_sys::known_folder_templates(); 47 | let video_dir = dirs_sys::known_folder_videos(); 48 | 49 | let user_dirs = UserDirs { 50 | home_dir: home_dir, 51 | audio_dir: audio_dir, 52 | desktop_dir: desktop_dir, 53 | document_dir: document_dir, 54 | download_dir: download_dir, 55 | font_dir: None, 56 | picture_dir: picture_dir, 57 | public_dir: public_dir, 58 | template_dir: template_dir, 59 | video_dir: video_dir 60 | }; 61 | Some(user_dirs) 62 | } else { 63 | None 64 | } 65 | } 66 | 67 | pub fn project_dirs_from_path(project_path: PathBuf) -> Option { 68 | let app_data_local = dirs_sys::known_folder_local_app_data(); 69 | let app_data_roaming = dirs_sys::known_folder_roaming_app_data(); 70 | if let (Some(app_data_local), Some(app_data_roaming)) = (app_data_local, app_data_roaming) { 71 | let app_data_local = app_data_local.join(&project_path); 72 | let app_data_roaming = app_data_roaming.join(&project_path); 73 | let cache_dir = app_data_local.join("cache"); 74 | let data_local_dir = app_data_local.join("data"); 75 | let config_dir = app_data_roaming.join("config"); 76 | let config_local_dir = app_data_local.join("config"); 77 | let data_dir = app_data_roaming.join("data"); 78 | let preference_dir = config_dir.clone(); 79 | 80 | let project_dirs = ProjectDirs { 81 | project_path: project_path, 82 | cache_dir: cache_dir, 83 | config_dir: config_dir, 84 | config_local_dir: config_local_dir, 85 | data_dir: data_dir, 86 | data_local_dir: data_local_dir, 87 | preference_dir: preference_dir, 88 | runtime_dir: None, 89 | state_dir: None 90 | }; 91 | Some(project_dirs) 92 | } else { 93 | None 94 | } 95 | 96 | } 97 | 98 | pub fn project_dirs_from(_qualifier: &str, organization: &str, application: &str) -> Option { 99 | ProjectDirs::from_path(PathBuf::from_iter(&[organization, application])) 100 | } 101 | -------------------------------------------------------------------------------- /src/lin.rs: -------------------------------------------------------------------------------- 1 | extern crate dirs_sys; 2 | 3 | use std::env; 4 | use std::path::PathBuf; 5 | 6 | use BaseDirs; 7 | use UserDirs; 8 | use ProjectDirs; 9 | 10 | pub fn base_dirs() -> Option { 11 | if let Some(home_dir) = dirs_sys::home_dir() { 12 | let cache_dir = env::var_os("XDG_CACHE_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".cache")); 13 | let config_dir = env::var_os("XDG_CONFIG_HOME").and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".config")); 14 | let config_local_dir = config_dir.clone(); 15 | let data_dir = env::var_os("XDG_DATA_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".local/share")); 16 | let data_local_dir = data_dir.clone(); 17 | let preference_dir = config_dir.clone(); 18 | let runtime_dir = env::var_os("XDG_RUNTIME_DIR").and_then(dirs_sys::is_absolute_path); 19 | let state_dir = env::var_os("XDG_STATE_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".local/state")); 20 | let executable_dir = env::var_os("XDG_BIN_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".local/bin")); 21 | 22 | let base_dirs = BaseDirs { 23 | home_dir: home_dir, 24 | cache_dir: cache_dir, 25 | config_dir: config_dir, 26 | config_local_dir: config_local_dir, 27 | data_dir: data_dir, 28 | data_local_dir: data_local_dir, 29 | executable_dir: Some(executable_dir), 30 | preference_dir: preference_dir, 31 | runtime_dir: runtime_dir, 32 | state_dir: Some(state_dir) 33 | }; 34 | Some(base_dirs) 35 | } else { 36 | None 37 | } 38 | } 39 | 40 | pub fn user_dirs() -> Option { 41 | if let Some(home_dir) = dirs_sys::home_dir() { 42 | let data_dir = env::var_os("XDG_DATA_HOME").and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".local/share")); 43 | let font_dir = data_dir.join("fonts"); 44 | let mut user_dirs_map = dirs_sys::user_dirs(&home_dir); 45 | 46 | let user_dirs = UserDirs { 47 | home_dir: home_dir, 48 | audio_dir: user_dirs_map.remove("MUSIC"), 49 | desktop_dir: user_dirs_map.remove("DESKTOP"), 50 | document_dir: user_dirs_map.remove("DOCUMENTS"), 51 | download_dir: user_dirs_map.remove("DOWNLOAD"), 52 | font_dir: Some(font_dir), 53 | picture_dir: user_dirs_map.remove("PICTURES"), 54 | public_dir: user_dirs_map.remove("PUBLICSHARE"), 55 | template_dir: user_dirs_map.remove("TEMPLATES"), 56 | video_dir: user_dirs_map.remove("VIDEOS") 57 | }; 58 | Some(user_dirs) 59 | } else { 60 | None 61 | } 62 | } 63 | 64 | pub fn project_dirs_from_path(project_path: PathBuf) -> Option { 65 | if let Some(home_dir) = dirs_sys::home_dir() { 66 | let cache_dir = env::var_os("XDG_CACHE_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".cache")).join(&project_path); 67 | let config_dir = env::var_os("XDG_CONFIG_HOME").and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".config")).join(&project_path); 68 | let config_local_dir = config_dir.clone(); 69 | let data_dir = env::var_os("XDG_DATA_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".local/share")).join(&project_path); 70 | let data_local_dir = data_dir.clone(); 71 | let preference_dir = config_dir.clone(); 72 | let runtime_dir = env::var_os("XDG_RUNTIME_DIR").and_then(dirs_sys::is_absolute_path).map(|o| o.join(&project_path)); 73 | let state_dir = env::var_os("XDG_STATE_HOME") .and_then(dirs_sys::is_absolute_path).unwrap_or_else(|| home_dir.join(".local/state")).join(&project_path); 74 | 75 | let project_dirs = ProjectDirs { 76 | project_path: project_path, 77 | cache_dir: cache_dir, 78 | config_dir: config_dir, 79 | config_local_dir: config_local_dir, 80 | data_dir: data_dir, 81 | data_local_dir: data_local_dir, 82 | preference_dir: preference_dir, 83 | runtime_dir: runtime_dir, 84 | state_dir: Some(state_dir) 85 | }; 86 | Some(project_dirs) 87 | } else { 88 | None 89 | } 90 | } 91 | 92 | pub fn project_dirs_from(_qualifier: &str, _organization: &str, application: &str) -> Option { 93 | ProjectDirs::from_path(PathBuf::from(&trim_and_lowercase_then_replace_spaces(application, ""))) 94 | } 95 | 96 | fn trim_and_lowercase_then_replace_spaces(name: &str, replacement: &str) -> String { 97 | let mut buf = String::with_capacity(name.len()); 98 | let mut parts = name.split_whitespace(); 99 | let mut current_part = parts.next(); 100 | let replace = !replacement.is_empty(); 101 | while current_part.is_some() { 102 | let value = current_part.unwrap().to_lowercase(); 103 | buf.push_str(&value); 104 | current_part = parts.next(); 105 | if replace && current_part.is_some() { 106 | buf.push_str(replacement); 107 | } 108 | } 109 | buf 110 | } 111 | 112 | #[cfg(test)] 113 | mod tests { 114 | use lin::trim_and_lowercase_then_replace_spaces; 115 | 116 | #[test] 117 | fn test_trim_and_lowercase_then_replace_spaces() { 118 | let input1 = "Bar App"; 119 | let actual1 = trim_and_lowercase_then_replace_spaces(input1, "-"); 120 | let expected1 = "bar-app"; 121 | assert_eq!(expected1, actual1); 122 | 123 | let input2 = "BarApp-Foo"; 124 | let actual2 = trim_and_lowercase_then_replace_spaces(input2, "-"); 125 | let expected2 = "barapp-foo"; 126 | assert_eq!(expected2, actual2); 127 | 128 | let input3 = " Bar App "; 129 | let actual3 = trim_and_lowercase_then_replace_spaces(input3, "-"); 130 | let expected3 = "bar-app"; 131 | assert_eq!(expected3, actual3); 132 | 133 | let input4 = " Bar App "; 134 | let actual4 = trim_and_lowercase_then_replace_spaces(input4, "-"); 135 | let expected4 = "bar-app"; 136 | assert_eq!(expected4, actual4); 137 | } 138 | 139 | #[test] 140 | fn test_file_user_dirs_exists() { 141 | let base_dirs = ::BaseDirs::new(); 142 | let user_dirs_file = base_dirs.unwrap().config_dir().join("user-dirs.dirs"); 143 | println!("{:?} exists: {:?}", user_dirs_file, user_dirs_file.exists()); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![crates.io](https://img.shields.io/crates/v/directories.svg?style=for-the-badge)](https://crates.io/crates/directories) 2 | [![API documentation](https://img.shields.io/docsrs/directories/latest?style=for-the-badge)](https://docs.rs/directories/) 3 | ![actively developed](https://img.shields.io/badge/maintenance-actively--developed-brightgreen.svg?style=for-the-badge) 4 | ![License: MIT/Apache-2.0](https://img.shields.io/badge/license-MIT%2FApache--2.0-orange.svg?style=for-the-badge) 5 | 6 | # `directories` 7 | 8 | ## Introduction 9 | 10 | - a tiny mid-level library with a minimal API 11 | - that provides the platform-specific, user-accessible locations 12 | - for retrieving and storing configuration, cache and other data 13 | - on Linux, Redox, Windows (≥ Vista), macOS and other platforms. 14 | 15 | The library provides the location of these directories by leveraging the mechanisms defined by 16 | - the [XDG base directory](https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) and 17 | the [XDG user directory](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/) specifications on Linux 18 | - the [Known Folder](https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457.aspx) API on Windows 19 | - the [Standard Directories](https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW6) 20 | guidelines on macOS 21 | 22 | ## Platforms 23 | 24 | This library is written in Rust, and supports Linux, Redox, macOS and Windows. 25 | Other platforms are also supported; they use the Linux conventions. 26 | 27 | _dirs_, the low-level sister library, is available at [dirs-rs](https://github.com/soc/dirs-rs). 28 | 29 | A version of this library running on the JVM is provided by [directories-jvm](https://github.com/soc/directories-jvm). 30 | 31 | ## Usage 32 | 33 | #### Dependency 34 | 35 | Add the library as a dependency to your project by inserting 36 | 37 | ```toml 38 | directories = "5.0" 39 | ``` 40 | 41 | into the `[dependencies]` section of your Cargo.toml file. 42 | 43 | If you are upgrading from version 2, please read the [section on breaking changes](#3) first. 44 | 45 | #### Example 46 | 47 | Library run by user Alice: 48 | 49 | ```rust 50 | extern crate directories; 51 | use directories::{BaseDirs, UserDirs, ProjectDirs}; 52 | 53 | if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") { 54 | proj_dirs.config_dir(); 55 | // Lin: /home/alice/.config/barapp 56 | // Win: C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\config 57 | // Mac: /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App 58 | } 59 | 60 | if let Some(base_dirs) = BaseDirs::new() { 61 | base_dirs.executable_dir(); 62 | // Lin: Some(/home/alice/.local/bin) 63 | // Win: None 64 | // Mac: None 65 | } 66 | 67 | if let Some(user_dirs) = UserDirs::new() { 68 | user_dirs.audio_dir(); 69 | // Lin: /home/alice/Music 70 | // Win: C:\Users\Alice\Music 71 | // Mac: /Users/Alice/Music 72 | } 73 | ``` 74 | 75 | ## Design Goals 76 | 77 | - The _directories_ library is designed to provide an accurate snapshot of the system's state at 78 | the point of invocation of `BaseDirs::new`, `UserDirs::new` or `ProjectDirs::from`.
79 | Subsequent changes to the state of the system are not reflected in values created prior to such a change. 80 | - This library does not create directories or check for their existence. The library only provides 81 | information on what the path to a certain directory _should_ be.
82 | How this information is used is a decision that developers need to make based on the requirements 83 | of each individual application. 84 | - This library is intentionally focused on providing information on user-writable directories only, 85 | as there is no discernible benefit in returning a path that points to a user-level, writable 86 | directory on one operating system, but a system-level, read-only directory on another.
87 | The confusion and unexpected failure modes of such an approach would be immense. 88 | - `executable_dir` is specified to provide the path to a user-writable directory for binaries.
89 | As such a directory only commonly exists on Linux, it returns `None` on macOS and Windows. 90 | - `font_dir` is specified to provide the path to a user-writable directory for fonts.
91 | As such a directory only exists on Linux and macOS, it returns `None` on Windows. 92 | - `runtime_dir` is specified to provide the path to a directory for non-essential runtime data. 93 | It is required that this directory is created when the user logs in, is only accessible by the 94 | user itself, is deleted when the user logs out, and supports all filesystem features of the 95 | operating system.
96 | As such a directory only commonly exists on Linux, it returns `None` on macOS and Windows. 97 | 98 | ## Features 99 | 100 | ### `BaseDirs` 101 | 102 | The intended use case for `BaseDirs` is to query the paths of user-invisible standard directories 103 | that have been defined according to the conventions of the operating system the library is running on. 104 | 105 | If you want to compute the location of cache, config or data directories for your own application or project, use `ProjectDirs` instead. 106 | 107 | | Function name | Value on Linux | Value on Windows | Value on macOS | 108 | |--------------------|----------------------------------------------------------| --------------------------- | ----------------------------------- | 109 | | `home_dir` | `$HOME` | `{FOLDERID_Profile}` | `$HOME` | 110 | | `cache_dir` | `$XDG_CACHE_HOME` or `$HOME`/.cache | `{FOLDERID_LocalAppData}` | `$HOME`/Library/Caches | 111 | | `config_dir` | `$XDG_CONFIG_HOME` or `$HOME`/.config | `{FOLDERID_RoamingAppData}` | `$HOME`/Library/Application Support | 112 | | `config_local_dir` | `$XDG_CONFIG_HOME` or `$HOME`/.config | `{FOLDERID_LocalAppData}` | `$HOME`/Library/Application Support | 113 | | `data_dir` | `$XDG_DATA_HOME` or `$HOME`/.local/share | `{FOLDERID_RoamingAppData}` | `$HOME`/Library/Application Support | 114 | | `data_local_dir` | `$XDG_DATA_HOME` or `$HOME`/.local/share | `{FOLDERID_LocalAppData}` | `$HOME`/Library/Application Support | 115 | | `executable_dir` | `Some($XDG_BIN_HOME)` or `Some($HOME`/.local/bin`)` | `None` | `None` | 116 | | `preference_dir` | `$XDG_CONFIG_HOME` or `$HOME`/.config | `{FOLDERID_RoamingAppData}` | `$HOME`/Library/Preferences | 117 | | `runtime_dir` | `Some($XDG_RUNTIME_DIR)` or `None` | `None` | `None` | 118 | | `state_dir` | `Some($XDG_STATE_HOME)` or `Some($HOME`/.local/state`)` | `None` | `None` | 119 | 120 | ### `UserDirs` 121 | 122 | The intended use case for `UserDirs` is to query the paths of user-facing standard directories 123 | that have been defined according to the conventions of the operating system the library is running on. 124 | 125 | | Function name | Value on Linux | Value on Windows | Value on macOS | 126 | | ---------------- | ---------------------------------------------------------------------- | -------------------------------- | ------------------------------ | 127 | | `home_dir` | `$HOME` | `{FOLDERID_Profile}` | `$HOME` | 128 | | `audio_dir` | `Some(XDG_MUSIC_DIR)` or `None` | `Some({FOLDERID_Music})` | `Some($HOME`/Music/`)` | 129 | | `desktop_dir` | `Some(XDG_DESKTOP_DIR)` or `None` | `Some({FOLDERID_Desktop})` | `Some($HOME`/Desktop/`)` | 130 | | `document_dir` | `Some(XDG_DOCUMENTS_DIR)` or `None` | `Some({FOLDERID_Documents})` | `Some($HOME`/Documents/`)` | 131 | | `download_dir` | `Some(XDG_DOWNLOAD_DIR)` or `None` | `Some({FOLDERID_Downloads})` | `Some($HOME`/Downloads/`)` | 132 | | `font_dir` | `Some($XDG_DATA_HOME`/fonts/`)` or `Some($HOME`/.local/share/fonts/`)` | `None` | `Some($HOME`/Library/Fonts/`)` | 133 | | `picture_dir` | `Some(XDG_PICTURES_DIR)` or `None` | `Some({FOLDERID_Pictures})` | `Some($HOME`/Pictures/`)` | 134 | | `public_dir` | `Some(XDG_PUBLICSHARE_DIR)` or `None` | `Some({FOLDERID_Public})` | `Some($HOME`/Public/`)` | 135 | | `template_dir` | `Some(XDG_TEMPLATES_DIR)` or `None` | `Some({FOLDERID_Templates})` | `None` | 136 | | `video_dir` | `Some(XDG_VIDEOS_DIR)` or `None` | `Some({FOLDERID_Videos})` | `Some($HOME`/Movies/`)` | 137 | 138 | ### `ProjectDirs` 139 | 140 | The intended use case for `ProjectDirs` is to compute the location of cache, config or data directories for your own application or project, 141 | which are derived from the standard directories. 142 | 143 | | Function name | Value on Linux | Value on Windows | Value on macOS | 144 | |--------------------|------------------------------------------------------------------------------------|-----------------------------------------------------| ---------------------------------------------------- | 145 | | `cache_dir` | `$XDG_CACHE_HOME`/`` or `$HOME`/.cache/`` | `{FOLDERID_LocalAppData}`/``/cache | `$HOME`/Library/Caches/`` | 146 | | `config_dir` | `$XDG_CONFIG_HOME`/`` or `$HOME`/.config/`` | `{FOLDERID_RoamingAppData}`/``/config | `$HOME`/Library/Application Support/`` | 147 | | `config_local_dir` | `$XDG_CONFIG_HOME`/`` or `$HOME`/.config/`` | `{FOLDERID_LocalAppData}`/``/config | `$HOME`/Library/Application Support/`` | 148 | | `data_dir` | `$XDG_DATA_HOME`/`` or `$HOME`/.local/share/`` | `{FOLDERID_RoamingAppData}`/``/data | `$HOME`/Library/Application Support/`` | 149 | | `data_local_dir` | `$XDG_DATA_HOME`/`` or `$HOME`/.local/share/`` | `{FOLDERID_LocalAppData}`/``/data | `$HOME`/Library/Application Support/`` | 150 | | `preference_dir` | `$XDG_CONFIG_HOME`/`` or `$HOME`/.config/`` | `{FOLDERID_RoamingAppData}`/``/config | `$HOME`/Library/Preferences/`` | 151 | | `runtime_dir` | `Some($XDG_RUNTIME_DIR`/`)` | `None` | `None` | 152 | | `state_dir` | `Some($XDG_STATE_HOME`/`)` or `$HOME`/.local/state/`` | `None` | `None` | 153 | 154 | The specific value of `` is computed by the 155 | 156 | ProjectDirs::from(qualifier: &str, 157 | organization: &str, 158 | application: &str) 159 | 160 | function and varies across operating systems. As an example, calling 161 | 162 | ProjectDirs::from("org" /*qualifier*/, 163 | "Baz Corp" /*organization*/, 164 | "Foo Bar-App" /*application*/) 165 | 166 | results in the following values: 167 | 168 | | Value on Linux | Value on Windows | Value on macOS | 169 | | -------------- | ------------------------ | ---------------------------- | 170 | | `"foobar-app"` | `"Baz Corp/Foo Bar-App"` | `"org.Baz-Corp.Foo-Bar-App"` | 171 | 172 | The `ProjectDirs::from_path` function allows the creation of `ProjectDirs` structs directly from a `PathBuf` value. 173 | This argument is used verbatim and is not adapted to operating system standards. 174 | 175 | The use of `ProjectDirs::from_path` is strongly discouraged, as its results will not follow operating system standards on at least two of three platforms. 176 | 177 | ## Comparison 178 | 179 | There are other crates in the Rust ecosystem that try similar or related things. 180 | Here is an overview of them, combined with ratings on properties that guided the design of this crate. 181 | 182 | Please take this table with a grain of salt: a different crate might very well be more suitable for your specific use case. 183 | (Of course _my_ crate achieves _my_ design goals better than other crates, which might have had different design goals.) 184 | 185 | | Library | Status | Lin | Mac | Win |Base|User|Proj|Conv| 186 | | --------------------------------------------------------- | -------------- |:---:|:---:|:---:|:--:|:--:|:--:|:--:| 187 | | [app_dirs](https://crates.io/crates/app_dirs) | Unmaintained | ✔ | ✔ | ✔ | 🞈 | ✖ | ✔ | ✖ | 188 | | [app_dirs2](https://crates.io/crates/app_dirs2) | Maintained | ✔ | ✔ | ✔ | 🞈 | ✖ | ✔ | ✖ | 189 | | [dirs](https://crates.io/crates/dirs) | Developed | ✔ | ✔ | ✔ | ✔ | ✔ | ✖ | ✔ | 190 | | **directories** | **Developed** | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | 191 | | [s_app_dir](https://crates.io/crates/s_app_dir) | Unmaintained? | ✔ | ✖ | 🞈 | ✖ | ✖ | 🞈 | ✖ | 192 | | [standard_paths](https://crates.io/crates/standard_paths) | Maintained | ✔ | ✖ | ✔ | ✔ | ✔ | ✔ | ✖ | 193 | | [xdg](https://crates.io/crates/xdg) | Maintained | ✔ | ✖ | ✖ | ✔ | ✖ | ✔ | 🞈 | 194 | | [xdg-basedir](https://crates.io/crates/xdg-basedir) | Unmaintained? | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | 🞈 | 195 | | [xdg-rs](https://crates.io/crates/xdg-rs) | Obsolete | ✔ | ✖ | ✖ | ✔ | ✖ | ✖ | 🞈 | 196 | 197 | - Lin: Linux support 198 | - Mac: macOS support 199 | - Win: Windows support 200 | - Base: Supports [generic base directories](#basedirs) 201 | - User: Supports [user directories](#userdirs) 202 | - Proj: Supports [project-specific base directories](#projectdirs) 203 | - Conv: Follows naming conventions of the operating system it runs on 204 | 205 | ## Build 206 | 207 | It's possible to cross-compile this library if the necessary toolchains are installed with rustup. 208 | This is helpful to ensure a change has not broken compilation on a different platform. 209 | 210 | The following commands will build this library on Linux, macOS and Windows: 211 | 212 | ``` 213 | cargo build --target=x86_64-unknown-linux-gnu 214 | cargo build --target=x86_64-pc-windows-gnu 215 | cargo build --target=x86_64-apple-darwin 216 | cargo build --target=x86_64-unknown-redox 217 | ``` 218 | 219 | ## Changelog 220 | 221 | ### 6 222 | 223 | - Update `dirs-sys` dependency to `0.5.0`, which in turn updates `windows-sys` dependency to `0.59.0`. 224 | 225 | ### 5 226 | 227 | - Update `dirs-sys` dependency to `0.4.0`. 228 | - Add `config_local_dir` for non-roaming configuration on Windows. On non-Windows platforms the behavior is identical to `config dir`. 229 | 230 | ### 4 231 | 232 | - **BREAKING CHANGE** The behavior of `executable_dir` has been adjusted to not depend on `$XDG_DATA_HOME`. 233 | Code, which assumed that setting the `$XDG_DATA_HOME` environment variable also impacted `executable_dir` if 234 | the `$XDG_BIN_HOME` environment variable was not set, requires adjustment. 235 | - Add support for `XDG_STATE_HOME`. 236 | 237 | ### 3 238 | 239 | - **BREAKING CHANGE** The behavior of the `BaseDirs::config_dir` and `ProjectDirs::config_dir` 240 | on macOS has been adjusted (thanks to [everyone involved](https://github.com/dirs-dev/directories-rs/issues/62)): 241 | - The existing `config_dir` functions have been changed to return the `Application Support` 242 | directory on macOS, as suggested by Apple documentation. 243 | - The behavior of the `config_dir` functions on non-macOS platforms has not been changed. 244 | - If you have used the `config_dir` functions to store files, it may be necessary to write code 245 | that migrates the files to the new location on macOS.
246 | (Alternative: change uses of the `config_dir` functions to uses of the `preference_dir` functions 247 | to retain the old behavior.) 248 | - The newly added `BaseDirs::preference_dir` and `ProjectDirs::preference_dir` functions returns 249 | the `Preferences` directory on macOS now, which – according to Apple documentation – shall only 250 | be used to store .plist files using Apple-proprietary APIs. 251 | – `preference_dir` and `config_dir` behave identical on non-macOS platforms. 252 | 253 | ### 2 254 | 255 | **BREAKING CHANGE** The behavior of deactivated, missing or invalid [_XDG User Dirs_](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/) 256 | entries on Linux has been improved (contributed by @tmiasko, thank you!): 257 | 258 | - Version 1 returned the user's home directory (`Some($HOME)`) for such faulty entries, except for a faulty `XDG_DESKTOP_DIR` entry which returned (`Some($HOME/Desktop)`). 259 | - Version 2 returns `None` for such entries. 260 | 261 | ## License 262 | 263 | Licensed under either of 264 | 265 | * Apache License, Version 2.0 266 | ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 267 | * MIT license 268 | ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 269 | 270 | at your option. 271 | 272 | ## Contribution 273 | 274 | Unless you explicitly state otherwise, any contribution intentionally submitted 275 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be 276 | dual licensed as above, without any additional terms or conditions. 277 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! The _directories_ crate is 2 | //! 3 | //! - a tiny library with a minimal API (3 structs, 4 factory functions, getters) 4 | //! - that provides the platform-specific, user-accessible locations 5 | //! - for finding and storing configuration, cache and other data 6 | //! - on Linux, Redox, Windows (≥ Vista) and macOS. 7 | //! 8 | //! The library provides the location of these directories by leveraging the mechanisms defined by 9 | //! 10 | //! - 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, 11 | //! - the [Known Folder](https://msdn.microsoft.com/en-us/library/windows/desktop/bb776911(v=vs.85).aspx) system on Windows, and 12 | //! - 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. 13 | //! 14 | 15 | #![deny(missing_docs)] 16 | 17 | use std::path::Path; 18 | use std::path::PathBuf; 19 | 20 | #[cfg(target_os = "windows")] 21 | mod win; 22 | #[cfg(target_os = "windows")] 23 | use win as sys; 24 | #[cfg(any(target_os = "macos", target_os = "ios"))] 25 | mod mac; 26 | #[cfg(any(target_os = "macos", target_os = "ios"))] 27 | use mac as sys; 28 | #[cfg(target_arch = "wasm32")] 29 | mod wasm; 30 | #[cfg(target_arch = "wasm32")] 31 | use wasm as sys; 32 | #[cfg(not(any( 33 | target_os = "windows", 34 | target_os = "macos", target_os = "ios", 35 | target_arch = "wasm32" 36 | )))] 37 | mod lin; 38 | #[cfg(not(any( 39 | target_os = "windows", 40 | target_os = "macos", target_os = "ios", 41 | target_arch = "wasm32" 42 | )))] 43 | use lin as sys; 44 | 45 | /// `BaseDirs` provides paths of user-invisible standard directories, following the conventions of the operating system the library is running on. 46 | /// 47 | /// To compute the location of cache, config or data directories for individual projects or applications, use `ProjectDirs` instead. 48 | /// 49 | /// # Examples 50 | /// 51 | /// All examples on this page are computed with a user named _Alice_. 52 | /// 53 | /// ``` 54 | /// use directories::BaseDirs; 55 | /// if let Some(base_dirs) = BaseDirs::new() { 56 | /// base_dirs.config_dir(); 57 | /// // Linux: /home/alice/.config 58 | /// // Windows: C:\Users\Alice\AppData\Roaming 59 | /// // macOS: /Users/Alice/Library/Application Support 60 | /// } 61 | /// ``` 62 | #[derive(Debug, Clone)] 63 | pub struct BaseDirs { 64 | // home directory 65 | home_dir: PathBuf, 66 | 67 | // base directories 68 | cache_dir: PathBuf, 69 | config_dir: PathBuf, 70 | config_local_dir: PathBuf, 71 | data_dir: PathBuf, 72 | data_local_dir: PathBuf, 73 | executable_dir: Option, 74 | preference_dir: PathBuf, 75 | runtime_dir: Option, 76 | state_dir: Option 77 | } 78 | 79 | /// `UserDirs` provides paths of user-facing standard directories, following the conventions of the operating system the library is running on. 80 | /// 81 | /// # Examples 82 | /// 83 | /// All examples on this page are computed with a user named _Alice_. 84 | /// 85 | /// ``` 86 | /// use directories::UserDirs; 87 | /// if let Some(user_dirs) = UserDirs::new() { 88 | /// user_dirs.audio_dir(); 89 | /// // Linux: /home/alice/Music 90 | /// // Windows: C:\Users\Alice\Music 91 | /// // macOS: /Users/Alice/Music 92 | /// } 93 | /// ``` 94 | #[derive(Debug, Clone)] 95 | pub struct UserDirs { 96 | // home directory 97 | home_dir: PathBuf, 98 | 99 | // user directories 100 | audio_dir: Option, 101 | desktop_dir: Option, 102 | document_dir: Option, 103 | download_dir: Option, 104 | font_dir: Option, 105 | picture_dir: Option, 106 | public_dir: Option, 107 | template_dir: Option, 108 | // trash_dir: PathBuf, 109 | video_dir: Option 110 | } 111 | 112 | /// `ProjectDirs` computes the location of cache, config or data directories for a specific application, 113 | /// which are derived from the standard directories and the name of the project/organization. 114 | /// 115 | /// # Examples 116 | /// 117 | /// All examples on this page are computed with a user named _Alice_, 118 | /// and a `ProjectDirs` struct created with `ProjectDirs::from("com", "Foo Corp", "Bar App")`. 119 | /// 120 | /// ``` 121 | /// use directories::ProjectDirs; 122 | /// if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") { 123 | /// proj_dirs.config_dir(); 124 | /// // Linux: /home/alice/.config/barapp 125 | /// // Windows: C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App 126 | /// // macOS: /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App 127 | /// } 128 | /// ``` 129 | #[derive(Debug, Clone)] 130 | pub struct ProjectDirs { 131 | project_path: PathBuf, 132 | 133 | // base directories 134 | cache_dir: PathBuf, 135 | config_dir: PathBuf, 136 | config_local_dir: PathBuf, 137 | data_dir: PathBuf, 138 | data_local_dir: PathBuf, 139 | preference_dir: PathBuf, 140 | runtime_dir: Option, 141 | state_dir: Option 142 | } 143 | 144 | impl BaseDirs { 145 | /// Creates a `BaseDirs` struct which holds the paths to user-invisible directories for cache, config, etc. data on the system. 146 | /// 147 | /// The returned value depends on the operating system and is either 148 | /// - `Some`, containing a snapshot of the state of the system's paths at the time `new()` was invoked, or 149 | /// - `None`, if no valid home directory path could be retrieved from the operating system. 150 | /// 151 | /// To determine whether a system provides a valid `$HOME` path, the following rules are applied: 152 | /// 153 | /// ### Linux and macOS: 154 | /// 155 | /// - Use `$HOME` if it is set and not empty. 156 | /// - If `$HOME` is not set or empty, then the function `getpwuid_r` is used to determine 157 | /// the home directory of the current user. 158 | /// - If `getpwuid_r` lacks an entry for the current user id or the home directory field is empty, 159 | /// then the function returns `None`. 160 | /// 161 | /// ### Windows: 162 | /// 163 | /// - Retrieve the user profile folder using `SHGetKnownFolderPath`. 164 | /// - If this fails, then the function returns `None`. 165 | /// 166 | /// _Note:_ This logic differs from [`std::env::home_dir`], 167 | /// which works incorrectly on Linux, macOS and Windows. 168 | /// 169 | /// [`std::env::home_dir`]: https://doc.rust-lang.org/std/env/fn.home_dir.html 170 | pub fn new() -> Option { 171 | sys::base_dirs() 172 | } 173 | /// Returns the path to the user's home directory. 174 | /// 175 | /// |Platform | Value | Example | 176 | /// | ------- | -------------------- | -------------- | 177 | /// | Linux | `$HOME` | /home/alice | 178 | /// | macOS | `$HOME` | /Users/Alice | 179 | /// | Windows | `{FOLDERID_Profile}` | C:\Users\Alice | 180 | pub fn home_dir(&self) -> &Path { 181 | self.home_dir.as_path() 182 | } 183 | /// Returns the path to the user's cache directory. 184 | /// 185 | /// |Platform | Value | Example | 186 | /// | ------- | ----------------------------------- | ---------------------------- | 187 | /// | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache | 188 | /// | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches | 189 | /// | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | 190 | pub fn cache_dir(&self) -> &Path { 191 | self.cache_dir.as_path() 192 | } 193 | /// Returns the path to the user's config directory. 194 | /// 195 | /// |Platform | Value | Example | 196 | /// | ------- | ------------------------------------- | ---------------------------------------- | 197 | /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config | 198 | /// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | 199 | /// | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | 200 | pub fn config_dir(&self) -> &Path { 201 | self.config_dir.as_path() 202 | } 203 | /// Returns the path to the user's local config directory. 204 | /// 205 | /// |Platform | Value | Example | 206 | /// | ------- | ------------------------------------- | ---------------------------------------- | 207 | /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config | 208 | /// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | 209 | /// | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | 210 | pub fn config_local_dir(&self) -> &Path { 211 | self.config_local_dir.as_path() 212 | } 213 | /// Returns the path to the user's data directory. 214 | /// 215 | /// |Platform | Value | Example | 216 | /// | ------- | ---------------------------------------- | ---------------------------------------- | 217 | /// | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | 218 | /// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | 219 | /// | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | 220 | pub fn data_dir(&self) -> &Path { 221 | self.data_dir.as_path() 222 | } 223 | /// Returns the path to the user's local data directory. 224 | /// 225 | /// |Platform | Value | Example | 226 | /// | ------- | ---------------------------------------- | ---------------------------------------- | 227 | /// | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | 228 | /// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | 229 | /// | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | 230 | pub fn data_local_dir(&self) -> &Path { 231 | self.data_local_dir.as_path() 232 | } 233 | /// Returns the path to the user's executable directory. 234 | /// 235 | /// |Platform | Value | Example | 236 | /// | ------- | ---------------------------------------------------------------- | ---------------------- | 237 | /// | Linux | `$XDG_BIN_HOME` or `$XDG_DATA_HOME`/../bin or `$HOME`/.local/bin | /home/alice/.local/bin | 238 | /// | macOS | – | – | 239 | /// | Windows | – | – | 240 | pub fn executable_dir(&self) -> Option<&Path> { 241 | self.executable_dir.as_ref().map(|p| p.as_path()) 242 | } 243 | /// Returns the path to the user's preference directory. 244 | /// 245 | /// |Platform | Value | Example | 246 | /// | ------- | ------------------------------------- | -------------------------------- | 247 | /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config | 248 | /// | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences | 249 | /// | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | 250 | pub fn preference_dir(&self) -> &Path { 251 | self.preference_dir.as_path() 252 | } 253 | /// Returns the path to the user's runtime directory. 254 | /// 255 | /// |Platform | Value | Example | 256 | /// | ------- | ------------------ | --------------- | 257 | /// | Linux | `$XDG_RUNTIME_DIR` | /run/user/1001/ | 258 | /// | macOS | – | – | 259 | /// | Windows | – | – | 260 | pub fn runtime_dir(&self) -> Option<&Path> { 261 | self.runtime_dir.as_ref().map(|p| p.as_path()) 262 | } 263 | /// Returns the path to the user's state directory. 264 | /// 265 | /// The state directory contains data that should be retained between sessions (unlike the runtime 266 | /// directory), but may not be important/portable enough to be synchronized across machines (unlike 267 | /// the config/preferences/data directories). 268 | /// 269 | /// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`. 270 | /// 271 | /// |Platform | Value | Example | 272 | /// | ------- | ----------------------------------------- | ------------------------ | 273 | /// | Linux | `$XDG_STATE_HOME` or `$HOME`/.local/state | /home/alice/.local/state | 274 | /// | macOS | – | – | 275 | /// | Windows | – | – | 276 | pub fn state_dir(&self) -> Option<&Path> { 277 | self.state_dir.as_ref().map(|p| p.as_path()) 278 | } 279 | } 280 | 281 | impl UserDirs { 282 | /// Creates a `UserDirs` struct which holds the paths to user-facing directories for audio, font, video, etc. data on the system. 283 | /// 284 | /// The returned value depends on the operating system and is either 285 | /// - `Some`, containing a snapshot of the state of the system's paths at the time `new()` was invoked, or 286 | /// - `None`, if no valid home directory path could be retrieved from the operating system. 287 | /// 288 | /// To determine whether a system provides a valid `$HOME` path, please refer to [`BaseDirs::new`] 289 | pub fn new() -> Option { 290 | sys::user_dirs() 291 | } 292 | /// Returns the path to the user's home directory. 293 | /// 294 | /// |Platform | Value | Example | 295 | /// | ------- | -------------------- | -------------- | 296 | /// | Linux | `$HOME` | /home/alice | 297 | /// | macOS | `$HOME` | /Users/Alice | 298 | /// | Windows | `{FOLDERID_Profile}` | C:\Users\Alice | 299 | pub fn home_dir(&self) -> &Path { 300 | self.home_dir.as_path() 301 | } 302 | /// Returns the path to the user's audio directory. 303 | /// 304 | /// |Platform | Value | Example | 305 | /// | ------- | ------------------ | -------------------- | 306 | /// | Linux | `XDG_MUSIC_DIR` | /home/alice/Music | 307 | /// | macOS | `$HOME`/Music | /Users/Alice/Music | 308 | /// | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music | 309 | pub fn audio_dir(&self) -> Option<&Path> { 310 | self.audio_dir.as_ref().map(|p| p.as_path()) 311 | } 312 | /// Returns the path to the user's desktop directory. 313 | /// 314 | /// |Platform | Value | Example | 315 | /// | ------- | -------------------- | ---------------------- | 316 | /// | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop | 317 | /// | macOS | `$HOME`/Desktop | /Users/Alice/Desktop | 318 | /// | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop | 319 | pub fn desktop_dir(&self) -> Option<&Path> { 320 | self.desktop_dir.as_ref().map(|p| p.as_path()) 321 | } 322 | /// Returns the path to the user's document directory. 323 | /// 324 | /// |Platform | Value | Example | 325 | /// | ------- | ---------------------- | ------------------------ | 326 | /// | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents | 327 | /// | macOS | `$HOME`/Documents | /Users/Alice/Documents | 328 | /// | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents | 329 | pub fn document_dir(&self) -> Option<&Path> { 330 | self.document_dir.as_ref().map(|p| p.as_path()) 331 | } 332 | /// Returns the path to the user's download directory. 333 | /// 334 | /// |Platform | Value | Example | 335 | /// | ------- | ---------------------- | ------------------------ | 336 | /// | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads | 337 | /// | macOS | `$HOME`/Downloads | /Users/Alice/Downloads | 338 | /// | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads | 339 | pub fn download_dir(&self) -> Option<&Path> { 340 | self.download_dir.as_ref().map(|p| p.as_path()) 341 | } 342 | /// Returns the path to the user's font directory. 343 | /// 344 | /// |Platform | Value | Example | 345 | /// | ------- | ---------------------------------------------------- | ------------------------------ | 346 | /// | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts | 347 | /// | macOS | `$HOME`/Library/Fonts | /Users/Alice/Library/Fonts | 348 | /// | Windows | – | – | 349 | pub fn font_dir(&self) -> Option<&Path> { 350 | self.font_dir.as_ref().map(|p| p.as_path()) 351 | } 352 | /// Returns the path to the user's picture directory. 353 | /// 354 | /// |Platform | Value | Example | 355 | /// | ------- | --------------------- | ----------------------- | 356 | /// | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures | 357 | /// | macOS | `$HOME`/Pictures | /Users/Alice/Pictures | 358 | /// | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures | 359 | pub fn picture_dir(&self) -> Option<&Path> { 360 | self.picture_dir.as_ref().map(|p| p.as_path()) 361 | } 362 | /// Returns the path to the user's public directory. 363 | /// 364 | /// |Platform | Value | Example | 365 | /// | ------- | --------------------- | ------------------- | 366 | /// | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public | 367 | /// | macOS | `$HOME`/Public | /Users/Alice/Public | 368 | /// | Windows | `{FOLDERID_Public}` | C:\Users\Public | 369 | pub fn public_dir(&self) -> Option<&Path> { 370 | self.public_dir.as_ref().map(|p| p.as_path()) 371 | } 372 | /// Returns the path to the user's template directory. 373 | /// 374 | /// |Platform | Value | Example | 375 | /// | ------- | ---------------------- | ---------------------------------------------------------- | 376 | /// | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates | 377 | /// | macOS | – | – | 378 | /// | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates | 379 | pub fn template_dir(&self) -> Option<&Path> { 380 | self.template_dir.as_ref().map(|p| p.as_path()) 381 | } 382 | /// Returns the path to the user's video directory. 383 | /// 384 | /// |Platform | Value | Example | 385 | /// | ------- | ------------------- | --------------------- | 386 | /// | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos | 387 | /// | macOS | `$HOME`/Movies | /Users/Alice/Movies | 388 | /// | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos | 389 | pub fn video_dir(&self) -> Option<&Path> { 390 | self.video_dir.as_ref().map(|p| p.as_path()) 391 | } 392 | } 393 | 394 | impl ProjectDirs { 395 | /// Creates a `ProjectDirs` struct directly from a `PathBuf` value. 396 | /// The argument is used verbatim and is not adapted to operating system standards. 397 | /// 398 | /// The use of `ProjectDirs::from_path` is strongly discouraged, as its results will 399 | /// not follow operating system standards on at least two of three platforms. 400 | /// 401 | /// Use [`ProjectDirs::from`] instead. 402 | pub fn from_path(project_path: PathBuf) -> Option { 403 | sys::project_dirs_from_path(project_path) 404 | } 405 | /// Creates a `ProjectDirs` struct from values describing the project. 406 | /// 407 | /// The returned value depends on the operating system and is either 408 | /// - `Some`, containing project directory paths based on the state of the system's paths at the time `new()` was invoked, or 409 | /// - `None`, if no valid home directory path could be retrieved from the operating system. 410 | /// 411 | /// To determine whether a system provides a valid `$HOME` path, please refer to [`BaseDirs::new`] 412 | /// 413 | /// The use of `ProjectDirs::from` (instead of `ProjectDirs::from_path`) is strongly encouraged, 414 | /// as its results will follow operating system standards on Linux, macOS and Windows. 415 | /// 416 | /// # Parameters 417 | /// 418 | /// - `qualifier` – The reverse domain name notation of the application, excluding the organization or application name itself.
419 | /// An empty string can be passed if no qualifier should be used (only affects macOS).
420 | /// Example values: `"com.example"`, `"org"`, `"uk.co"`, `"io"`, `""` 421 | /// - `organization` – The name of the organization that develops this application, or for which the application is developed.
422 | /// An empty string can be passed if no organization should be used (only affects macOS and Windows).
423 | /// Example values: `"Foo Corp"`, `"Alice and Bob Inc"`, `""` 424 | /// - `application` – The name of the application itself.
425 | /// Example values: `"Bar App"`, `"ExampleProgram"`, `"Unicorn-Programme"` 426 | /// 427 | /// [`BaseDirs::home_dir`]: struct.BaseDirs.html#method.home_dir 428 | pub fn from(qualifier: &str, organization: &str, application: &str) -> Option { 429 | sys::project_dirs_from(qualifier, organization, application) 430 | } 431 | /// Returns the project path fragment used to compute the project's cache/config/data directories. 432 | /// The value is derived from the `ProjectDirs::from` call and is platform-dependent. 433 | pub fn project_path(&self) -> &Path { 434 | self.project_path.as_path() 435 | } 436 | /// Returns the path to the project's cache directory. 437 | /// 438 | /// |Platform | Value | Example | 439 | /// | ------- | --------------------------------------------------------------------- | --------------------------------------------------- | 440 | /// | Linux | `$XDG_CACHE_HOME`/`_project_path_` or `$HOME`/.cache/`_project_path_` | /home/alice/.cache/barapp | 441 | /// | macOS | `$HOME`/Library/Caches/`_project_path_` | /Users/Alice/Library/Caches/com.Foo-Corp.Bar-App | 442 | /// | Windows | `{FOLDERID_LocalAppData}`\\`_project_path_`\\cache | C:\Users\Alice\AppData\Local\Foo Corp\Bar App\cache | 443 | pub fn cache_dir(&self) -> &Path { 444 | self.cache_dir.as_path() 445 | } 446 | /// Returns the path to the project's config directory. 447 | /// 448 | /// |Platform | Value | Example | 449 | /// | ------- | ----------------------------------------------------------------------- | -------------------------------------------------------------- | 450 | /// | Linux | `$XDG_CONFIG_HOME`/`_project_path_` or `$HOME`/.config/`_project_path_` | /home/alice/.config/barapp | 451 | /// | macOS | `$HOME`/Library/Application Support/`_project_path_` | /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App | 452 | /// | Windows | `{FOLDERID_RoamingAppData}`\\`_project_path_`\\config | C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\config | 453 | pub fn config_dir(&self) -> &Path { 454 | self.config_dir.as_path() 455 | } 456 | /// Returns the path to the project's local config directory. 457 | /// 458 | /// |Platform | Value | Example | 459 | /// | ------- | ----------------------------------------------------------------------- | -------------------------------------------------------------- | 460 | /// | Linux | `$XDG_CONFIG_HOME`/`_project_path_` or `$HOME`/.config/`_project_path_` | /home/alice/.config/barapp | 461 | /// | macOS | `$HOME`/Library/Application Support/`_project_path_` | /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App | 462 | /// | Windows | `{FOLDERID_LocalAppData}`\\`_project_path_`\\config | C:\Users\Alice\AppData\Local\Foo Corp\Bar App\config | 463 | pub fn config_local_dir(&self) -> &Path { 464 | self.config_local_dir.as_path() 465 | } 466 | /// Returns the path to the project's data directory. 467 | /// 468 | /// |Platform | Value | Example | 469 | /// | ------- | -------------------------------------------------------------------------- | ------------------------------------------------------------- | 470 | /// | Linux | `$XDG_DATA_HOME`/`_project_path_` or `$HOME`/.local/share/`_project_path_` | /home/alice/.local/share/barapp | 471 | /// | macOS | `$HOME`/Library/Application Support/`_project_path_` | /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App | 472 | /// | Windows | `{FOLDERID_RoamingAppData}`\\`_project_path_`\\data | C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\data | 473 | pub fn data_dir(&self) -> &Path { 474 | self.data_dir.as_path() 475 | } 476 | /// Returns the path to the project's local data directory. 477 | /// 478 | /// |Platform | Value | Example | 479 | /// | ------- | -------------------------------------------------------------------------- | ------------------------------------------------------------- | 480 | /// | Linux | `$XDG_DATA_HOME`/`_project_path_` or `$HOME`/.local/share/`_project_path_` | /home/alice/.local/share/barapp | 481 | /// | macOS | `$HOME`/Library/Application Support/`_project_path_` | /Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App | 482 | /// | Windows | `{FOLDERID_LocalAppData}`\\`_project_path_`\\data | C:\Users\Alice\AppData\Local\Foo Corp\Bar App\data | 483 | pub fn data_local_dir(&self) -> &Path { 484 | self.data_local_dir.as_path() 485 | } 486 | /// Returns the path to the project's preference directory. 487 | /// 488 | /// |Platform | Value | Example | 489 | /// | ------- | ----------------------------------------------------------------------- | ------------------------------------------------------ | 490 | /// | Linux | `$XDG_CONFIG_HOME`/`_project_path_` or `$HOME`/.config/`_project_path_` | /home/alice/.config/barapp | 491 | /// | macOS | `$HOME`/Library/Preferences/`_project_path_` | /Users/Alice/Library/Preferences/com.Foo-Corp.Bar-App | 492 | /// | Windows | `{FOLDERID_RoamingAppData}`\\`_project_path_`\\config | C:\Users\Alice\AppData\Roaming\Foo Corp\Bar App\config | 493 | pub fn preference_dir(&self) -> &Path { 494 | self.preference_dir.as_path() 495 | } 496 | /// Returns the path to the project's runtime directory. 497 | /// 498 | /// The runtime directory contains transient, non-essential data (like sockets or named pipes) that 499 | /// is expected to be cleared when the user's session ends. 500 | /// 501 | /// |Platform | Value | Example | 502 | /// | ------- | ----------------------------------- | --------------------- | 503 | /// | Linux | `$XDG_RUNTIME_DIR`/`_project_path_` | /run/user/1001/barapp | 504 | /// | macOS | – | – | 505 | /// | Windows | – | – | 506 | pub fn runtime_dir(&self) -> Option<&Path> { 507 | self.runtime_dir.as_ref().map(|p| p.as_path()) 508 | } 509 | /// Returns the path to the project's state directory. 510 | /// 511 | /// The state directory contains data that should be retained between sessions (unlike the runtime 512 | /// directory), but may not be important/portable enough to be synchronized across machines (unlike 513 | /// the config/preferences/data directories). 514 | /// 515 | /// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`. 516 | /// 517 | /// |Platform | Value | Example | 518 | /// | ------- | --------------------------------------------------------------------------- | ------------------------------- | 519 | /// | Linux | `$XDG_STATE_HOME`/`_project_path_` or `$HOME`/.local/state/`_project_path_` | /home/alice/.local/state/barapp | 520 | /// | macOS | – | – | 521 | /// | Windows | – | – | 522 | pub fn state_dir(&self) -> Option<&Path> { 523 | self.state_dir.as_ref().map(|p| p.as_path()) 524 | } 525 | } 526 | 527 | #[cfg(test)] 528 | mod tests { 529 | #[test] 530 | fn test_base_dirs() { 531 | println!("BaseDirs::new())\n{:?}", ::BaseDirs::new()); 532 | } 533 | 534 | #[test] 535 | fn test_user_dirs() { 536 | println!("UserDirs::new())\n{:?}", ::UserDirs::new()); 537 | } 538 | 539 | #[test] 540 | fn test_project_dirs() { 541 | let proj_dirs = ::ProjectDirs::from("qux", "FooCorp", "BarApp"); 542 | println!("ProjectDirs::from(\"qux\", \"FooCorp\", \"BarApp\")\n{:?}", proj_dirs); 543 | let proj_dirs = ::ProjectDirs::from("qux.zoo", "Foo Corp", "Bar-App"); 544 | println!("ProjectDirs::from(\"qux.zoo\", \"Foo Corp\", \"Bar-App\")\n{:?}", proj_dirs); 545 | let proj_dirs = ::ProjectDirs::from("com", "Foo Corp.", "Bar App"); 546 | println!("ProjectDirs::from(\"com\", \"Foo Corp.\", \"Bar App\")\n{:?}", proj_dirs); 547 | } 548 | } 549 | --------------------------------------------------------------------------------