mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-20 23:51:40 +01:00
refactor(windows): converge core/pathext.rs to the shared version
Take the KDE copy (module docs, ~ handled on unix as well as windows, the platform split in the test) as the common base. The only Windows-visible difference would be the JSON schema description of ResolvedPathBuf, so it is selected by cfg(target_os = "windows") and keeps the Windows text byte for byte (schema.json is the step 3 oracle). Windows behaviour is unchanged: the cfg(unix) branches do not compile in on this target. Verified: Windows clippy --workspace --locked --all-targets -D warnings clean. Unit tests cannot run on Linux (not verified).
This commit is contained in:
1 parent
8179aef3e2
commit
f7028485d1
1 file changed
+38
-7
@@ -1,3 +1,5 @@
|
||||
//! Resolving environment variables in the paths of the configuration.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsStr;
|
||||
use std::path::Component;
|
||||
@@ -25,12 +27,20 @@ impl<P: AsRef<Path>> PathExt for P {
|
||||
|
||||
for c in self.as_ref().components() {
|
||||
match c {
|
||||
#[allow(unused_mut)]
|
||||
Component::Normal(mut c) => {
|
||||
#[cfg(target_os = "windows")]
|
||||
// Special case for ~ and $HOME, replace with $Env:USERPROFILE
|
||||
if c == OsStr::new("~") || c.eq_ignore_ascii_case("$HOME") {
|
||||
c = OsStr::new("$Env:USERPROFILE");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
// Special case for ~, replace with $HOME (macOS and Linux)
|
||||
if c == OsStr::new("~") {
|
||||
c = OsStr::new("$HOME");
|
||||
}
|
||||
|
||||
let bytes = c.as_encoded_bytes();
|
||||
|
||||
// %LOCALAPPDATA%
|
||||
@@ -127,9 +137,15 @@ impl serde_with::schemars_1::JsonSchemaAs<PathBuf> for ResolvedPathBuf {
|
||||
}
|
||||
|
||||
fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
|
||||
// the description is part of the checked-in schemas, which differ per platform
|
||||
#[cfg(target_os = "windows")]
|
||||
let description = "A file system path. Environment variables like %VAR%, $Env:VAR, or $VAR are automatically resolved.";
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let description = "A file system path. Environment variables like $VAR are automatically resolved.";
|
||||
|
||||
schemars::json_schema!({
|
||||
"type": "string",
|
||||
"description": "A file system path. Environment variables like %VAR%, $Env:VAR, or $VAR are automatically resolved."
|
||||
"description": description
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -186,13 +202,28 @@ mod tests {
|
||||
);
|
||||
assert_eq!(resolve("/path/$ASD/to/d"), expected("/path/$ASD/to/d"));
|
||||
|
||||
// Set a $env:USERPROFILE variable for testing
|
||||
unsafe {
|
||||
std::env::set_var("USERPROFILE", "C:\\Users\\user");
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
// Set a $env:USERPROFILE variable for testing
|
||||
unsafe {
|
||||
std::env::set_var("USERPROFILE", "C:\\Users\\user");
|
||||
}
|
||||
|
||||
// ~ and $HOME should be replaced with $Env:USERPROFILE
|
||||
assert_eq!(resolve("~"), expected("C:\\Users\\user"));
|
||||
assert_eq!(resolve("$HOME"), expected("C:\\Users\\user"));
|
||||
}
|
||||
|
||||
// ~ and $HOME should be replaced with $Env:USERPROFILE
|
||||
assert_eq!(resolve("~"), expected("C:\\Users\\user"));
|
||||
assert_eq!(resolve("$HOME"), expected("C:\\Users\\user"));
|
||||
#[cfg(unix)]
|
||||
{
|
||||
// Set a $env:USERPROFILE variable for testing
|
||||
unsafe {
|
||||
std::env::set_var("HOME", "/Users/user");
|
||||
}
|
||||
|
||||
// ~ and $HOME should be replaced with $Env:USERPROFILE
|
||||
assert_eq!(resolve("~"), expected("/Users/user"));
|
||||
assert_eq!(resolve("$HOME"), expected("/Users/user"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user