2022-09-05 08:32:09 +02:00
|
|
|
use std::path::{Component, Path};
|
2021-12-13 02:47:14 +01:00
|
|
|
|
2023-02-05 22:17:46 +01:00
|
|
|
use nu_protocol::ast::Call;
|
2023-08-26 15:41:29 +02:00
|
|
|
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
2022-11-09 22:55:05 +01:00
|
|
|
use nu_protocol::{
|
2023-08-07 06:03:23 +02:00
|
|
|
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
2022-11-09 22:55:05 +01:00
|
|
|
};
|
2021-12-13 02:47:14 +01:00
|
|
|
|
|
|
|
use super::PathSubcommandArguments;
|
|
|
|
|
2023-07-14 23:04:22 +02:00
|
|
|
struct Arguments;
|
2021-12-13 02:47:14 +01:00
|
|
|
|
2023-07-14 23:04:22 +02:00
|
|
|
impl PathSubcommandArguments for Arguments {}
|
2021-12-13 02:47:14 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"path split"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2023-08-07 06:03:23 +02:00
|
|
|
Signature::build("path split")
|
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::String, Type::List(Box::new(Type::String))),
|
|
|
|
(
|
|
|
|
Type::List(Box::new(Type::String)),
|
|
|
|
Type::List(Box::new(Type::List(Box::new(Type::String)))),
|
|
|
|
),
|
|
|
|
])
|
|
|
|
.category(Category::Path)
|
2021-12-13 02:47:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2022-10-23 08:02:52 +02:00
|
|
|
"Split a path into a list based on the system's path separator."
|
2021-12-13 02:47:14 +01:00
|
|
|
}
|
|
|
|
|
2023-08-26 15:41:29 +02:00
|
|
|
fn is_const(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:47:14 +01:00
|
|
|
fn run(
|
|
|
|
&self,
|
2023-02-05 22:17:46 +01:00
|
|
|
engine_state: &EngineState,
|
2023-07-14 23:04:22 +02:00
|
|
|
_stack: &mut Stack,
|
2023-02-05 22:17:46 +01:00
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-12-13 02:47:14 +01:00
|
|
|
let head = call.head;
|
2023-07-14 23:04:22 +02:00
|
|
|
let args = Arguments;
|
2021-12-13 02:47:14 +01:00
|
|
|
|
Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217)
# Description
* I was dismayed to discover recently that UnsupportedInput and
TypeMismatch are used *extremely* inconsistently across the codebase.
UnsupportedInput is sometimes used for input type-checks (as per the
name!!), but *also* used for argument type-checks. TypeMismatch is also
used for both.
I thus devised the following standard: input type-checking *only* uses
UnsupportedInput, and argument type-checking *only* uses TypeMismatch.
Moreover, to differentiate them, UnsupportedInput now has *two* error
arrows (spans), one pointing at the command and the other at the input
origin, while TypeMismatch only has the one (because the command should
always be nearby)
* In order to apply that standard, a very large number of
UnsupportedInput uses were changed so that the input's span could be
retrieved and delivered to it.
* Additionally, I noticed many places where **errors are not propagated
correctly**: there are lots of `match` sites which take a Value::Error,
then throw it away and replace it with a new Value::Error with
less/misleading information (such as reporting the error as an
"incorrect type"). I believe that the earliest errors are the most
important, and should always be propagated where possible.
* Also, to standardise one broad subset of UnsupportedInput error
messages, who all used slightly different wordings of "expected
`<type>`, got `<type>`", I created OnlySupportsThisInputType as a
variant of it.
* Finally, a bunch of error sites that had "repeated spans" - i.e. where
an error expected two spans, but `call.head` was given for both - were
fixed to use different spans.
# Example
BEFORE
```
〉20b | str starts-with 'a'
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #31:1:1]
1 │ 20b | str starts-with 'a'
· ┬
· ╰── Input's type is filesize. This command only works with strings.
╰────
〉'a' | math cos
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #33:1:1]
1 │ 'a' | math cos
· ─┬─
· ╰── Only numerical values are supported, input type: String
╰────
〉0x[12] | encode utf8
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #38:1:1]
1 │ 0x[12] | encode utf8
· ───┬──
· ╰── non-string input
╰────
```
AFTER
```
〉20b | str starts-with 'a'
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #1:1:1]
1 │ 20b | str starts-with 'a'
· ┬ ───────┬───────
· │ ╰── only string input data is supported
· ╰── input type: filesize
╰────
〉'a' | math cos
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #2:1:1]
1 │ 'a' | math cos
· ─┬─ ────┬───
· │ ╰── only numeric input data is supported
· ╰── input type: string
╰────
〉0x[12] | encode utf8
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #3:1:1]
1 │ 0x[12] | encode utf8
· ───┬── ───┬──
· │ ╰── only string input data is supported
· ╰── input type: binary
╰────
```
# User-Facing Changes
Various error messages suddenly make more sense (i.e. have two arrows
instead of one).
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2022-12-23 07:48:53 +01:00
|
|
|
// This doesn't match explicit nulls
|
|
|
|
if matches!(input, PipelineData::Empty) {
|
2023-03-01 20:34:48 +01:00
|
|
|
return Err(ShellError::PipelineEmpty { dst_span: head });
|
Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217)
# Description
* I was dismayed to discover recently that UnsupportedInput and
TypeMismatch are used *extremely* inconsistently across the codebase.
UnsupportedInput is sometimes used for input type-checks (as per the
name!!), but *also* used for argument type-checks. TypeMismatch is also
used for both.
I thus devised the following standard: input type-checking *only* uses
UnsupportedInput, and argument type-checking *only* uses TypeMismatch.
Moreover, to differentiate them, UnsupportedInput now has *two* error
arrows (spans), one pointing at the command and the other at the input
origin, while TypeMismatch only has the one (because the command should
always be nearby)
* In order to apply that standard, a very large number of
UnsupportedInput uses were changed so that the input's span could be
retrieved and delivered to it.
* Additionally, I noticed many places where **errors are not propagated
correctly**: there are lots of `match` sites which take a Value::Error,
then throw it away and replace it with a new Value::Error with
less/misleading information (such as reporting the error as an
"incorrect type"). I believe that the earliest errors are the most
important, and should always be propagated where possible.
* Also, to standardise one broad subset of UnsupportedInput error
messages, who all used slightly different wordings of "expected
`<type>`, got `<type>`", I created OnlySupportsThisInputType as a
variant of it.
* Finally, a bunch of error sites that had "repeated spans" - i.e. where
an error expected two spans, but `call.head` was given for both - were
fixed to use different spans.
# Example
BEFORE
```
〉20b | str starts-with 'a'
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #31:1:1]
1 │ 20b | str starts-with 'a'
· ┬
· ╰── Input's type is filesize. This command only works with strings.
╰────
〉'a' | math cos
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #33:1:1]
1 │ 'a' | math cos
· ─┬─
· ╰── Only numerical values are supported, input type: String
╰────
〉0x[12] | encode utf8
Error: nu::shell::unsupported_input (link)
× Unsupported input
╭─[entry #38:1:1]
1 │ 0x[12] | encode utf8
· ───┬──
· ╰── non-string input
╰────
```
AFTER
```
〉20b | str starts-with 'a'
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #1:1:1]
1 │ 20b | str starts-with 'a'
· ┬ ───────┬───────
· │ ╰── only string input data is supported
· ╰── input type: filesize
╰────
〉'a' | math cos
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #2:1:1]
1 │ 'a' | math cos
· ─┬─ ────┬───
· │ ╰── only numeric input data is supported
· ╰── input type: string
╰────
〉0x[12] | encode utf8
Error: nu::shell::pipeline_mismatch (link)
× Pipeline mismatch.
╭─[entry #3:1:1]
1 │ 0x[12] | encode utf8
· ───┬── ───┬──
· │ ╰── only string input data is supported
· ╰── input type: binary
╰────
```
# User-Facing Changes
Various error messages suddenly make more sense (i.e. have two arrows
instead of one).
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2022-12-23 07:48:53 +01:00
|
|
|
}
|
2021-12-13 02:47:14 +01:00
|
|
|
input.map(
|
|
|
|
move |value| super::operate(&split, &args, value, head),
|
|
|
|
engine_state.ctrlc.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-26 15:41:29 +02:00
|
|
|
fn run_const(
|
|
|
|
&self,
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let head = call.head;
|
|
|
|
let args = Arguments;
|
|
|
|
|
|
|
|
// This doesn't match explicit nulls
|
|
|
|
if matches!(input, PipelineData::Empty) {
|
|
|
|
return Err(ShellError::PipelineEmpty { dst_span: head });
|
|
|
|
}
|
|
|
|
input.map(
|
|
|
|
move |value| super::operate(&split, &args, value, head),
|
|
|
|
working_set.permanent().ctrlc.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:47:14 +01:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Split a path into parts",
|
|
|
|
example: r"'C:\Users\viking\spam.txt' | path split",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2022-09-05 08:32:09 +02:00
|
|
|
Value::test_string(r"C:\"),
|
2021-12-13 02:47:14 +01:00
|
|
|
Value::test_string("Users"),
|
|
|
|
Value::test_string("viking"),
|
|
|
|
Value::test_string("spam.txt"),
|
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2021-12-13 02:47:14 +01:00
|
|
|
},
|
|
|
|
Example {
|
2023-07-14 23:04:22 +02:00
|
|
|
description: "Split paths in list into parts",
|
|
|
|
example: r"[ C:\Users\viking\spam.txt C:\Users\viking\eggs.txt ] | path split",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2023-07-14 23:04:22 +02:00
|
|
|
Value::test_list(vec![
|
|
|
|
Value::test_string(r"C:\"),
|
|
|
|
Value::test_string("Users"),
|
|
|
|
Value::test_string("viking"),
|
|
|
|
Value::test_string("spam.txt"),
|
|
|
|
]),
|
|
|
|
Value::test_list(vec![
|
|
|
|
Value::test_string(r"C:\"),
|
|
|
|
Value::test_string("Users"),
|
|
|
|
Value::test_string("viking"),
|
|
|
|
Value::test_string("eggs.txt"),
|
|
|
|
]),
|
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2021-12-13 02:47:14 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Split a path into parts",
|
|
|
|
example: r"'/home/viking/spam.txt' | path split",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2021-12-13 02:47:14 +01:00
|
|
|
Value::test_string("/"),
|
|
|
|
Value::test_string("home"),
|
|
|
|
Value::test_string("viking"),
|
|
|
|
Value::test_string("spam.txt"),
|
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2021-12-13 02:47:14 +01:00
|
|
|
},
|
|
|
|
Example {
|
2023-07-14 23:04:22 +02:00
|
|
|
description: "Split paths in list into parts",
|
|
|
|
example: r"[ /home/viking/spam.txt /home/viking/eggs.txt ] | path split",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2023-07-14 23:04:22 +02:00
|
|
|
Value::test_list(vec![
|
|
|
|
Value::test_string("/"),
|
|
|
|
Value::test_string("home"),
|
|
|
|
Value::test_string("viking"),
|
|
|
|
Value::test_string("spam.txt"),
|
|
|
|
]),
|
|
|
|
Value::test_list(vec![
|
|
|
|
Value::test_string("/"),
|
|
|
|
Value::test_string("home"),
|
|
|
|
Value::test_string("viking"),
|
|
|
|
Value::test_string("eggs.txt"),
|
|
|
|
]),
|
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2021-12-13 02:47:14 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn split(path: &Path, span: Span, _: &Arguments) -> Value {
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::list(
|
|
|
|
path.components()
|
2022-09-05 08:32:09 +02:00
|
|
|
.filter_map(|comp| {
|
|
|
|
let comp = process_component(comp);
|
|
|
|
comp.map(|s| Value::string(s, span))
|
2021-12-13 02:47:14 +01:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
span,
|
2023-09-03 16:27:29 +02:00
|
|
|
)
|
2021-12-13 02:47:14 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:32:09 +02:00
|
|
|
#[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())
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:47:14 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|