mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 11:45:50 +02:00
Path migration part 3: $nu
paths (#13368)
# 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.
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use super::helpers;
|
||||
#[cfg(windows)]
|
||||
use omnipath::WinPathExt;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
/// Normalize the path, expanding occurrences of n-dots.
|
||||
@ -63,7 +64,18 @@ pub fn expand_dots(path: impl AsRef<Path>) -> PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
helpers::simiplified(&result)
|
||||
simiplified(&result)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn simiplified(path: &std::path::Path) -> PathBuf {
|
||||
path.to_winuser_path()
|
||||
.unwrap_or_else(|_| path.to_path_buf())
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn simiplified(path: &std::path::Path) -> PathBuf {
|
||||
path.to_path_buf()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -1,8 +1,9 @@
|
||||
#[cfg(windows)]
|
||||
use omnipath::WinPathExt;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use super::dots::{expand_dots, expand_ndots};
|
||||
use super::helpers;
|
||||
use super::tilde::expand_tilde;
|
||||
|
||||
// Join a path relative to another path. Paths starting with tilde are considered as absolute.
|
||||
@ -30,8 +31,17 @@ where
|
||||
fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> {
|
||||
let path = expand_tilde(path);
|
||||
let path = expand_ndots(path);
|
||||
canonicalize_path(&path)
|
||||
}
|
||||
|
||||
helpers::canonicalize(&path)
|
||||
#[cfg(windows)]
|
||||
fn canonicalize_path(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
|
||||
path.canonicalize()?.to_winuser_path()
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn canonicalize_path(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
|
||||
path.canonicalize()
|
||||
}
|
||||
|
||||
/// Resolve all symbolic links and all components (tilde, ., .., ...+) and return the path in its
|
||||
|
@ -1,59 +1,32 @@
|
||||
#[cfg(windows)]
|
||||
use omnipath::WinPathExt;
|
||||
use std::path::PathBuf;
|
||||
use crate::AbsolutePathBuf;
|
||||
|
||||
pub fn home_dir() -> Option<PathBuf> {
|
||||
dirs::home_dir()
|
||||
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<PathBuf> {
|
||||
match std::env::var("XDG_DATA_HOME").map(PathBuf::from) {
|
||||
Ok(xdg_data) if xdg_data.is_absolute() => Some(canonicalize(&xdg_data).unwrap_or(xdg_data)),
|
||||
_ => get_canonicalized_path(dirs::data_dir()),
|
||||
}
|
||||
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<PathBuf> {
|
||||
match std::env::var("XDG_CACHE_HOME").map(PathBuf::from) {
|
||||
Ok(xdg_cache) if xdg_cache.is_absolute() => {
|
||||
Some(canonicalize(&xdg_cache).unwrap_or(xdg_cache))
|
||||
}
|
||||
_ => get_canonicalized_path(dirs::cache_dir()),
|
||||
}
|
||||
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<PathBuf> {
|
||||
match std::env::var("XDG_CONFIG_HOME").map(PathBuf::from) {
|
||||
Ok(xdg_config) if xdg_config.is_absolute() => {
|
||||
Some(canonicalize(&xdg_config).unwrap_or(xdg_config))
|
||||
}
|
||||
_ => get_canonicalized_path(dirs::config_dir()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_canonicalized_path(path: Option<PathBuf>) -> Option<PathBuf> {
|
||||
let path = path?;
|
||||
Some(canonicalize(&path).unwrap_or(path))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn canonicalize(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
|
||||
path.canonicalize()?.to_winuser_path()
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
pub fn canonicalize(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
|
||||
path.canonicalize()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn simiplified(path: &std::path::Path) -> PathBuf {
|
||||
path.to_winuser_path()
|
||||
.unwrap_or_else(|_| path.to_path_buf())
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
pub fn simiplified(path: &std::path::Path) -> PathBuf {
|
||||
path.to_path_buf()
|
||||
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))
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ mod trailing_slash;
|
||||
|
||||
pub use components::components;
|
||||
pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, locate_in_dirs};
|
||||
pub use helpers::{cache_dir, config_dir, data_dir, get_canonicalized_path, home_dir};
|
||||
pub use helpers::{cache_dir, config_dir, data_dir, home_dir};
|
||||
pub use path::*;
|
||||
pub use tilde::expand_tilde;
|
||||
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};
|
||||
|
Reference in New Issue
Block a user