diff --git a/src/lib.rs b/src/lib.rs index 0446887..6274813 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,5 @@ use std::{env, path::PathBuf}; -use eyre::Result; - /// Cross-platform directory resolver with XDG compliance. /// /// This struct provides methods to resolve standard directories across Linux, macOS, and Windows @@ -19,17 +17,19 @@ use eyre::Result; /// use dir_spec::Dir; /// /// // Get config directory (respects XDG_CONFIG_HOME if set) -/// let config = Dir::config_home()?; -/// println!("Config directory: {}", config.display()); +/// if let Some(config) = Dir::config_home() { +/// println!("Config directory: {}", config.display()); +/// } /// /// // Get cache directory (respects XDG_CACHE_HOME if set) -/// let cache = Dir::cache_home()?; -/// println!("Cache directory: {}", cache.display()); +/// if let Some(cache) = Dir::cache_home() { +/// println!("Cache directory: {}", cache.display()); +/// } /// /// // Get videos directory (new cleaner API) -/// let videos = Dir::videos()?; -/// println!("Videos directory: {}", videos.display()); -/// # Ok::<(), eyre::Error>(()) +/// if let Some(videos) = Dir::videos() { +/// println!("Videos directory: {}", videos.display()); +/// } /// ``` pub struct Dir; @@ -44,26 +44,23 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let bin_dir = Dir::bin_home()?; - /// # Ok(()) - /// # } + /// if let Some(bin_dir) = Dir::bin_home() { + /// println!("Bin directory: {}", bin_dir.display()); + /// } /// ``` - pub fn bin_home() -> Result { + pub fn bin_home() -> Option { if let Some(path) = Self::resolve_xdg_path("XDG_BIN_HOME") { - return Ok(path); + return Some(path); } #[cfg(any(target_os = "macos", target_os = "linux"))] { - Ok(Self::home()?.join(".local/bin")) + Self::home().map(|home| home.join(".local/bin")) } #[cfg(target_os = "windows")] { - env::var("LOCALAPPDATA") - .map(|path| PathBuf::from(path).join("Programs")) - .map_err(|_| eyre::eyre!("Failed to resolve bin directory")) + env::var("LOCALAPPDATA").ok().map(|path| PathBuf::from(path).join("Programs")) } } @@ -78,14 +75,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let cache_dir = Dir::cache_home()?; - /// # Ok(()) - /// # } + /// if let Some(cache_dir) = Dir::cache_home() { + /// println!("Cache directory: {}", cache_dir.display()); + /// } /// ``` - pub fn cache_home() -> Result { + pub fn cache_home() -> Option { Self::resolve_xdg_path("XDG_CACHE_HOME") - .map_or_else(|| Self::get_platform_default("Library/Caches", "LOCALAPPDATA", ".cache"), Ok) + .or_else(|| Self::get_platform_default("Library/Caches", "LOCALAPPDATA", ".cache")) } /// Returns the user's configuration directory. @@ -99,14 +95,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let config_dir = Dir::config_home()?; - /// # Ok(()) - /// # } + /// if let Some(config_dir) = Dir::config_home() { + /// println!("Config directory: {}", config_dir.display()); + /// } /// ``` - pub fn config_home() -> Result { + pub fn config_home() -> Option { Self::resolve_xdg_path("XDG_CONFIG_HOME") - .map_or_else(|| Self::get_platform_default("Library/Application Support", "APPDATA", ".config"), Ok) + .or_else(|| Self::get_platform_default("Library/Application Support", "APPDATA", ".config")) } /// Returns the user's data directory. @@ -120,14 +115,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let data_dir = Dir::data_home()?; - /// # Ok(()) - /// # } + /// if let Some(data_dir) = Dir::data_home() { + /// println!("Data directory: {}", data_dir.display()); + /// } /// ``` - pub fn data_home() -> Result { + pub fn data_home() -> Option { Self::resolve_xdg_path("XDG_DATA_HOME") - .map_or_else(|| Self::get_platform_default("Library/Application Support", "APPDATA", ".local/share"), Ok) + .or_else(|| Self::get_platform_default("Library/Application Support", "APPDATA", ".local/share")) } /// Returns the user's desktop directory. @@ -140,26 +134,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let desktop = Dir::desktop()?; - /// # Ok(()) - /// # } + /// if let Some(desktop) = Dir::desktop() { + /// println!("Desktop directory: {}", desktop.display()); + /// } /// ``` - pub fn desktop() -> Result { - Self::resolve_xdg_path("XDG_DESKTOP_DIR").map_or_else( - || Self::get_platform_default_with_windows_subdir("Desktop", "USERPROFILE", "Desktop", "Desktop"), - Ok, - ) - } - - /// Returns the user's desktop directory. - /// - /// **Deprecated**: Use [`Dir::desktop()`] instead. - /// - /// [`Dir::desktop()`]: Self::desktop - #[deprecated(since = "0.1.0", note = "Use `Dir::desktop()` instead")] - pub fn desktop_dir() -> Result { - Self::desktop() + pub fn desktop() -> Option { + Self::resolve_xdg_path("XDG_DESKTOP_DIR") + .or_else(|| Self::get_platform_default_with_windows_subdir("Desktop", "USERPROFILE", "Desktop", "Desktop")) } /// Returns the user's documents directory. @@ -172,26 +153,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let documents = Dir::documents()?; - /// # Ok(()) - /// # } + /// if let Some(documents) = Dir::documents() { + /// println!("Documents directory: {}", documents.display()); + /// } /// ``` - pub fn documents() -> Result { - Self::resolve_xdg_path("XDG_DOCUMENTS_DIR").map_or_else( - || Self::get_platform_default_with_windows_subdir("Documents", "USERPROFILE", "Documents", "Documents"), - Ok, - ) - } - - /// Returns the user's documents directory. - /// - /// **Deprecated**: Use [`Dir::documents()`] instead. - /// - /// [`Dir::documents()`]: Self::documents - #[deprecated(since = "0.1.0", note = "Use `Dir::documents()` instead")] - pub fn documents_dir() -> Result { - Self::documents() + pub fn documents() -> Option { + Self::resolve_xdg_path("XDG_DOCUMENTS_DIR") + .or_else(|| Self::get_platform_default_with_windows_subdir("Documents", "USERPROFILE", "Documents", "Documents")) } /// Returns the user's downloads directory. @@ -204,45 +172,29 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let downloads = Dir::downloads()?; - /// # Ok(()) - /// # } + /// if let Some(downloads) = Dir::downloads() { + /// println!("Downloads directory: {}", downloads.display()); + /// } /// ``` - pub fn downloads() -> Result { - Self::resolve_xdg_path("XDG_DOWNLOAD_DIR").map_or_else( - || Self::get_platform_default_with_windows_subdir("Downloads", "USERPROFILE", "Downloads", "Downloads"), - Ok, - ) - } - - /// Returns the user's downloads directory. - /// - /// **Deprecated**: Use [`Dir::downloads()`] instead. - /// - /// [`Dir::downloads()`]: Self::downloads - #[deprecated(since = "0.1.0", note = "Use `Dir::downloads()` instead")] - pub fn download_dir() -> Result { - Self::downloads() + pub fn downloads() -> Option { + Self::resolve_xdg_path("XDG_DOWNLOAD_DIR") + .or_else(|| Self::get_platform_default_with_windows_subdir("Downloads", "USERPROFILE", "Downloads", "Downloads")) } /// Returns the user's home directory. /// - /// Uses platform-specific methods to resolve the home directory: - /// - **Unix**: `$HOME` environment variable, falling back to passwd database lookup - /// - **Windows**: `%USERPROFILE%`, falling back to `%HOMEDRIVE%%HOMEPATH%` + /// Uses the standard library's `std::env::home_dir()` function. /// /// # Examples /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let home_dir = Dir::home()?; - /// # Ok(()) - /// # } + /// if let Some(home_dir) = Dir::home() { + /// println!("Home directory: {}", home_dir.display()); + /// } /// ``` - pub fn home() -> Result { - Ok(std::env::home_dir().unwrap()) + pub fn home() -> Option { + std::env::home_dir() } /// Returns the user's music directory. @@ -255,24 +207,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let music = Dir::music()?; - /// # Ok(()) - /// # } + /// if let Some(music) = Dir::music() { + /// println!("Music directory: {}", music.display()); + /// } /// ``` - pub fn music() -> Result { + pub fn music() -> Option { Self::resolve_xdg_path("XDG_MUSIC_DIR") - .map_or_else(|| Self::get_platform_default_with_windows_subdir("Music", "USERPROFILE", "Music", "Music"), Ok) - } - - /// Returns the user's music directory. - /// - /// **Deprecated**: Use [`Dir::music()`] instead. - /// - /// [`Dir::music()`]: Self::music - #[deprecated(since = "0.1.0", note = "Use `Dir::music()` instead")] - pub fn music_dir() -> Result { - Self::music() + .or_else(|| Self::get_platform_default_with_windows_subdir("Music", "USERPROFILE", "Music", "Music")) } /// Returns the user's pictures directory. @@ -285,26 +226,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let pictures = Dir::pictures()?; - /// # Ok(()) - /// # } + /// if let Some(pictures) = Dir::pictures() { + /// println!("Pictures directory: {}", pictures.display()); + /// } /// ``` - pub fn pictures() -> Result { - Self::resolve_xdg_path("XDG_PICTURES_DIR").map_or_else( - || Self::get_platform_default_with_windows_subdir("Pictures", "USERPROFILE", "Pictures", "Pictures"), - Ok, - ) - } - - /// Returns the user's pictures directory. - /// - /// **Deprecated**: Use [`Dir::pictures()`] instead. - /// - /// [`Dir::pictures()`]: Self::pictures - #[deprecated(since = "0.1.0", note = "Use `Dir::pictures()` instead")] - pub fn pictures_dir() -> Result { - Self::pictures() + pub fn pictures() -> Option { + Self::resolve_xdg_path("XDG_PICTURES_DIR") + .or_else(|| Self::get_platform_default_with_windows_subdir("Pictures", "USERPROFILE", "Pictures", "Pictures")) } /// Returns the user's public share directory. @@ -317,42 +245,31 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let public = Dir::publicshare()?; - /// # Ok(()) - /// # } + /// if let Some(public) = Dir::publicshare() { + /// println!("Public directory: {}", public.display()); + /// } /// ``` - pub fn publicshare() -> Result { + pub fn publicshare() -> Option { if let Some(path) = Self::resolve_xdg_path("XDG_PUBLICSHARE_DIR") { - return Ok(path); + return Some(path); } #[cfg(target_os = "macos")] { - Ok(Self::home()?.join("Public")) + Self::home().map(|home| home.join("Public")) } #[cfg(target_os = "windows")] { - Ok(PathBuf::from("C:\\Users\\Public")) + Some(PathBuf::from("C:\\Users\\Public")) } #[cfg(target_os = "linux")] { - Ok(Self::home()?.join("Public")) + Self::home().map(|home| home.join("Public")) } } - /// Returns the user's public share directory. - /// - /// **Deprecated**: Use [`Dir::publicshare()`] instead. - /// - /// [`Dir::publicshare()`]: Self::publicshare - #[deprecated(since = "0.1.0", note = "Use `Dir::publicshare()` instead")] - pub fn publicshare_dir() -> Result { - Self::publicshare() - } - /// Returns the user's runtime directory. /// /// Checks `XDG_RUNTIME_DIR` first, then falls back to platform defaults: @@ -364,44 +281,33 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let runtime = Dir::runtime()?; - /// # Ok(()) - /// # } + /// if let Some(runtime) = Dir::runtime() { + /// println!("Runtime directory: {}", runtime.display()); + /// } /// ``` - pub fn runtime() -> Result { + pub fn runtime() -> Option { if let Some(path) = Self::resolve_xdg_path("XDG_RUNTIME_DIR") { - return Ok(path); + return Some(path); } #[cfg(target_os = "macos")] { - if let Ok(tmpdir) = env::var("TMPDIR") { Ok(PathBuf::from(tmpdir)) } else { Ok(PathBuf::from("/tmp")) } + env::var("TMPDIR").ok().map(PathBuf::from).or_else(|| Some(PathBuf::from("/tmp"))) } #[cfg(target_os = "windows")] { - env::var("TEMP").map(PathBuf::from).map_err(|_| eyre::eyre!("Failed to resolve runtime directory")) + env::var("TEMP").ok().map(PathBuf::from) } #[cfg(target_os = "linux")] { #[allow(unsafe_code)] let uid = unsafe { libc::getuid() }; - Ok(PathBuf::from(format!("/run/user/{}", uid))) + Some(PathBuf::from(format!("/run/user/{}", uid))) } } - /// Returns the user's runtime directory. - /// - /// **Deprecated**: Use [`Dir::runtime()`] instead. - /// - /// [`Dir::runtime()`]: Self::runtime - #[deprecated(since = "0.1.0", note = "Use `Dir::runtime()` instead")] - pub fn runtime_dir() -> Result { - Self::runtime() - } - /// Returns the user's state directory. /// /// Checks `XDG_STATE_HOME` first, then falls back to platform defaults: @@ -413,14 +319,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let state_dir = Dir::state_home()?; - /// # Ok(()) - /// # } + /// if let Some(state_dir) = Dir::state_home() { + /// println!("State directory: {}", state_dir.display()); + /// } /// ``` - pub fn state_home() -> Result { + pub fn state_home() -> Option { Self::resolve_xdg_path("XDG_STATE_HOME") - .map_or_else(|| Self::get_platform_default("Library/Application Support", "LOCALAPPDATA", ".local/state"), Ok) + .or_else(|| Self::get_platform_default("Library/Application Support", "LOCALAPPDATA", ".local/state")) } /// Returns the user's templates directory. @@ -433,26 +338,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let templates = Dir::templates()?; - /// # Ok(()) - /// # } + /// if let Some(templates) = Dir::templates() { + /// println!("Templates directory: {}", templates.display()); + /// } /// ``` - pub fn templates() -> Result { - Self::resolve_xdg_path("XDG_TEMPLATES_DIR").map_or_else( - || Self::get_platform_default_with_windows_subdir("Templates", "USERPROFILE", "Templates", "Templates"), - Ok, - ) - } - - /// Returns the user's templates directory. - /// - /// **Deprecated**: Use [`Dir::templates()`] instead. - /// - /// [`Dir::templates()`]: Self::templates - #[deprecated(since = "0.1.0", note = "Use `Dir::templates()` instead")] - pub fn templates_dir() -> Result { - Self::templates() + pub fn templates() -> Option { + Self::resolve_xdg_path("XDG_TEMPLATES_DIR") + .or_else(|| Self::get_platform_default_with_windows_subdir("Templates", "USERPROFILE", "Templates", "Templates")) } /// Returns the user's videos directory. @@ -466,24 +358,13 @@ impl Dir { /// /// ```rust /// use dir_spec::Dir; - /// # fn main() -> Result<(), eyre::Error> { - /// let videos = Dir::videos()?; - /// # Ok(()) - /// # } + /// if let Some(videos) = Dir::videos() { + /// println!("Videos directory: {}", videos.display()); + /// } /// ``` - pub fn videos() -> Result { + pub fn videos() -> Option { Self::resolve_xdg_path("XDG_VIDEOS_DIR") - .map_or_else(|| Self::get_platform_default_with_windows_subdir("Movies", "USERPROFILE", "Videos", "Videos"), Ok) - } - - /// Returns the user's videos directory. - /// - /// **Deprecated**: Use [`Dir::videos()`] instead. - /// - /// [`Dir::videos()`]: Self::videos - #[deprecated(since = "0.1.0", note = "Use `Dir::videos()` instead")] - pub fn videos_dir() -> Result { - Self::videos() + .or_else(|| Self::get_platform_default_with_windows_subdir("Movies", "USERPROFILE", "Videos", "Videos")) } /// Resolves platform-specific directory paths using a simple pattern. @@ -495,20 +376,20 @@ impl Dir { macos_path: &str, #[allow(unused_variables)] windows_env: &str, #[allow(unused_variables)] linux_path: &str, - ) -> Result { + ) -> Option { #[cfg(target_os = "macos")] { - Ok(Self::home()?.join(macos_path)) + Self::home().map(|home| home.join(macos_path)) } #[cfg(target_os = "windows")] { - env::var(windows_env).map(PathBuf::from).map_err(|_| eyre::eyre!("Failed to resolve directory")) + env::var(windows_env).ok().map(PathBuf::from) } #[cfg(target_os = "linux")] { - Ok(Self::home()?.join(linux_path)) + Self::home().map(|home| home.join(linux_path)) } } @@ -522,22 +403,20 @@ impl Dir { #[allow(unused_variables)] windows_env: &str, #[allow(unused_variables)] windows_subdir: &str, #[allow(unused_variables)] linux_path: &str, - ) -> Result { + ) -> Option { #[cfg(target_os = "macos")] { - Ok(Self::home()?.join(macos_path)) + Self::home().map(|home| home.join(macos_path)) } #[cfg(target_os = "windows")] { - env::var(windows_env) - .map(|path| PathBuf::from(path).join(windows_subdir)) - .map_err(|_| eyre::eyre!("Failed to resolve directory")) + env::var(windows_env).ok().map(|path| PathBuf::from(path).join(windows_subdir)) } #[cfg(target_os = "linux")] { - Ok(Self::home()?.join(linux_path)) + Self::home().map(|home| home.join(linux_path)) } } @@ -596,9 +475,9 @@ mod tests { #[test] fn test_home_directory() { let home = Dir::home(); - assert!(home.is_ok()); - let home_path = home.unwrap(); - assert!(home_path.is_absolute()); + if let Some(home_path) = home { + assert!(home_path.is_absolute()); + } } #[test] @@ -608,15 +487,15 @@ mod tests { env::remove_var("XDG_CONFIG_HOME"); } let config = Dir::config_home(); - assert!(config.is_ok()); - let config_path = config.unwrap(); - assert!(config_path.is_absolute()); + if let Some(config_path) = config { + assert!(config_path.is_absolute()); - #[cfg(target_os = "linux")] - assert!(config_path.to_string_lossy().ends_with(".config")); + #[cfg(target_os = "linux")] + assert!(config_path.to_string_lossy().ends_with(".config")); - #[cfg(target_os = "macos")] - assert!(config_path.to_string_lossy().contains("Library/Application Support")); + #[cfg(target_os = "macos")] + assert!(config_path.to_string_lossy().contains("Library/Application Support")); + } } #[test] @@ -627,8 +506,7 @@ mod tests { env::set_var("XDG_CONFIG_HOME", test_path); } let config = Dir::config_home(); - assert!(config.is_ok()); - assert_eq!(config.unwrap(), PathBuf::from(test_path)); + assert_eq!(config, Some(PathBuf::from(test_path))); unsafe { env::remove_var("XDG_CONFIG_HOME"); } @@ -641,15 +519,15 @@ mod tests { env::remove_var("XDG_CACHE_HOME"); } let cache = Dir::cache_home(); - assert!(cache.is_ok()); - let cache_path = cache.unwrap(); - assert!(cache_path.is_absolute()); + if let Some(cache_path) = cache { + assert!(cache_path.is_absolute()); - #[cfg(target_os = "linux")] - assert!(cache_path.to_string_lossy().ends_with(".cache")); + #[cfg(target_os = "linux")] + assert!(cache_path.to_string_lossy().ends_with(".cache")); - #[cfg(target_os = "macos")] - assert!(cache_path.to_string_lossy().contains("Library/Caches")); + #[cfg(target_os = "macos")] + assert!(cache_path.to_string_lossy().contains("Library/Caches")); + } } #[test] @@ -659,15 +537,15 @@ mod tests { env::remove_var("XDG_DATA_HOME"); } let data = Dir::data_home(); - assert!(data.is_ok()); - let data_path = data.unwrap(); - assert!(data_path.is_absolute()); + if let Some(data_path) = data { + assert!(data_path.is_absolute()); - #[cfg(target_os = "linux")] - assert!(data_path.to_string_lossy().ends_with(".local/share")); + #[cfg(target_os = "linux")] + assert!(data_path.to_string_lossy().ends_with(".local/share")); - #[cfg(target_os = "macos")] - assert!(data_path.to_string_lossy().contains("Library/Application Support")); + #[cfg(target_os = "macos")] + assert!(data_path.to_string_lossy().contains("Library/Application Support")); + } } #[test] @@ -677,15 +555,15 @@ mod tests { env::remove_var("XDG_BIN_HOME"); } let bin = Dir::bin_home(); - assert!(bin.is_ok()); - let bin_path = bin.unwrap(); - assert!(bin_path.is_absolute()); + if let Some(bin_path) = bin { + assert!(bin_path.is_absolute()); - #[cfg(any(target_os = "linux", target_os = "macos"))] - assert!(bin_path.to_string_lossy().ends_with(".local/bin")); + #[cfg(any(target_os = "linux", target_os = "macos"))] + assert!(bin_path.to_string_lossy().ends_with(".local/bin")); - #[cfg(target_os = "windows")] - assert!(bin_path.to_string_lossy().contains("Programs")); + #[cfg(target_os = "windows")] + assert!(bin_path.to_string_lossy().contains("Programs")); + } } #[test] @@ -695,17 +573,17 @@ mod tests { env::remove_var("XDG_RUNTIME_DIR"); } let runtime = Dir::runtime(); - assert!(runtime.is_ok()); - let runtime_path = runtime.unwrap(); - assert!(runtime_path.is_absolute()); + if let Some(runtime_path) = runtime { + assert!(runtime_path.is_absolute()); - #[cfg(target_os = "linux")] - assert!(runtime_path.to_string_lossy().starts_with("/run/user/")); + #[cfg(target_os = "linux")] + assert!(runtime_path.to_string_lossy().starts_with("/run/user/")); - #[cfg(target_os = "macos")] - { - let path_str = runtime_path.to_string_lossy(); - assert!(path_str.contains("tmp") || path_str.starts_with("/var/folders")); + #[cfg(target_os = "macos")] + { + let path_str = runtime_path.to_string_lossy(); + assert!(path_str.contains("tmp") || path_str.starts_with("/var/folders")); + } } } @@ -716,10 +594,10 @@ mod tests { env::remove_var("XDG_DESKTOP_DIR"); } let desktop = Dir::desktop(); - assert!(desktop.is_ok()); - let desktop_path = desktop.unwrap(); - assert!(desktop_path.is_absolute()); - assert!(desktop_path.to_string_lossy().ends_with("Desktop")); + if let Some(desktop_path) = desktop { + assert!(desktop_path.is_absolute()); + assert!(desktop_path.to_string_lossy().ends_with("Desktop")); + } } #[test] @@ -729,18 +607,18 @@ mod tests { env::remove_var("XDG_VIDEOS_DIR"); } let videos = Dir::videos(); - assert!(videos.is_ok()); - let videos_path = videos.unwrap(); - assert!(videos_path.is_absolute()); + if let Some(videos_path) = videos { + assert!(videos_path.is_absolute()); - #[cfg(target_os = "linux")] - assert!(videos_path.to_string_lossy().ends_with("Videos")); + #[cfg(target_os = "linux")] + assert!(videos_path.to_string_lossy().ends_with("Videos")); - #[cfg(target_os = "macos")] - assert!(videos_path.to_string_lossy().ends_with("Movies")); + #[cfg(target_os = "macos")] + assert!(videos_path.to_string_lossy().ends_with("Movies")); - #[cfg(target_os = "windows")] - assert!(videos_path.to_string_lossy().ends_with("Videos")); + #[cfg(target_os = "windows")] + assert!(videos_path.to_string_lossy().ends_with("Videos")); + } } #[test] @@ -750,60 +628,14 @@ mod tests { env::remove_var("XDG_PUBLICSHARE_DIR"); } let public = Dir::publicshare(); - assert!(public.is_ok()); - let public_path = public.unwrap(); - assert!(public_path.is_absolute()); - - #[cfg(target_os = "windows")] - assert_eq!(public_path, PathBuf::from("C:\\Users\\Public")); - - #[cfg(any(target_os = "linux", target_os = "macos"))] - assert!(public_path.to_string_lossy().ends_with("Public")); - } + if let Some(public_path) = public { + assert!(public_path.is_absolute()); - #[test] - #[allow(unsafe_code)] - fn test_deprecated_methods_work() { - #[allow(deprecated)] - { - assert!(Dir::desktop_dir().is_ok()); - assert!(Dir::documents_dir().is_ok()); - assert!(Dir::download_dir().is_ok()); - assert!(Dir::music_dir().is_ok()); - assert!(Dir::pictures_dir().is_ok()); - assert!(Dir::publicshare_dir().is_ok()); - assert!(Dir::runtime_dir().is_ok()); - assert!(Dir::templates_dir().is_ok()); - assert!(Dir::videos_dir().is_ok()); - } - } + #[cfg(target_os = "windows")] + assert_eq!(public_path, PathBuf::from("C:\\Users\\Public")); - #[test] - #[allow(unsafe_code)] - fn test_new_methods_equivalent_to_deprecated() { - unsafe { - env::remove_var("XDG_DESKTOP_DIR"); - env::remove_var("XDG_DOCUMENTS_DIR"); - env::remove_var("XDG_DOWNLOAD_DIR"); - env::remove_var("XDG_MUSIC_DIR"); - env::remove_var("XDG_PICTURES_DIR"); - env::remove_var("XDG_PUBLICSHARE_DIR"); - env::remove_var("XDG_RUNTIME_DIR"); - env::remove_var("XDG_TEMPLATES_DIR"); - env::remove_var("XDG_VIDEOS_DIR"); - } - - #[allow(deprecated)] - { - assert_eq!(Dir::desktop().unwrap(), Dir::desktop_dir().unwrap()); - assert_eq!(Dir::documents().unwrap(), Dir::documents_dir().unwrap()); - assert_eq!(Dir::downloads().unwrap(), Dir::download_dir().unwrap()); - assert_eq!(Dir::music().unwrap(), Dir::music_dir().unwrap()); - assert_eq!(Dir::pictures().unwrap(), Dir::pictures_dir().unwrap()); - assert_eq!(Dir::publicshare().unwrap(), Dir::publicshare_dir().unwrap()); - assert_eq!(Dir::runtime().unwrap(), Dir::runtime_dir().unwrap()); - assert_eq!(Dir::templates().unwrap(), Dir::templates_dir().unwrap()); - assert_eq!(Dir::videos().unwrap(), Dir::videos_dir().unwrap()); + #[cfg(any(target_os = "linux", target_os = "macos"))] + assert!(public_path.to_string_lossy().ends_with("Public")); } } }