Move Value to helpers, separate span call (#10121)

# Description

As part of the refactor to split spans off of Value, this moves to using
helper functions to create values, and using `.span()` instead of
matching span out of Value directly.

Hoping to get a few more helping hands to finish this, as there are a
lot of commands to update :)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: WindSoilder <windsoilder@outlook.com>
This commit is contained in:
JT
2023-09-04 02:27:29 +12:00
committed by GitHub
parent af79eb2943
commit 6cdfee3573
372 changed files with 5811 additions and 7448 deletions

View File

@ -131,18 +131,15 @@ If you need to distinguish dirs and files, please use `path type`."#
fn exists(path: &Path, span: Span, args: &Arguments) -> Value {
let path = expand_path_with(path, &args.pwd);
Value::Bool {
val: match path.try_exists() {
Value::bool(
match path.try_exists() {
Ok(exists) => exists,
Err(err) => {
return Value::Error {
error: Box::new(ShellError::IOErrorSpanned(err.to_string(), span)),
span,
}
return Value::error(ShellError::IOErrorSpanned(err.to_string(), span), span)
}
},
span,
}
)
}
#[cfg(test)]

View File

@ -156,8 +156,8 @@ fn expand(path: &Path, span: Span, args: &Arguments) -> Value {
Value::string(p.to_string_lossy(), span)
}
}
Err(_) => Value::Error {
error: Box::new(ShellError::GenericError(
Err(_) => Value::error(
ShellError::GenericError(
"Could not expand path".into(),
"could not be expanded (path might not exist, non-final \
component is not a directory, or other cause)"
@ -165,9 +165,9 @@ fn expand(path: &Path, span: Span, args: &Arguments) -> Value {
Some(span),
None,
Vec::new(),
)),
),
span,
},
),
}
} else if args.not_follow_symlink {
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)

View File

@ -103,10 +103,10 @@ the output of 'path parse' and 'path split' subcommands."#
Example {
description: "Join a table of structured paths into a list of paths",
example: r"[ [parent stem extension]; ['C:\Users\viking' 'spam' 'txt']] | path join",
result: Some(Value::List {
vals: vec![Value::test_string(r"C:\Users\viking\spam.txt")],
span: Span::test_data(),
}),
result: Some(Value::list(
vec![Value::test_string(r"C:\Users\viking\spam.txt")],
Span::test_data(),
)),
},
]
}
@ -137,10 +137,10 @@ the output of 'path parse' and 'path split' subcommands."#
Example {
description: "Join a table of structured paths into a list of paths",
example: r"[[ parent stem extension ]; [ '/home/viking' 'spam' 'txt' ]] | path join",
result: Some(Value::List {
vals: vec![Value::test_string(r"/home/viking/spam.txt")],
span: Span::test_data(),
}),
result: Some(Value::list(
vec![Value::test_string(r"/home/viking/spam.txt")],
Span::test_data(),
)),
},
]
}
@ -168,10 +168,11 @@ fn run(call: &Call, args: &Arguments, input: PipelineData) -> Result<PipelineDat
}
fn handle_value(v: Value, args: &Arguments, head: Span) -> Value {
let span = v.span();
match v {
Value::String { ref val, .. } => join_single(Path::new(val), head, args),
Value::Record { val, span } => join_record(&val, head, span, args),
Value::List { vals, span } => join_list(&vals, head, span, args),
Value::Record { val, .. } => join_record(&val, head, span, args),
Value::List { vals, .. } => join_list(&vals, head, span, args),
_ => super::handle_invalid_values(v, head),
}
@ -200,16 +201,16 @@ fn join_list(parts: &[Value], head: Span, span: Span, args: &Arguments) -> Value
.map(|r| join_record(r, head, span, args))
.collect();
Value::List { vals, span }
Value::list(vals, span)
}
Err(_) => Value::Error {
error: Box::new(ShellError::PipelineMismatch {
Err(_) => Value::error(
ShellError::PipelineMismatch {
exp_input_type: "string or record".into(),
dst_span: head,
src_span: span,
}),
},
span,
},
),
}
}
}
@ -218,10 +219,7 @@ fn join_list(parts: &[Value], head: Span, span: Span, args: &Arguments) -> Value
fn join_record(record: &Record, head: Span, span: Span, args: &Arguments) -> Value {
match merge_record(record, head, span) {
Ok(p) => join_single(p.as_path(), head, args),
Err(error) => Value::Error {
error: Box::new(error),
span,
},
Err(error) => Value::error(error, span),
}
}

View File

@ -36,17 +36,15 @@ where
F: Fn(&StdPath, Span, &A) -> Value + Send + Sync + 'static,
A: PathSubcommandArguments + Send + Sync + 'static,
{
let span = v.span();
match v {
Value::String { val, span } => cmd(StdPath::new(&val), span, args),
Value::String { val, .. } => cmd(StdPath::new(&val), span, args),
_ => handle_invalid_values(v, name),
}
}
fn handle_invalid_values(rest: Value, name: Span) -> Value {
Value::Error {
error: Box::new(err_from_value(&rest, name)),
span: name,
}
Value::error(err_from_value(&rest, name), name)
}
fn err_from_value(rest: &Value, name: Span) -> ShellError {

View File

@ -47,16 +47,16 @@ the path literal."#
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::String {
val: get_full_help(
Ok(Value::string(
get_full_help(
&PathCommand.signature(),
&PathCommand.examples(),
engine_state,
stack,
self.is_parser_keyword(),
),
span: call.head,
}
call.head,
)
.into_pipeline_data())
}
}

View File

@ -153,15 +153,15 @@ fn relative_to(path: &Path, span: Span, args: &Arguments) -> Value {
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) => Value::Error {
error: Box::new(ShellError::CantConvert {
Err(e) => Value::error(
ShellError::CantConvert {
to_type: e.to_string(),
from_type: "string".into(),
span,
help: None,
}),
},
span,
},
),
}
}

View File

@ -85,21 +85,21 @@ impl Command for SubCommand {
Example {
description: "Split a path into parts",
example: r"'C:\Users\viking\spam.txt' | path split",
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_string(r"C:\"),
Value::test_string("Users"),
Value::test_string("viking"),
Value::test_string("spam.txt"),
],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
Example {
description: "Split paths in list into parts",
example: r"[ C:\Users\viking\spam.txt C:\Users\viking\eggs.txt ] | path split",
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_list(vec![
Value::test_string(r"C:\"),
Value::test_string("Users"),
@ -113,8 +113,8 @@ impl Command for SubCommand {
Value::test_string("eggs.txt"),
]),
],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
]
}
@ -125,21 +125,21 @@ impl Command for SubCommand {
Example {
description: "Split a path into parts",
example: r"'/home/viking/spam.txt' | path split",
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_string("/"),
Value::test_string("home"),
Value::test_string("viking"),
Value::test_string("spam.txt"),
],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
Example {
description: "Split paths in list into parts",
example: r"[ /home/viking/spam.txt /home/viking/eggs.txt ] | path split",
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_list(vec![
Value::test_string("/"),
Value::test_string("home"),
@ -153,24 +153,23 @@ impl Command for SubCommand {
Value::test_string("eggs.txt"),
]),
],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
]
}
}
fn split(path: &Path, span: Span, _: &Arguments) -> Value {
Value::List {
vals: path
.components()
Value::list(
path.components()
.filter_map(|comp| {
let comp = process_component(comp);
comp.map(|s| Value::string(s, span))
})
.collect(),
span,
}
)
}
#[cfg(windows)]