mirror of
https://github.com/nushell/nushell.git
synced 2025-04-10 14:08:40 +02:00
# Description Part 3 of replacing `std::path` types with `nu_path` types added in #13115. This PR targets the paths listed in `$nu`. That is, the home, config, data, and cache directories.
33 lines
1.4 KiB
Rust
33 lines
1.4 KiB
Rust
use crate::AbsolutePathBuf;
|
|
|
|
pub fn home_dir() -> Option<AbsolutePathBuf> {
|
|
dirs::home_dir().and_then(|home| AbsolutePathBuf::try_from(home).ok())
|
|
}
|
|
|
|
/// Return the data directory for the current platform or XDG_DATA_HOME if specified.
|
|
pub fn data_dir() -> Option<AbsolutePathBuf> {
|
|
std::env::var("XDG_DATA_HOME")
|
|
.ok()
|
|
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
|
|
.or_else(|| dirs::data_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
|
|
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
|
|
}
|
|
|
|
/// Return the cache directory for the current platform or XDG_CACHE_HOME if specified.
|
|
pub fn cache_dir() -> Option<AbsolutePathBuf> {
|
|
std::env::var("XDG_CACHE_HOME")
|
|
.ok()
|
|
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
|
|
.or_else(|| dirs::cache_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
|
|
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
|
|
}
|
|
|
|
/// Return the config directory for the current platform or XDG_CONFIG_HOME if specified.
|
|
pub fn config_dir() -> Option<AbsolutePathBuf> {
|
|
std::env::var("XDG_CONFIG_HOME")
|
|
.ok()
|
|
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
|
|
.or_else(|| dirs::config_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
|
|
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
|
|
}
|