Rename Drive2PWDmap as DriveToPwdMap, eliminate extra #[cfg(windows)] and mod _impl

This commit is contained in:
Zhenping Zhao
2024-11-23 16:45:59 -08:00
parent 63c9947247
commit 6ef5b04b25
2 changed files with 299 additions and 302 deletions

View File

@ -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 helpers::{cache_dir, data_dir, home_dir, nu_config_dir};
pub use path::*; pub use path::*;
#[cfg(windows)] #[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 tilde::expand_tilde;
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash}; pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};

View File

@ -1,5 +1,3 @@
#[cfg(windows)]
pub mod _impl {
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
/// Usage for pwd_per_drive on windows /// Usage for pwd_per_drive on windows
/// ///
@ -72,7 +70,7 @@ pub mod _impl {
#[test] #[test]
fn test_usage_for_pwd_per_drive() { fn test_usage_for_pwd_per_drive() {
use super::_impl::singleton::{expand_pwd, set_pwd}; use singleton::{expand_pwd, set_pwd};
// Set PWD for drive F // Set PWD for drive F
assert!(set_pwd(Path::new(r"F:\Users\Home")).is_ok()); assert!(set_pwd(Path::new(r"F:\Users\Home")).is_ok());
@ -95,20 +93,20 @@ pub mod _impl {
expanded, expanded,
Some(PathBuf::from(format!( Some(PathBuf::from(format!(
"{}test", "{}test",
Drive2PWD::ensure_trailing_separator(&sys_abs) DriveToPwdMap::ensure_trailing_separator(&sys_abs)
))) )))
); );
} }
assert_eq!(expanded, Some(PathBuf::from(r"D:\test"))); assert_eq!(expanded, Some(PathBuf::from(r"D:\test")));
} }
struct Drive2PWD { struct DriveToPwdMap {
map: [Option<String>; 26], // Fixed-size array for A-Z map: [Option<String>; 26], // Fixed-size array for A-Z
} }
impl Drive2PWD { impl DriveToPwdMap {
pub fn new() -> Self { pub fn new() -> Self {
Drive2PWD { DriveToPwdMap {
map: Default::default(), // Initialize all to `None` map: Default::default(), // Initialize all to `None`
} }
} }
@ -189,11 +187,11 @@ pub mod _impl {
use std::sync::{Mutex, OnceLock}; use std::sync::{Mutex, OnceLock};
/// Global singleton instance of DrivePwdMap /// Global singleton instance of DrivePwdMap
static DRIVE_PWD_MAP: OnceLock<Mutex<Drive2PWD>> = OnceLock::new(); static DRIVE_PWD_MAP: OnceLock<Mutex<DriveToPwdMap>> = OnceLock::new();
/// Access the singleton instance /// Access the singleton instance
fn get_drive_pwd_map() -> &'static Mutex<Drive2PWD> { fn get_drive_pwd_map() -> &'static Mutex<DriveToPwdMap> {
DRIVE_PWD_MAP.get_or_init(|| Mutex::new(Drive2PWD::new())) DRIVE_PWD_MAP.get_or_init(|| Mutex::new(DriveToPwdMap::new()))
} }
/// Test for Drive2PWD map /// Test for Drive2PWD map
@ -234,7 +232,7 @@ pub mod _impl {
#[test] #[test]
fn test_expand_path() { fn test_expand_path() {
let mut drive_map = Drive2PWD::new(); let mut drive_map = DriveToPwdMap::new();
// Set PWD for drive C // Set PWD for drive C
assert_eq!(drive_map.set_pwd(Path::new(r"C:\Users\Home")), Ok(())); assert_eq!(drive_map.set_pwd(Path::new(r"C:\Users\Home")), Ok(()));
@ -258,7 +256,7 @@ pub mod _impl {
expanded, expanded,
Some(PathBuf::from(format!( Some(PathBuf::from(format!(
r"{}test", r"{}test",
Drive2PWD::ensure_trailing_separator(&pwd_on_d) DriveToPwdMap::ensure_trailing_separator(&pwd_on_d)
))) )))
); );
} else { } else {
@ -268,7 +266,7 @@ pub mod _impl {
#[test] #[test]
fn test_set_and_get_pwd() { fn test_set_and_get_pwd() {
let mut drive_map = Drive2PWD::new(); let mut drive_map = DriveToPwdMap::new();
// Set PWD for drive C // Set PWD for drive C
assert!(drive_map.set_pwd(Path::new(r"C:\Users\Example")).is_ok()); assert!(drive_map.set_pwd(Path::new(r"C:\Users\Example")).is_ok());
@ -296,7 +294,7 @@ pub mod _impl {
#[test] #[test]
fn test_set_pwd_invalid_path() { fn test_set_pwd_invalid_path() {
let mut drive_map = Drive2PWD::new(); let mut drive_map = DriveToPwdMap::new();
// Invalid path (no drive letter) // Invalid path (no drive letter)
let result = drive_map.set_pwd(Path::new(r"\InvalidPath")); let result = drive_map.set_pwd(Path::new(r"\InvalidPath"));
@ -306,7 +304,7 @@ pub mod _impl {
#[test] #[test]
fn test_get_pwd_invalid_drive() { 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) // Get PWD for a drive not set (e.g., Z)
assert_eq!(drive_map.get_pwd('Z'), Some(r"Z:\".to_string())); assert_eq!(drive_map.get_pwd('Z'), Some(r"Z:\".to_string()));
@ -315,4 +313,3 @@ pub mod _impl {
assert_eq!(drive_map.get_pwd('1'), None); assert_eq!(drive_map.get_pwd('1'), None);
} }
} }
}