diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 7884d7fab9..74f2391eaa 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -858,7 +858,7 @@ fn do_auto_cd( report_shell_error(engine_state, &err); return; }; - use nu_path::pwd_per_drive::pwd_per_drive::set_pwd_per_drive; + use nu_path::pwd_per_drive::pwd_per_drive_singleton::set_pwd_per_drive; let _as_is = set_pwd_per_drive(PathBuf::from(path.clone()).as_path()); let cwd = Value::string(cwd, span); diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs index 81e93e6ec0..913353b7ab 100644 --- a/crates/nu-command/src/filesystem/cd.rs +++ b/crates/nu-command/src/filesystem/cd.rs @@ -114,7 +114,7 @@ impl Command for Cd { //FIXME: this only changes the current scope, but instead this environment variable //should probably be a block that loads the information from the state in the overlay PermissionResult::PermissionOk => { - use nu_path::pwd_per_drive::pwd_per_drive::set_pwd_per_drive; + use nu_path::pwd_per_drive::pwd_per_drive_singleton::set_pwd_per_drive; let _as_is = set_pwd_per_drive(path.as_path()); stack.set_cwd(path)?; Ok(PipelineData::empty()) diff --git a/crates/nu-path/src/lib.rs b/crates/nu-path/src/lib.rs index 145013e7fe..637e5e67cf 100644 --- a/crates/nu-path/src/lib.rs +++ b/crates/nu-path/src/lib.rs @@ -4,10 +4,10 @@ mod components; pub mod dots; pub mod expansions; pub mod form; -#[cfg(target_os="windows")] -pub mod pwd_per_drive; mod helpers; mod path; +#[cfg(target_os = "windows")] +pub mod pwd_per_drive; mod tilde; mod trailing_slash; diff --git a/crates/nu-path/src/pwd_per_drive.rs b/crates/nu-path/src/pwd_per_drive.rs index a9f8238319..70e91fd401 100644 --- a/crates/nu-path/src/pwd_per_drive.rs +++ b/crates/nu-path/src/pwd_per_drive.rs @@ -71,7 +71,7 @@ impl DrivePWDmap { /// Helper to convert a drive letter to an array index fn drive_to_index(drive: char) -> Option { let drive = drive.to_ascii_uppercase(); - if ('A'..='Z').contains(&drive) { + if drive.is_ascii_uppercase() { Some((drive as usize) - ('A' as usize)) } else { None @@ -270,16 +270,20 @@ mod tests { /// let expanded = expand_pwd_per_drive(Path::new("D:test")); /// assert_eq!(expanded, Some(PathBuf::from("D:\\test"))); /// ``` -pub mod pwd_per_drive { - use std::path::{ Path, PathBuf }; - use super::{get_drive_pwd_map}; +pub mod pwd_per_drive_singleton { + use super::get_drive_pwd_map; + use std::path::{Path, PathBuf}; /// set_pwd_per_drive /// record PWD for drive, path must be absolute path /// return Ok(()) if succeeded, otherwise error message #[cfg(target_os = "windows")] pub fn set_pwd_per_drive(path: &Path) -> Result<(), String> { - get_drive_pwd_map().lock().unwrap().set_pwd(path) + if let Ok(mut pwd_per_drive) = get_drive_pwd_map().lock() { + pwd_per_drive.set_pwd(path) + } else { + Err("Failed to lock DrivePWDmap".to_string()) + } } #[cfg(not(target_os = "windows"))] @@ -293,10 +297,11 @@ pub mod pwd_per_drive { #[cfg(target_os = "windows")] pub fn expand_pwd_per_drive(path: &Path) -> Option { if need_expand_pwd_per_drive(path) { - get_drive_pwd_map().lock().unwrap().expand_path(path) - } else { - None + if let Ok(mut pwd_per_drive) = get_drive_pwd_map().lock() { + return pwd_per_drive.expand_path(path); + } } + None } /// expand_pwd_per_drive will return None on non-windows platform @@ -312,7 +317,8 @@ pub mod pwd_per_drive { if let Some(path_str) = path.to_str() { let chars: Vec = path_str.chars().collect(); if chars.len() >= 2 { - return chars[1] == ':' && (chars.len() == 2 || (chars[2] != '/' && chars[2] != '\\')); + return chars[1] == ':' + && (chars.len() == 2 || (chars[2] != '/' && chars[2] != '\\')); } } false @@ -345,4 +351,4 @@ pub mod pwd_per_drive { let expanded = expand_pwd_per_drive(Path::new("D:test")); assert_eq!(expanded, Some(PathBuf::from("D:\\test"))); } -} \ No newline at end of file +} diff --git a/crates/nu-path/src/tilde.rs b/crates/nu-path/src/tilde.rs index fe44fa07aa..ed44759293 100644 --- a/crates/nu-path/src/tilde.rs +++ b/crates/nu-path/src/tilde.rs @@ -21,9 +21,7 @@ fn expand_tilde_with_home(path: impl AsRef, home: Option) -> Path let path = path.as_ref(); if !path.starts_with("~") { - use crate::pwd_per_drive:: { - pwd_per_drive::expand_pwd_per_drive, - }; + use crate::pwd_per_drive::pwd_per_drive_singleton::expand_pwd_per_drive; if let Some(expanded_dir) = expand_pwd_per_drive(path) { return expanded_dir; }