let path split keeps 'C:\' together (#6485)

* `path split` keeps 'C:\' together

* fmt

* fix clippt

* fix match arm
This commit is contained in:
pwygab 2022-09-05 14:32:09 +08:00 committed by GitHub
parent a6ba58ec41
commit daec3fc3d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
use std::path::Path; use std::path::{Component, Path};
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::{engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Value}; use nu_protocol::{engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Value};
@ -62,8 +62,7 @@ impl Command for SubCommand {
example: r"'C:\Users\viking\spam.txt' | path split", example: r"'C:\Users\viking\spam.txt' | path split",
result: Some(Value::List { result: Some(Value::List {
vals: vec![ vals: vec![
Value::test_string("C:"), Value::test_string(r"C:\"),
Value::test_string(r"\"),
Value::test_string("Users"), Value::test_string("Users"),
Value::test_string("viking"), Value::test_string("viking"),
Value::test_string("spam.txt"), Value::test_string("spam.txt"),
@ -108,15 +107,33 @@ fn split(path: &Path, span: Span, _: &Arguments) -> Value {
Value::List { Value::List {
vals: path vals: path
.components() .components()
.map(|comp| { .filter_map(|comp| {
let s = comp.as_os_str().to_string_lossy(); let comp = process_component(comp);
Value::string(s, span) comp.map(|s| Value::string(s, span))
}) })
.collect(), .collect(),
span, span,
} }
} }
#[cfg(windows)]
fn process_component(comp: Component) -> Option<String> {
match comp {
Component::RootDir => None,
Component::Prefix(_) => {
let mut s = comp.as_os_str().to_string_lossy().to_string();
s.push('\\');
Some(s)
}
comp => Some(comp.as_os_str().to_string_lossy().to_string()),
}
}
#[cfg(not(windows))]
fn process_component(comp: Component) -> Option<String> {
Some(comp.as_os_str().to_string_lossy().to_string())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;