2023-06-22 21:45:54 +00:00
|
|
|
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
Add `command_prelude` module (#12291)
# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Type, Value,
};
```
This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```
This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
2024-03-26 21:17:30 +00:00
|
|
|
use nu_engine::command_prelude::*;
|
2022-07-11 11:26:00 +00:00
|
|
|
|
|
|
|
struct Arguments {
|
|
|
|
pattern: Vec<u8>,
|
|
|
|
end: bool,
|
2022-10-29 21:29:46 +00:00
|
|
|
cell_paths: Option<Vec<CellPath>>,
|
2022-07-11 11:26:00 +00:00
|
|
|
all: bool,
|
|
|
|
}
|
|
|
|
|
2022-10-29 21:29:46 +00:00
|
|
|
impl CmdArgument for Arguments {
|
|
|
|
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
|
|
|
|
self.cell_paths.take()
|
2022-07-11 11:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BytesRemove;
|
|
|
|
|
|
|
|
impl Command for BytesRemove {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"bytes remove"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("bytes remove")
|
2023-07-14 03:20:35 +00:00
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::Binary, Type::Binary),
|
2024-04-24 15:46:35 +00:00
|
|
|
(Type::table(), Type::table()),
|
|
|
|
(Type::record(), Type::record()),
|
2023-07-14 03:20:35 +00:00
|
|
|
])
|
2023-12-15 06:32:37 +00:00
|
|
|
.required("pattern", SyntaxShape::Binary, "The pattern to find.")
|
2022-07-11 11:26:00 +00:00
|
|
|
.rest(
|
|
|
|
"rest",
|
|
|
|
SyntaxShape::CellPath,
|
2023-12-15 06:32:37 +00:00
|
|
|
"For a data structure input, remove bytes from data at the given cell paths.",
|
2022-07-11 11:26:00 +00:00
|
|
|
)
|
|
|
|
.switch("end", "remove from end of binary", Some('e'))
|
|
|
|
.switch("all", "remove occurrences of finding binary", Some('a'))
|
|
|
|
.category(Category::Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-03-01 05:33:02 +00:00
|
|
|
"Remove bytes."
|
2022-07-11 11:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["search", "shift", "switch"]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-10-29 21:29:46 +00:00
|
|
|
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
|
2022-11-04 15:27:23 +00:00
|
|
|
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
2022-07-11 11:26:00 +00:00
|
|
|
let pattern_to_remove = call.req::<Spanned<Vec<u8>>>(engine_state, stack, 0)?;
|
|
|
|
if pattern_to_remove.item.is_empty() {
|
2023-03-06 10:31:07 +00:00
|
|
|
return Err(ShellError::TypeMismatch {
|
|
|
|
err_message: "the pattern to remove cannot be empty".to_string(),
|
|
|
|
span: pattern_to_remove.span,
|
|
|
|
});
|
2022-07-11 11:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let pattern_to_remove: Vec<u8> = pattern_to_remove.item;
|
|
|
|
let arg = Arguments {
|
|
|
|
pattern: pattern_to_remove,
|
2024-01-11 15:19:48 +00:00
|
|
|
end: call.has_flag(engine_state, stack, "end")?,
|
2022-10-29 21:29:46 +00:00
|
|
|
cell_paths,
|
2024-01-11 15:19:48 +00:00
|
|
|
all: call.has_flag(engine_state, stack, "all")?,
|
2022-07-11 11:26:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
operate(remove, arg, input, call.head, engine_state.ctrlc.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Remove contents",
|
|
|
|
example: "0x[10 AA FF AA FF] | bytes remove 0x[10 AA]",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_binary (
|
2023-09-03 14:27:29 +00:00
|
|
|
vec![0xFF, 0xAA, 0xFF],
|
|
|
|
)),
|
2022-07-11 11:26:00 +00:00
|
|
|
},
|
|
|
|
Example {
|
2023-07-26 21:13:57 +00:00
|
|
|
description: "Remove all occurrences of find binary in record field",
|
2023-10-05 16:45:28 +00:00
|
|
|
example: "{ data: 0x[10 AA 10 BB 10] } | bytes remove --all 0x[10] data",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_record(record! {
|
|
|
|
"data" => Value::test_binary(vec![0xAA, 0xBB])
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
})),
|
2022-07-11 11:26:00 +00:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Remove occurrences of find binary from end",
|
2023-10-05 16:45:28 +00:00
|
|
|
example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[10]",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_binary (
|
2023-09-03 14:27:29 +00:00
|
|
|
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
|
|
|
|
)),
|
2022-07-11 11:26:00 +00:00
|
|
|
},
|
2023-12-27 23:01:55 +00:00
|
|
|
Example {
|
|
|
|
description: "Remove find binary from end not found",
|
|
|
|
example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[11]",
|
|
|
|
result: Some(Value::test_binary (
|
|
|
|
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA, 0x10],
|
|
|
|
)),
|
|
|
|
},
|
2022-07-11 11:26:00 +00:00
|
|
|
Example {
|
|
|
|
description: "Remove all occurrences of find binary in table",
|
|
|
|
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_list (
|
|
|
|
vec![Value::test_record(record! {
|
|
|
|
"ColA" => Value::test_binary ( vec![0x12, 0x13],),
|
|
|
|
"ColB" => Value::test_binary ( vec![0x14, 0x15, 0x16],),
|
|
|
|
"ColC" => Value::test_binary ( vec![0x17, 0x18, 0x19],),
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
})],
|
2023-09-03 14:27:29 +00:00
|
|
|
)),
|
2022-07-11 11:26:00 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 21:29:46 +00:00
|
|
|
fn remove(val: &Value, args: &Arguments, span: Span) -> Value {
|
2023-09-03 14:27:29 +00:00
|
|
|
let val_span = val.span();
|
2022-10-29 21:29:46 +00:00
|
|
|
match val {
|
2023-09-03 14:27:29 +00:00
|
|
|
Value::Binary { val, .. } => remove_impl(val, args, val_span),
|
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 06:48:53 +00:00
|
|
|
// Propagate errors by explicitly matching them before the final case.
|
|
|
|
Value::Error { .. } => val.clone(),
|
2023-09-03 14:27:29 +00:00
|
|
|
other => Value::error(
|
|
|
|
ShellError::OnlySupportsThisInputType {
|
2023-03-01 19:34:48 +00:00
|
|
|
exp_input_type: "binary".into(),
|
|
|
|
wrong_type: other.get_type().to_string(),
|
|
|
|
dst_span: span,
|
2023-08-24 20:48:05 +00:00
|
|
|
src_span: other.span(),
|
2023-09-03 14:27:29 +00:00
|
|
|
},
|
2023-08-24 20:48:05 +00:00
|
|
|
span,
|
2023-09-03 14:27:29 +00:00
|
|
|
),
|
2022-10-29 21:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
|
2022-07-11 11:26:00 +00:00
|
|
|
let mut result = vec![];
|
|
|
|
let remove_all = arg.all;
|
|
|
|
let input_len = input.len();
|
|
|
|
let pattern_len = arg.pattern.len();
|
|
|
|
|
|
|
|
// Note:
|
|
|
|
// remove_all from start and end will generate the same result.
|
2023-01-15 02:03:32 +00:00
|
|
|
// so we'll put `remove_all` relative logic into else clause.
|
2022-07-11 11:26:00 +00:00
|
|
|
if arg.end && !remove_all {
|
|
|
|
let (mut left, mut right) = (
|
|
|
|
input.len() as isize - arg.pattern.len() as isize,
|
|
|
|
input.len() as isize,
|
|
|
|
);
|
|
|
|
while left >= 0 && input[left as usize..right as usize] != arg.pattern {
|
|
|
|
result.push(input[right as usize - 1]);
|
|
|
|
left -= 1;
|
|
|
|
right -= 1;
|
|
|
|
}
|
2023-01-15 02:03:32 +00:00
|
|
|
// append the remaining thing to result, this can be happening when
|
2022-07-11 11:26:00 +00:00
|
|
|
// we have something to remove and remove_all is False.
|
2023-12-27 23:01:55 +00:00
|
|
|
// check if the left is positive, if it is not, we don't need to append anything.
|
|
|
|
if left > 0 {
|
|
|
|
let mut remain = input[..left as usize].iter().copied().rev().collect();
|
|
|
|
result.append(&mut remain);
|
|
|
|
}
|
2022-07-11 11:26:00 +00:00
|
|
|
result = result.into_iter().rev().collect();
|
2023-09-03 14:27:29 +00:00
|
|
|
Value::binary(result, span)
|
2022-07-11 11:26:00 +00:00
|
|
|
} else {
|
|
|
|
let (mut left, mut right) = (0, arg.pattern.len());
|
|
|
|
while right <= input_len {
|
|
|
|
if input[left..right] == arg.pattern {
|
|
|
|
left += pattern_len;
|
|
|
|
right += pattern_len;
|
|
|
|
if !remove_all {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(input[left]);
|
|
|
|
left += 1;
|
|
|
|
right += 1;
|
|
|
|
}
|
|
|
|
}
|
Fix typos by codespell (#7600)
# Description
Found via `codespell -S target -L
crate,ser,numer,falsy,ro,te,nd,bu,ndoes,statics,ons,fo,rouge,pard`
# User-Facing Changes
None.
# Tests + Formatting
None and done.
# After Submitting
None.
2022-12-26 07:31:26 +00:00
|
|
|
// append the remaining thing to result, this can happened when
|
2022-07-11 11:26:00 +00:00
|
|
|
// we have something to remove and remove_all is False.
|
|
|
|
let mut remain = input[left..].to_vec();
|
|
|
|
result.append(&mut remain);
|
2023-09-03 14:27:29 +00:00
|
|
|
Value::binary(result, span)
|
2022-07-11 11:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(BytesRemove {})
|
|
|
|
}
|
|
|
|
}
|