1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
#![cfg(any(unix, target_os = "redox"))] //! This simple crate allows you to get paths to well known user directories, //! using [`xdg-user-dirs`][1]s `user-dirs.dirs` file. //! //! There are two ways of using this crate - with functions in the root of the //! crate, or with the [`UserDirs`] struct. [`UserDirs`] will read and parse the //! config file only once - when you call the [`UserDirs::new`] function. //! Functions in the root will read and parse the config file EVERY TIME you //! call them - so use them ONLY if you need to get one or two folders one or //! two times. //! //! # Example //! //! ```rust //! println!("Pictures folder: {:?}", xdg_user::pictures()?); //! println!("Music folder: {:?}", xdg_user::music()?); //! //! let dirs = xdg_user::UserDirs::new()?; //! println!("Documents folder: {:?}", dirs.documents()); //! println!("Downloads folder: {:?}", dirs.downloads()); //! ``` //! //! [1]: https://www.freedesktop.org/wiki/Software/xdg-user-dirs/ use std::path::{Path, PathBuf}; mod parser; mod utils; /// This crates main and only error type #[derive(Debug)] pub enum Error { /// Something went wrong while accessing the config file Io(std::io::Error), /// Unable to find the home directory NoHome, /// Error while parsing the config file Parse, } impl From<std::io::Error> for Error { fn from(e: std::io::Error) -> Self { Self::Io(e) } } impl From<std::str::Utf8Error> for Error { fn from(_: std::str::Utf8Error) -> Self { Self::Parse } } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Self::Io(e) => e.fmt(f), Self::NoHome => write!(f, "unable to find the home directory"), Self::Parse => write!(f, "error while parsing"), } } } impl std::error::Error for Error {} /// This crates main and only struct, allows you to access the paths to all the /// user directories pub struct UserDirs { desktop: Option<PathBuf>, documents: Option<PathBuf>, downloads: Option<PathBuf>, music: Option<PathBuf>, pictures: Option<PathBuf>, public: Option<PathBuf>, templates: Option<PathBuf>, videos: Option<PathBuf>, } impl std::fmt::Debug for UserDirs { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.debug_struct("UserDirs").finish() } } impl UserDirs { /// Attempts to read and parse the `${XDG_COFNIG_HOME}/user-dirs.dirs` file pub fn new() -> Result<Self, Error> { let mut this = Self { desktop: None, documents: None, downloads: None, music: None, pictures: None, public: None, templates: None, videos: None, }; utils::parse_file(|key, val| { match key { utils::DESKTOP => this.desktop = val, utils::DOCUMENTS => this.documents = val, utils::DOWNLOADS => this.downloads = val, utils::MUSIC => this.music = val, utils::PICTURES => this.pictures = val, utils::PUBLIC => this.public = val, utils::TEMPLATES => this.templates = val, utils::VIDEOS => this.videos = val, _ => {} } true })?; Ok(this) } /// Returns an absolute path to users desktop directory (`XDG_DESKTOP_DIR`), /// if found pub fn desktop(&self) -> Option<&Path> { self.desktop.as_deref() } /// Returns an absolute path to users documents directory /// (`XDG_DOCUMENTS_DIR`), if found pub fn documents(&self) -> Option<&Path> { self.documents.as_deref() } /// Returns an absolute path to users downloads directory /// (`XDG_DOWNLOAD_DIR`), if found pub fn downloads(&self) -> Option<&Path> { self.downloads.as_deref() } /// Returns an absolute path to users music directory (`XDG_MUSIC_DIR`), /// if found pub fn music(&self) -> Option<&Path> { self.music.as_deref() } /// Returns an absolute path to users pictures directory /// (`XDG_PICTURES_DIR`), if found pub fn pictures(&self) -> Option<&Path> { self.pictures.as_deref() } /// Returns an absolute path to users public share directory /// (`XDG_PUBLICSHARE_DIR`), if found pub fn public(&self) -> Option<&Path> { self.public.as_deref() } /// Returns an absolute path to users templates directory /// (`XDG_TEMPLATES_DIR`), if found pub fn templates(&self) -> Option<&Path> { self.templates.as_deref() } /// Returns an absolute path to users videos directory (`XDG_VIDEOS_DIR`), /// if found pub fn videos(&self) -> Option<&Path> { self.videos.as_deref() } } fn read_single(env: &[u8]) -> Result<Option<PathBuf>, Error> { let mut ret = None; utils::parse_file(|key, val| { if key == env { ret = val; false } else { true } })?; Ok(ret) } /// Returns an absolute path to users desktop directory (`XDG_DESKTOP_DIR`), if /// found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn desktop() -> Result<Option<PathBuf>, Error> { read_single(utils::DESKTOP) } /// Returns an absolute path to users documents directory (`XDG_DOCUMENTS_DIR`), /// if found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn documents() -> Result<Option<PathBuf>, Error> { read_single(utils::DOCUMENTS) } /// Returns an absolute path to users downloads directory (`XDG_DOWNLOAD_DIR`), /// if found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn downloads() -> Result<Option<PathBuf>, Error> { read_single(utils::DOWNLOADS) } /// Returns an absolute path to users music directory (`XDG_MUSIC_DIR`), if /// found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn music() -> Result<Option<PathBuf>, Error> { read_single(utils::MUSIC) } /// Returns an absolute path to users pictures directory (`XDG_PICTURES_DIR`), /// if found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn pictures() -> Result<Option<PathBuf>, Error> { read_single(utils::PICTURES) } /// Returns an absolute path to users public share directory /// (`XDG_PUBLICSHARE_DIR`), if found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn public() -> Result<Option<PathBuf>, Error> { read_single(utils::PUBLIC) } /// Returns an absolute path to users templates directory (`XDG_TEMPLATES_DIR`), /// if found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn templates() -> Result<Option<PathBuf>, Error> { read_single(utils::TEMPLATES) } /// Returns an absolute path to users videos directory (`XDG_VIDEOS_DIR`), if /// found /// /// # Warning /// /// This function will parse the `user-dirs.dirs` file every time it's called, /// so if you need paths to multiple different directories - consider using /// [`UserDirs`] instead pub fn videos() -> Result<Option<PathBuf>, Error> { read_single(utils::VIDEOS) }