2023-06-22 21:45:54 +00:00
|
|
|
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
2023-06-23 19:23:08 +00:00
|
|
|
use nu_cmd_base::util;
|
2022-07-09 02:42:31 +00:00
|
|
|
use nu_engine::CallExt;
|
2023-10-28 12:52:31 +00:00
|
|
|
use nu_protocol::record;
|
2022-07-09 02:42:31 +00:00
|
|
|
use nu_protocol::{
|
2023-04-02 16:28:36 +00:00
|
|
|
ast::{Call, CellPath},
|
|
|
|
engine::{Command, EngineState, Stack},
|
2023-10-28 12:52:31 +00:00
|
|
|
Category, Example, PipelineData, Range, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
2022-07-09 02:42:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BytesAt;
|
|
|
|
|
|
|
|
struct Arguments {
|
2023-04-02 16:28:36 +00:00
|
|
|
indexes: Subbytes,
|
2022-10-29 21:29:46 +00:00
|
|
|
cell_paths: Option<Vec<CellPath>>,
|
2022-07-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
|
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-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:28:36 +00:00
|
|
|
impl From<(isize, isize)> for Subbytes {
|
|
|
|
fn from(input: (isize, isize)) -> Self {
|
|
|
|
Self(input.0, input.1)
|
|
|
|
}
|
2022-07-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 16:28:36 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Subbytes(isize, isize);
|
|
|
|
|
2022-07-09 02:42:31 +00:00
|
|
|
impl Command for BytesAt {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"bytes at"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("bytes at")
|
2023-07-23 18:46:53 +00:00
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::Binary, Type::Binary),
|
|
|
|
(
|
|
|
|
Type::List(Box::new(Type::Binary)),
|
|
|
|
Type::List(Box::new(Type::Binary)),
|
|
|
|
),
|
2023-07-24 11:17:30 +00:00
|
|
|
(Type::Table(vec![]), Type::Table(vec![])),
|
2023-07-26 21:13:57 +00:00
|
|
|
(Type::Record(vec![]), Type::Record(vec![])),
|
2023-07-23 18:46:53 +00:00
|
|
|
])
|
2023-04-02 16:28:36 +00:00
|
|
|
.required("range", SyntaxShape::Range, "the range to get bytes")
|
2022-07-09 02:42:31 +00:00
|
|
|
.rest(
|
|
|
|
"rest",
|
|
|
|
SyntaxShape::CellPath,
|
2022-11-10 05:49:11 +00:00
|
|
|
"for a data structure input, get bytes from data at the given cell paths",
|
2022-07-09 02:42:31 +00:00
|
|
|
)
|
|
|
|
.category(Category::Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-04-02 16:28:36 +00:00
|
|
|
"Get bytes defined by a range"
|
2022-07-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["slice"]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2023-04-02 16:28:36 +00:00
|
|
|
let range: Range = call.req(engine_state, stack, 0)?;
|
|
|
|
let indexes = match util::process_range(&range) {
|
|
|
|
Ok(idxs) => idxs.into(),
|
|
|
|
Err(processing_error) => {
|
|
|
|
return Err(processing_error("could not perform subbytes", call.head));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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);
|
2023-04-02 16:28:36 +00:00
|
|
|
let args = Arguments {
|
|
|
|
indexes,
|
2022-10-29 21:29:46 +00:00
|
|
|
cell_paths,
|
2022-07-09 02:42:31 +00:00
|
|
|
};
|
2023-04-02 16:28:36 +00:00
|
|
|
|
|
|
|
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
2022-07-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Get a subbytes `0x[10 01]` from the bytes `0x[33 44 55 10 01 13]`",
|
2023-04-02 16:28:36 +00:00
|
|
|
example: " 0x[33 44 55 10 01 13] | bytes at 3..<4",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_binary(vec![0x10])),
|
2022-07-09 02:42:31 +00:00
|
|
|
},
|
|
|
|
Example {
|
2023-04-02 16:28:36 +00:00
|
|
|
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",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_binary(vec![0x10, 0x01, 0x13])),
|
2022-07-09 02:42:31 +00:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Get the remaining characters from a starting index",
|
2023-07-26 21:13:57 +00:00
|
|
|
example: " { data: 0x[33 44 55 10 01 13] } | bytes at 3.. data",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_record(record! {
|
|
|
|
"data" => Value::test_binary(vec![0x10, 0x01, 0x13]),
|
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-09 02:42:31 +00:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Get the characters from the beginning until ending index",
|
2023-04-02 16:28:36 +00:00
|
|
|
example: " 0x[33 44 55 10 01 13] | bytes at ..<4",
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_binary(vec![0x33, 0x44, 0x55, 0x10])),
|
2022-07-09 02:42:31 +00:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description:
|
|
|
|
"Or the characters from the beginning until ending index inside a table",
|
2023-04-02 16:28:36 +00:00
|
|
|
example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes at 1.. ColB ColC"#,
|
2023-10-28 12:52:31 +00:00
|
|
|
result: Some(Value::test_list(vec![Value::test_record(record! {
|
|
|
|
"ColA" => Value::test_binary(vec![0x11, 0x12, 0x13]),
|
|
|
|
"ColB" => Value::test_binary(vec![0x15, 0x16]),
|
|
|
|
"ColC" => Value::test_binary(vec![0x18, 0x19]),
|
|
|
|
})])),
|
2022-07-09 02:42:31 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:28:36 +00:00
|
|
|
fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
|
|
|
let range = &args.indexes;
|
|
|
|
match input {
|
|
|
|
Value::Binary { val, .. } => {
|
|
|
|
use std::cmp::{self, Ordering};
|
|
|
|
let len = val.len() as isize;
|
2022-07-09 02:42:31 +00:00
|
|
|
|
2023-04-02 16:28:36 +00:00
|
|
|
let start = if range.0 < 0 { range.0 + len } else { range.0 };
|
2022-07-09 02:42:31 +00:00
|
|
|
|
2023-04-02 16:28:36 +00:00
|
|
|
let end = if range.1 < 0 {
|
|
|
|
cmp::max(range.1 + len, 0)
|
|
|
|
} else {
|
|
|
|
range.1
|
|
|
|
};
|
|
|
|
|
|
|
|
if start < len && end >= 0 {
|
|
|
|
match start.cmp(&end) {
|
2023-09-03 14:27:29 +00:00
|
|
|
Ordering::Equal => Value::binary(vec![], head),
|
|
|
|
Ordering::Greater => Value::error(
|
|
|
|
ShellError::TypeMismatch {
|
2023-04-02 16:28:36 +00:00
|
|
|
err_message: "End must be greater than or equal to Start".to_string(),
|
|
|
|
span: head,
|
|
|
|
},
|
2023-09-03 14:27:29 +00:00
|
|
|
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,
|
|
|
|
),
|
2023-04-02 16:28:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-03 14:27:29 +00:00
|
|
|
Value::binary(vec![], head)
|
2023-04-02 16:28:36 +00:00
|
|
|
}
|
2022-07-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 16:28:36 +00:00
|
|
|
Value::Error { .. } => input.clone(),
|
2022-07-09 02:42:31 +00:00
|
|
|
|
2023-09-03 14:27:29 +00:00
|
|
|
other => Value::error(
|
|
|
|
ShellError::UnsupportedInput(
|
2023-04-02 16:28:36 +00:00
|
|
|
"Only binary values are supported".into(),
|
|
|
|
format!("input type: {:?}", other.get_type()),
|
|
|
|
head,
|
|
|
|
// This line requires the Value::Error match above.
|
2023-08-24 20:48:05 +00:00
|
|
|
other.span(),
|
2023-09-03 14:27:29 +00:00
|
|
|
),
|
|
|
|
head,
|
|
|
|
),
|
2022-07-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
}
|