use super::PathSubcommandArguments;
use nu_engine::command_prelude::*;
use nu_path::expand_to_real_path;
use nu_protocol::engine::StateWorkingSet;
use std::path::Path;
struct Arguments {
path: Spanned<String>,
}
impl PathSubcommandArguments for Arguments {}
#[derive(Clone)]
pub struct PathRelativeTo;
impl Command for PathRelativeTo {
fn name(&self) -> &str {
"path relative-to"
}
fn signature(&self) -> Signature {
Signature::build("path relative-to")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
])
.required(
"path",
SyntaxShape::String,
"Parent shared with the input path.",
)
.category(Category::Path)
}
fn description(&self) -> &str {
"Express a path as relative to another path."
}
fn extra_description(&self) -> &str {
"Can be used only when the input and the argument paths are either both
absolute or both relative. The argument path needs to be a parent of the input
path."
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
path: call.req(engine_state, stack, 0)?,
};
if let PipelineData::Empty = input {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&relative_to, &args, value, head),
engine_state.signals(),
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
path: call.req_const(working_set, 0)?,
};
if let PipelineData::Empty = input {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&relative_to, &args, value, head),
working_set.permanent().signals(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example<'_>> {
vec![
Example {
description: "Find a relative path from two absolute paths.",
example: r"'C:\Users\viking' | path relative-to 'C:\Users'",
result: Some(Value::test_string("viking")),
},
Example {
description: "Find a relative path from absolute paths in list.",
example: r"[ C:\Users\viking, C:\Users\spam ] | path relative-to C:\Users",
result: Some(Value::test_list(vec![
Value::test_string("viking"),
Value::test_string("spam"),
])),
},
Example {
description: "Find a relative path from two relative paths.",
example: r"'eggs\bacon\sausage\spam' | path relative-to 'eggs\bacon\sausage'",
result: Some(Value::test_string("spam")),
},
]
}
#[cfg(not(windows))]
fn examples(&self) -> Vec<Example<'_>> {
vec![
Example {
description: "Find a relative path from two absolute paths.",
example: "'/home/viking' | path relative-to '/home'",
result: Some(Value::test_string("viking")),
},
Example {
description: "Find a relative path from absolute paths in list.",
example: "[ /home/viking, /home/spam ] | path relative-to '/home'",
result: Some(Value::test_list(vec![
Value::test_string("viking"),
Value::test_string("spam"),
])),
},
Example {
description: "Find a relative path from two relative paths.",
example: "'eggs/bacon/sausage/spam' | path relative-to 'eggs/bacon/sausage'",
result: Some(Value::test_string("spam")),
},
]
}
}
fn relative_to(path: &Path, span: Span, args: &Arguments) -> Value {
let lhs = expand_to_real_path(path);
let rhs = expand_to_real_path(&args.path.item);
match lhs.strip_prefix(&rhs) {
Ok(p) => Value::string(p.to_string_lossy(), span),
Err(e) => {
if is_case_insensitive_filesystem()
&& let Some(relative_path) = try_case_insensitive_strip_prefix(&lhs, &rhs)
{
return Value::string(relative_path.to_string_lossy(), span);
}
Value::error(
ShellError::CantConvert {
to_type: e.to_string(),
from_type: "string".into(),
span,
help: None,
},
span,
)
}
}
}
fn is_case_insensitive_filesystem() -> bool {
cfg!(any(target_os = "windows", target_os = "macos"))
}
fn try_case_insensitive_strip_prefix(lhs: &Path, rhs: &Path) -> Option<std::path::PathBuf> {
let mut lhs_components = lhs.components();
let mut rhs_components = rhs.components();
loop {
match (lhs_components.next(), rhs_components.next()) {
(Some(lhs_comp), Some(rhs_comp)) => {
match (lhs_comp, rhs_comp) {
(
std::path::Component::Normal(lhs_name),
std::path::Component::Normal(rhs_name),
) => {
if lhs_name.to_string_lossy().to_lowercase()
!= rhs_name.to_string_lossy().to_lowercase()
{
return None;
}
}
_ if lhs_comp != rhs_comp => {
return None;
}
_ => {}
}
}
(Some(lhs_comp), None) => {
let mut result = std::path::PathBuf::new();
result.push(lhs_comp);
for component in lhs_components {
result.push(component);
}
return Some(result);
}
(None, Some(_)) => {
return None;
}
(None, None) => {
return Some(std::path::PathBuf::new());
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_examples() -> nu_test_support::Result {
nu_test_support::test().examples(PathRelativeTo)
}
#[test]
fn test_case_insensitive_filesystem() {
use nu_protocol::{Span, Value};
use std::path::Path;
let args = Arguments {
path: Spanned {
item: "/Etc".to_string(),
span: Span::test_data(),
},
};
let result = relative_to(Path::new("/etc"), Span::test_data(), &args);
if is_case_insensitive_filesystem() {
match result {
Value::String { val, .. } => {
assert_eq!(val, "");
}
_ => panic!("Expected string result on case-insensitive filesystem"),
}
} else {
match result {
Value::Error { .. } => {
}
_ => panic!("Expected error on case-sensitive filesystem"),
}
}
}
#[test]
fn test_case_insensitive_with_subpath() {
use nu_protocol::{Span, Value};
use std::path::Path;
let args = Arguments {
path: Spanned {
item: "/Home/User".to_string(),
span: Span::test_data(),
},
};
let result = relative_to(Path::new("/home/user/documents"), Span::test_data(), &args);
if is_case_insensitive_filesystem() {
match result {
Value::String { val, .. } => {
assert_eq!(val, "documents");
}
_ => panic!("Expected string result on case-insensitive filesystem"),
}
} else {
match result {
Value::Error { .. } => {
}
_ => panic!("Expected error on case-sensitive filesystem"),
}
}
}
#[test]
fn test_truly_different_paths() {
use nu_protocol::{Span, Value};
use std::path::Path;
let args = Arguments {
path: Spanned {
item: "/Different/Path".to_string(),
span: Span::test_data(),
},
};
let result = relative_to(Path::new("/home/user"), Span::test_data(), &args);
match result {
Value::Error { .. } => {}
_ => panic!("Expected error for truly different paths"),
}
}
}