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

@ -91,56 +91,50 @@ impl Command for BytesAdd {
Example {
description: "Add bytes `0x[AA]` to `0x[1F FF AA AA]`",
example: "0x[1F FF AA AA] | bytes add 0x[AA]",
result: Some(Value::Binary {
val: vec![0xAA, 0x1F, 0xFF, 0xAA, 0xAA],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0xAA, 0x1F, 0xFF, 0xAA, 0xAA],
Span::test_data(),
)),
},
Example {
description: "Add bytes `0x[AA BB]` to `0x[1F FF AA AA]` at index 1",
example: "0x[1F FF AA AA] | bytes add 0x[AA BB] -i 1",
result: Some(Value::Binary {
val: vec![0x1F, 0xAA, 0xBB, 0xFF, 0xAA, 0xAA],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0x1F, 0xAA, 0xBB, 0xFF, 0xAA, 0xAA],
Span::test_data(),
)),
},
Example {
description: "Add bytes `0x[11]` to `0x[FF AA AA]` at the end",
example: "0x[FF AA AA] | bytes add 0x[11] -e",
result: Some(Value::Binary {
val: vec![0xFF, 0xAA, 0xAA, 0x11],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0xFF, 0xAA, 0xAA, 0x11],
Span::test_data(),
)),
},
Example {
description: "Add bytes `0x[11 22 33]` to `0x[FF AA AA]` at the end, at index 1(the index is start from end)",
example: "0x[FF AA BB] | bytes add 0x[11 22 33] -e -i 1",
result: Some(Value::Binary {
val: vec![0xFF, 0xAA, 0x11, 0x22, 0x33, 0xBB],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0xFF, 0xAA, 0x11, 0x22, 0x33, 0xBB],
Span::test_data(),
)),
},
]
}
}
fn add(val: &Value, args: &Arguments, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => add_impl(val, args, *val_span),
Value::Binary { val, .. } => add_impl(val, args, val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}
@ -151,12 +145,12 @@ fn add_impl(input: &[u8], args: &Arguments, span: Span) -> Value {
let mut added_data = args.added_data.clone();
let mut result = input.to_vec();
result.append(&mut added_data);
Value::Binary { val: result, span }
Value::binary(result, span)
} else {
let mut result = args.added_data.clone();
let mut input = input.to_vec();
result.append(&mut input);
Value::Binary { val: result, span }
Value::binary(result, span)
}
}
Some(mut indx) => {
@ -175,7 +169,7 @@ fn add_impl(input: &[u8], args: &Arguments, span: Span) -> Value {
result.append(&mut added_data);
let mut after_data = input[inserted_index..].to_vec();
result.append(&mut after_data);
Value::Binary { val: result, span }
Value::binary(result, span)
}
}
}

View File

@ -94,18 +94,12 @@ impl Command for BytesAt {
Example {
description: "Get a subbytes `0x[10 01]` from the bytes `0x[33 44 55 10 01 13]`",
example: " 0x[33 44 55 10 01 13] | bytes at 3..<4",
result: Some(Value::Binary {
val: vec![0x10],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0x10], Span::test_data())),
},
Example {
description: "Get a subbytes `0x[10 01 13]` from the bytes `0x[33 44 55 10 01 13]`",
example: " 0x[33 44 55 10 01 13] | bytes at 3..6",
result: Some(Value::Binary {
val: vec![0x10, 0x01, 0x13],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0x10, 0x01, 0x13], Span::test_data())),
},
Example {
description: "Get the remaining characters from a starting index",
@ -118,35 +112,26 @@ impl Command for BytesAt {
Example {
description: "Get the characters from the beginning until ending index",
example: " 0x[33 44 55 10 01 13] | bytes at ..<4",
result: Some(Value::Binary {
val: vec![0x33, 0x44, 0x55, 0x10],
span: Span::test_data(),
}),
result: Some(Value::binary(
vec![0x33, 0x44, 0x55, 0x10],
Span::test_data(),
)),
},
Example {
description:
"Or the characters from the beginning until ending index inside a table",
example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes at 1.. ColB ColC"#,
result: Some(Value::List {
vals: vec![Value::test_record(Record {
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::Binary {
val: vec![0x11, 0x12, 0x13],
span: Span::test_data(),
},
Value::Binary {
val: vec![0x15, 0x16],
span: Span::test_data(),
},
Value::Binary {
val: vec![0x18, 0x19],
span: Span::test_data(),
},
Value::binary(vec![0x11, 0x12, 0x13], Span::test_data()),
Value::binary(vec![0x15, 0x16], Span::test_data()),
Value::binary(vec![0x18, 0x19], Span::test_data()),
],
})],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
]
}
@ -169,51 +154,46 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
if start < len && end >= 0 {
match start.cmp(&end) {
Ordering::Equal => Value::Binary {
val: vec![],
span: head,
},
Ordering::Greater => Value::Error {
error: Box::new(ShellError::TypeMismatch {
Ordering::Equal => Value::binary(vec![], head),
Ordering::Greater => Value::error(
ShellError::TypeMismatch {
err_message: "End must be greater than or equal to Start".to_string(),
span: head,
}),
span: head,
},
Ordering::Less => Value::Binary {
val: {
if end == isize::max_value() {
val.iter().skip(start as usize).copied().collect()
} else {
val.iter()
.skip(start as usize)
.take((end - start) as usize)
.copied()
.collect()
}
},
span: head,
},
head,
),
Ordering::Less => Value::binary(
if end == isize::max_value() {
val.iter()
.skip(start as usize)
.copied()
.collect::<Vec<u8>>()
} else {
val.iter()
.skip(start as usize)
.take((end - start) as usize)
.copied()
.collect()
},
head,
),
}
} else {
Value::Binary {
val: vec![],
span: head,
}
Value::binary(vec![], head)
}
}
Value::Error { .. } => input.clone(),
other => Value::Error {
error: Box::new(ShellError::UnsupportedInput(
other => Value::error(
ShellError::UnsupportedInput(
"Only binary values are supported".into(),
format!("input type: {:?}", other.get_type()),
head,
// This line requires the Value::Error match above.
other.span(),
)),
span: head,
},
),
head,
),
}
}

View File

@ -33,10 +33,10 @@ impl Command for BytesBuild {
vec![Example {
example: "bytes build 0x[01 02] 0x[03] 0x[04]",
description: "Builds binary data from 0x[01 02], 0x[03], 0x[04]",
result: Some(Value::Binary {
val: vec![0x01, 0x02, 0x03, 0x04],
span: Span::test_data(),
}),
result: Some(Value::binary(
vec![0x01, 0x02, 0x03, 0x04],
Span::test_data(),
)),
}]
}
@ -63,11 +63,7 @@ impl Command for BytesBuild {
}
}
Ok(Value::Binary {
val: output,
span: call.head,
}
.into_pipeline_data())
Ok(Value::binary(output, call.head).into_pipeline_data())
}
}

View File

@ -34,16 +34,16 @@ impl Command for Bytes {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::String {
val: get_full_help(
Ok(Value::string(
get_full_help(
&Bytes.signature(),
&Bytes.examples(),
engine_state,
stack,
self.is_parser_keyword(),
),
span: call.head,
}
call.head,
)
.into_pipeline_data())
}
}

View File

@ -68,28 +68,16 @@ impl Command for BytesCollect {
}
match separator {
None => Ok(Value::Binary {
val: output_binary,
span: call.head,
}
.into_pipeline_data()),
None => Ok(Value::binary(output_binary, call.head).into_pipeline_data()),
Some(sep) => {
if output_binary.is_empty() {
Ok(Value::Binary {
val: output_binary,
span: call.head,
}
.into_pipeline_data())
Ok(Value::binary(output_binary, call.head).into_pipeline_data())
} else {
// have push one extra separator in previous step, pop them out.
for _ in sep {
let _ = output_binary.pop();
}
Ok(Value::Binary {
val: output_binary,
span: call.head,
}
.into_pipeline_data())
Ok(Value::binary(output_binary, call.head).into_pipeline_data())
}
}
}
@ -100,18 +88,15 @@ impl Command for BytesCollect {
Example {
description: "Create a byte array from input",
example: "[0x[11] 0x[13 15]] | bytes collect",
result: Some(Value::Binary {
val: vec![0x11, 0x13, 0x15],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0x11, 0x13, 0x15], Span::test_data())),
},
Example {
description: "Create a byte array from input with a separator",
example: "[0x[11] 0x[33] 0x[44]] | bytes collect 0x[01]",
result: Some(Value::Binary {
val: vec![0x11, 0x01, 0x33, 0x01, 0x44],
span: Span::test_data(),
}),
result: Some(Value::binary(
vec![0x11, 0x01, 0x33, 0x01, 0x44],
Span::test_data(),
)),
},
]
}

View File

@ -89,22 +89,20 @@ impl Command for BytesEndsWith {
}
fn ends_with(val: &Value, args: &Arguments, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => Value::bool(val.ends_with(&args.pattern), *val_span),
Value::Binary { val, .. } => Value::bool(val.ends_with(&args.pattern), val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}

View File

@ -95,58 +95,53 @@ impl Command for BytesIndexOf {
Example {
description: "Returns all matched index",
example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of -a 0x[33 44]",
result: Some(Value::List {
vals: vec![Value::test_int(0), Value::test_int(5), Value::test_int(7)],
span: Span::test_data(),
}),
result: Some(Value::list(
vec![Value::test_int(0), Value::test_int(5), Value::test_int(7)],
Span::test_data(),
)),
},
Example {
description: "Returns all matched index, searching from end",
example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of -a -e 0x[33 44]",
result: Some(Value::List {
vals: vec![Value::test_int(7), Value::test_int(5), Value::test_int(0)],
span: Span::test_data(),
}),
result: Some(Value::list(
vec![Value::test_int(7), Value::test_int(5), Value::test_int(0)],
Span::test_data(),
)),
},
Example {
description: "Returns index of pattern for specific column",
example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes index-of 0x[11] ColA ColC"#,
result: Some(Value::List {
vals: vec![Value::test_record(Record {
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::test_int(0),
Value::Binary {
val: vec![0x14, 0x15, 0x16],
span: Span::test_data(),
},
Value::binary(vec![0x14, 0x15, 0x16], Span::test_data()),
Value::test_int(-1),
],
})],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
]
}
}
fn index_of(val: &Value, args: &Arguments, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => index_of_impl(val, args, *val_span),
Value::Binary { val, .. } => index_of_impl(val, args, val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}
@ -157,22 +152,20 @@ fn index_of_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
let mut iter = input.windows(arg.pattern.len());
if arg.end {
Value::Int {
val: iter
.rev()
Value::int(
iter.rev()
.position(|sub_bytes| sub_bytes == arg.pattern)
.map(|x| (input.len() - arg.pattern.len() - x) as i64)
.unwrap_or(-1),
span,
}
)
} else {
Value::Int {
val: iter
.position(|sub_bytes| sub_bytes == arg.pattern)
Value::int(
iter.position(|sub_bytes| sub_bytes == arg.pattern)
.map(|x| x as i64)
.unwrap_or(-1),
span,
}
)
}
}
}
@ -186,10 +179,7 @@ fn search_all_index(input: &[u8], pattern: &[u8], from_end: bool, span: Span) ->
);
while left >= 0 {
if &input[left as usize..right as usize] == pattern {
result.push(Value::Int {
val: left as i64,
span,
});
result.push(Value::int(left as i64, span));
left -= pattern.len() as isize;
right -= pattern.len() as isize;
} else {
@ -197,7 +187,7 @@ fn search_all_index(input: &[u8], pattern: &[u8], from_end: bool, span: Span) ->
right -= 1;
}
}
Value::List { vals: result, span }
Value::list(result, span)
} else {
// doing find stuff.
let (mut left, mut right) = (0, pattern.len());
@ -205,10 +195,7 @@ fn search_all_index(input: &[u8], pattern: &[u8], from_end: bool, span: Span) ->
let pattern_len = pattern.len();
while right <= input_len {
if &input[left..right] == pattern {
result.push(Value::Int {
val: left as i64,
span,
});
result.push(Value::int(left as i64, span));
left += pattern_len;
right += pattern_len;
} else {
@ -217,7 +204,7 @@ fn search_all_index(input: &[u8], pattern: &[u8], from_end: bool, span: Span) ->
}
}
Value::List { vals: result, span }
Value::list(result, span)
}
}

View File

@ -64,32 +64,30 @@ impl Command for BytesLen {
Example {
description: "Return the lengths of multiple binaries",
example: "[0x[1F FF AA AB] 0x[1F]] | bytes length",
result: Some(Value::List {
vals: vec![Value::test_int(4), Value::test_int(1)],
span: Span::test_data(),
}),
result: Some(Value::list(
vec![Value::test_int(4), Value::test_int(1)],
Span::test_data(),
)),
},
]
}
}
fn length(val: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => Value::int(val.len() as i64, *val_span),
Value::Binary { val, .. } => Value::int(val.len() as i64, val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}

View File

@ -87,10 +87,10 @@ impl Command for BytesRemove {
Example {
description: "Remove contents",
example: "0x[10 AA FF AA FF] | bytes remove 0x[10 AA]",
result: Some(Value::Binary {
val: vec![0xFF, 0xAA, 0xFF],
span: Span::test_data(),
}),
result: Some(Value::binary (
vec![0xFF, 0xAA, 0xFF],
Span::test_data(),
)),
},
Example {
description: "Remove all occurrences of find binary in record field",
@ -103,56 +103,54 @@ impl Command for BytesRemove {
Example {
description: "Remove occurrences of find binary from end",
example: "0x[10 AA 10 BB CC AA 10] | bytes remove -e 0x[10]",
result: Some(Value::Binary {
val: vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
span: Span::test_data(),
}),
result: Some(Value::binary (
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
Span::test_data(),
)),
},
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",
result: Some(Value::List {
vals: vec![Value::test_record(Record {
result: Some(Value::list (
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::Binary {
val: vec![0x12, 0x13],
span: Span::test_data(),
},
Value::Binary {
val: vec![0x14, 0x15, 0x16],
span: Span::test_data(),
},
Value::Binary {
val: vec![0x17, 0x18, 0x19],
span: Span::test_data(),
},
Value::binary (
vec![0x12, 0x13],
Span::test_data(),
),
Value::binary (
vec![0x14, 0x15, 0x16],
Span::test_data(),
),
Value::binary (
vec![0x17, 0x18, 0x19],
Span::test_data(),
),
]
})],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
]
}
}
fn remove(val: &Value, args: &Arguments, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => remove_impl(val, args, *val_span),
Value::Binary { val, .. } => remove_impl(val, args, val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}
@ -180,7 +178,7 @@ fn remove_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
let mut remain = input[..left as usize].iter().copied().rev().collect();
result.append(&mut remain);
result = result.into_iter().rev().collect();
Value::Binary { val: result, span }
Value::binary(result, span)
} else {
let (mut left, mut right) = (0, arg.pattern.len());
while right <= input_len {
@ -200,7 +198,7 @@ fn remove_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
// we have something to remove and remove_all is False.
let mut remain = input[left..].to_vec();
result.append(&mut remain);
Value::Binary { val: result, span }
Value::binary(result, span)
}
}

View File

@ -87,64 +87,62 @@ impl Command for BytesReplace {
Example {
description: "Find and replace contents",
example: "0x[10 AA FF AA FF] | bytes replace 0x[10 AA] 0x[FF]",
result: Some(Value::Binary {
val: vec![0xFF, 0xFF, 0xAA, 0xFF],
span: Span::test_data(),
}),
result: Some(Value::binary (
vec![0xFF, 0xFF, 0xAA, 0xFF],
Span::test_data(),
)),
},
Example {
description: "Find and replace all occurrences of find binary",
example: "0x[10 AA 10 BB 10] | bytes replace -a 0x[10] 0x[A0]",
result: Some(Value::Binary {
val: vec![0xA0, 0xAA, 0xA0, 0xBB, 0xA0],
span: Span::test_data(),
}),
result: Some(Value::binary (
vec![0xA0, 0xAA, 0xA0, 0xBB, 0xA0],
Span::test_data(),
)),
},
Example {
description: "Find and replace all occurrences of find binary in table",
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes replace -a 0x[11] 0x[13] ColA ColC",
result: Some(Value::List {
vals: vec![Value::test_record(Record {
result: Some(Value::list (
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::Binary {
val: vec![0x13, 0x12, 0x13],
span: Span::test_data(),
},
Value::Binary {
val: vec![0x14, 0x15, 0x16],
span: Span::test_data(),
},
Value::Binary {
val: vec![0x17, 0x18, 0x19],
span: Span::test_data(),
},
Value::binary (
vec![0x13, 0x12, 0x13],
Span::test_data(),
),
Value::binary (
vec![0x14, 0x15, 0x16],
Span::test_data(),
),
Value::binary (
vec![0x17, 0x18, 0x19],
Span::test_data(),
),
],
})],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
]
}
}
fn replace(val: &Value, args: &Arguments, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => replace_impl(val, args, *val_span),
Value::Binary { val, .. } => replace_impl(val, args, val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}
@ -174,10 +172,7 @@ fn replace_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
let mut remain = input[left..].to_vec();
replaced.append(&mut remain);
Value::Binary {
val: replaced,
span,
}
Value::binary(replaced, span)
}
#[cfg(test)]

View File

@ -56,47 +56,39 @@ impl Command for BytesReverse {
Example {
description: "Reverse bytes `0x[1F FF AA AA]`",
example: "0x[1F FF AA AA] | bytes reverse",
result: Some(Value::Binary {
val: vec![0xAA, 0xAA, 0xFF, 0x1F],
span: Span::test_data(),
}),
result: Some(Value::binary(
vec![0xAA, 0xAA, 0xFF, 0x1F],
Span::test_data(),
)),
},
Example {
description: "Reverse bytes `0x[FF AA AA]`",
example: "0x[FF AA AA] | bytes reverse",
result: Some(Value::Binary {
val: vec![0xAA, 0xAA, 0xFF],
span: Span::test_data(),
}),
result: Some(Value::binary(vec![0xAA, 0xAA, 0xFF], Span::test_data())),
},
]
}
}
fn reverse(val: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => {
Value::Binary { val, .. } => {
let mut reversed_input = val.to_vec();
reversed_input.reverse();
Value::Binary {
val: reversed_input,
span: *val_span,
}
Value::binary(reversed_input, val_span)
}
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}

View File

@ -84,15 +84,15 @@ impl Command for BytesStartsWith {
Ok(v @ Value::Error { .. }) => return Ok(v.clone().into_pipeline_data()),
// Unsupported data
Ok(other) => {
return Ok(Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
return Ok(Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "string and binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
}
)
.into_pipeline_data());
}
Err(err) => return Err(err.to_owned()),
@ -147,22 +147,20 @@ impl Command for BytesStartsWith {
}
fn starts_with(val: &Value, args: &Arguments, span: Span) -> Value {
let val_span = val.span();
match val {
Value::Binary {
val,
span: val_span,
} => Value::bool(val.starts_with(&args.pattern), *val_span),
Value::Binary { val, .. } => Value::bool(val.starts_with(&args.pattern), val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
other => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
},
),
}
}