User directories
Recipe | Crates | Categories |
---|---|---|
dirs | ||
directories |
Get platform-specific locations for configuration, cache, and other data
dirs
dirs
is a low-level library that provides platform-specific standard locations of directories for config, cache and other data on Linux, Windows, macOS and Redox 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.
directories
directories
is a 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.
directories
is a higher-level library than dirs
and can also compute paths for applications.
use std::path::PathBuf; use anyhow::Result; use anyhow::anyhow; use directories::ProjectDirs; pub fn get_data_dir() -> Result<PathBuf> { let directory = if let Ok(s) = std::env::var("APP_DATA_DIR") { PathBuf::from(s) } else if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") { proj_dirs.data_local_dir().to_path_buf() } else { return Err(anyhow!("Unable to find the data directory")); }; Ok(directory) } pub fn get_config_dir() -> Result<PathBuf> { let directory = if let Ok(s) = std::env::var("APP_CONFIG_DIR") { PathBuf::from(s) } else if let Some(proj_dirs) = ProjectDirs::from("com", "Foo Corp", "Bar App") { proj_dirs.config_local_dir().to_path_buf() } else { return Err(anyhow!("Unable to find the config directory")); }; Ok(directory) } fn main() { println!("{:?}", get_data_dir()); println!("{:?}", get_config_dir()); }
- find a spot for the following:
open
Open a path or URL using the program configured on the system.
use open::that; fn main() { // Open a file using the default program if let Err(e) = that("example.txt") { eprintln!("Failed to open file: {}", e); } // Open a URL using the default web browser if let Err(e) = that("https://www.rust-lang.org") { eprintln!("Failed to open URL: {}", e); } // OR: open::with("http://rust-lang.org", "firefox")?; // Depending on the platform and system configuration, launchers can block. // If you want to be sure they don’t, use that_in_background() or // that_detached instead. // Get the commands to try open the path with. let path = "http://rust-lang.org"; open::commands(path) .iter() .for_each(|x| println!("{:?}", x)); }