mirror of
https://github.com/nushell/nushell.git
synced 2025-08-13 13:47:42 +02:00
Rename Drive2PWDmap as DriveToPwdMap, eliminate extra #[cfg(windows)] and mod _impl
This commit is contained in:
@ -16,6 +16,6 @@ pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, l
|
||||
pub use helpers::{cache_dir, data_dir, home_dir, nu_config_dir};
|
||||
pub use path::*;
|
||||
#[cfg(windows)]
|
||||
pub use pwd_per_drive::_impl::singleton::{expand_pwd, set_pwd};
|
||||
pub use pwd_per_drive::singleton::{expand_pwd, set_pwd};
|
||||
pub use tilde::expand_tilde;
|
||||
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};
|
||||
|
@ -1,36 +1,34 @@
|
||||
#[cfg(windows)]
|
||||
pub mod _impl {
|
||||
use std::path::{Path, PathBuf};
|
||||
/// Usage for pwd_per_drive on windows
|
||||
///
|
||||
/// Upon change PWD, call set_pwd_per_drive() with absolute path
|
||||
///
|
||||
/// Call expand_pwd_per_drive() with relative path to get absolution path
|
||||
///
|
||||
/// ```
|
||||
/// use std::path::{Path, PathBuf};
|
||||
/// use nu_path::{expand_pwd, set_pwd};
|
||||
///
|
||||
/// // Set PWD for drive C
|
||||
/// set_pwd(Path::new(r"C:\Users\Home")).unwrap();
|
||||
///
|
||||
/// // Expand a relative path
|
||||
/// let expanded = expand_pwd(Path::new("C:test"));
|
||||
/// assert_eq!(expanded, Some(PathBuf::from(r"C:\Users\Home\test")));
|
||||
///
|
||||
/// // Will NOT expand an absolute path
|
||||
/// let expanded = expand_pwd(Path::new(r"C:\absolute\path"));
|
||||
/// assert_eq!(expanded, None);
|
||||
///
|
||||
/// // Expand with no drive letter
|
||||
/// let expanded = expand_pwd(Path::new(r"\no_drive"));
|
||||
/// assert_eq!(expanded, None);
|
||||
///
|
||||
/// // Expand with no PWD set for the drive
|
||||
/// let expanded = expand_pwd(Path::new("D:test"));
|
||||
/// assert_eq!(expanded, Some(PathBuf::from(r"D:\test")));
|
||||
/// ```
|
||||
pub mod singleton {
|
||||
use std::path::{Path, PathBuf};
|
||||
/// Usage for pwd_per_drive on windows
|
||||
///
|
||||
/// Upon change PWD, call set_pwd_per_drive() with absolute path
|
||||
///
|
||||
/// Call expand_pwd_per_drive() with relative path to get absolution path
|
||||
///
|
||||
/// ```
|
||||
/// use std::path::{Path, PathBuf};
|
||||
/// use nu_path::{expand_pwd, set_pwd};
|
||||
///
|
||||
/// // Set PWD for drive C
|
||||
/// set_pwd(Path::new(r"C:\Users\Home")).unwrap();
|
||||
///
|
||||
/// // Expand a relative path
|
||||
/// let expanded = expand_pwd(Path::new("C:test"));
|
||||
/// assert_eq!(expanded, Some(PathBuf::from(r"C:\Users\Home\test")));
|
||||
///
|
||||
/// // Will NOT expand an absolute path
|
||||
/// let expanded = expand_pwd(Path::new(r"C:\absolute\path"));
|
||||
/// assert_eq!(expanded, None);
|
||||
///
|
||||
/// // Expand with no drive letter
|
||||
/// let expanded = expand_pwd(Path::new(r"\no_drive"));
|
||||
/// assert_eq!(expanded, None);
|
||||
///
|
||||
/// // Expand with no PWD set for the drive
|
||||
/// let expanded = expand_pwd(Path::new("D:test"));
|
||||
/// assert_eq!(expanded, Some(PathBuf::from(r"D:\test")));
|
||||
/// ```
|
||||
pub mod singleton {
|
||||
use super::*;
|
||||
|
||||
/// set_pwd_per_drive
|
||||
@ -68,11 +66,11 @@ pub mod _impl {
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_usage_for_pwd_per_drive() {
|
||||
use super::_impl::singleton::{expand_pwd, set_pwd};
|
||||
#[test]
|
||||
fn test_usage_for_pwd_per_drive() {
|
||||
use singleton::{expand_pwd, set_pwd};
|
||||
// Set PWD for drive F
|
||||
assert!(set_pwd(Path::new(r"F:\Users\Home")).is_ok());
|
||||
|
||||
@ -95,20 +93,20 @@ pub mod _impl {
|
||||
expanded,
|
||||
Some(PathBuf::from(format!(
|
||||
"{}test",
|
||||
Drive2PWD::ensure_trailing_separator(&sys_abs)
|
||||
DriveToPwdMap::ensure_trailing_separator(&sys_abs)
|
||||
)))
|
||||
);
|
||||
}
|
||||
assert_eq!(expanded, Some(PathBuf::from(r"D:\test")));
|
||||
}
|
||||
}
|
||||
|
||||
struct Drive2PWD {
|
||||
struct DriveToPwdMap {
|
||||
map: [Option<String>; 26], // Fixed-size array for A-Z
|
||||
}
|
||||
}
|
||||
|
||||
impl Drive2PWD {
|
||||
impl DriveToPwdMap {
|
||||
pub fn new() -> Self {
|
||||
Drive2PWD {
|
||||
DriveToPwdMap {
|
||||
map: Default::default(), // Initialize all to `None`
|
||||
}
|
||||
}
|
||||
@ -175,30 +173,30 @@ pub mod _impl {
|
||||
path.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_full_path_name_w(path_str: &str) -> Option<String> {
|
||||
fn get_full_path_name_w(path_str: &str) -> Option<String> {
|
||||
use omnipath::sys_absolute;
|
||||
if let Ok(path_sys_abs) = sys_absolute(PathBuf::from(path_str).as_path()) {
|
||||
Some(path_sys_abs.to_str()?.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
/// Global singleton instance of DrivePwdMap
|
||||
static DRIVE_PWD_MAP: OnceLock<Mutex<Drive2PWD>> = OnceLock::new();
|
||||
/// Global singleton instance of DrivePwdMap
|
||||
static DRIVE_PWD_MAP: OnceLock<Mutex<DriveToPwdMap>> = OnceLock::new();
|
||||
|
||||
/// Access the singleton instance
|
||||
fn get_drive_pwd_map() -> &'static Mutex<Drive2PWD> {
|
||||
DRIVE_PWD_MAP.get_or_init(|| Mutex::new(Drive2PWD::new()))
|
||||
}
|
||||
/// Access the singleton instance
|
||||
fn get_drive_pwd_map() -> &'static Mutex<DriveToPwdMap> {
|
||||
DRIVE_PWD_MAP.get_or_init(|| Mutex::new(DriveToPwdMap::new()))
|
||||
}
|
||||
|
||||
/// Test for Drive2PWD map
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// Test for Drive2PWD map
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@ -234,7 +232,7 @@ pub mod _impl {
|
||||
|
||||
#[test]
|
||||
fn test_expand_path() {
|
||||
let mut drive_map = Drive2PWD::new();
|
||||
let mut drive_map = DriveToPwdMap::new();
|
||||
|
||||
// Set PWD for drive C
|
||||
assert_eq!(drive_map.set_pwd(Path::new(r"C:\Users\Home")), Ok(()));
|
||||
@ -258,7 +256,7 @@ pub mod _impl {
|
||||
expanded,
|
||||
Some(PathBuf::from(format!(
|
||||
r"{}test",
|
||||
Drive2PWD::ensure_trailing_separator(&pwd_on_d)
|
||||
DriveToPwdMap::ensure_trailing_separator(&pwd_on_d)
|
||||
)))
|
||||
);
|
||||
} else {
|
||||
@ -268,7 +266,7 @@ pub mod _impl {
|
||||
|
||||
#[test]
|
||||
fn test_set_and_get_pwd() {
|
||||
let mut drive_map = Drive2PWD::new();
|
||||
let mut drive_map = DriveToPwdMap::new();
|
||||
|
||||
// Set PWD for drive C
|
||||
assert!(drive_map.set_pwd(Path::new(r"C:\Users\Example")).is_ok());
|
||||
@ -296,7 +294,7 @@ pub mod _impl {
|
||||
|
||||
#[test]
|
||||
fn test_set_pwd_invalid_path() {
|
||||
let mut drive_map = Drive2PWD::new();
|
||||
let mut drive_map = DriveToPwdMap::new();
|
||||
|
||||
// Invalid path (no drive letter)
|
||||
let result = drive_map.set_pwd(Path::new(r"\InvalidPath"));
|
||||
@ -306,7 +304,7 @@ pub mod _impl {
|
||||
|
||||
#[test]
|
||||
fn test_get_pwd_invalid_drive() {
|
||||
let mut drive_map = Drive2PWD::new();
|
||||
let mut drive_map = DriveToPwdMap::new();
|
||||
|
||||
// Get PWD for a drive not set (e.g., Z)
|
||||
assert_eq!(drive_map.get_pwd('Z'), Some(r"Z:\".to_string()));
|
||||
@ -314,5 +312,4 @@ pub mod _impl {
|
||||
// Invalid drive letter (non-alphabetic)
|
||||
assert_eq!(drive_map.get_pwd('1'), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user