mirror of
https://github.com/nushell/nushell.git
synced 2025-05-02 09:04:30 +02:00
# Description I need a command that will transform hex string into bytes and into other direction. I've implemented `decode hex` command and `encode hex` command. (Based on `encode base64` and `decode base64` commands # User-Facing Changes ``` > '010203' | decode hex 0x[01 02 03] ``` and ``` > 0x[01 02 0a] | encode hex '01020A' ``` --------- Co-authored-by: whiteand <andrewbeletskiy@gmail.com>
61 lines
1.5 KiB
Rust
61 lines
1.5 KiB
Rust
use super::hex::{operate, ActionType};
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct EncodeHex;
|
|
|
|
impl Command for EncodeHex {
|
|
fn name(&self) -> &str {
|
|
"encode hex"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("encode hex")
|
|
.input_output_types(vec![(Type::Binary, Type::String)])
|
|
.vectorizes_over_list(true)
|
|
.rest(
|
|
"rest",
|
|
SyntaxShape::CellPath,
|
|
"For a data structure input, encode data at the given cell paths",
|
|
)
|
|
.output_type(Type::String)
|
|
.category(Category::Formats)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Encode a binary value using hex."
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Encode binary data",
|
|
example: "0x[09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0] | encode hex",
|
|
result: Some(Value::test_string("09F911029D74E35BD84156C5635688C0")),
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
operate(ActionType::Encode, engine_state, stack, call, input)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
crate::test_examples(EncodeHex)
|
|
}
|
|
}
|