nushell/crates/nu-command/src/strings/encode_decode/decode_base64.rs
Justin Ma ce6df93d05
Add bits shl and bits shr command (#6202)
* Add `bits shift-left` and `bits shift-right` command

* update bits shift error tips

* some code refactor

* update shift right

* some code refactor for bits shift commands

* rename bits shift commands align with bits operators

* update search term

* Update crates/nu-command/src/bits/not.rs

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>

* Update crates/nu-command/src/bits/shift_left.rs

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>

* Update crates/nu-command/src/bits/shift_right.rs

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>

* ci skip

* change default number-bytes for bits shift

* fix bits not tests

* fix bits tests

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
2022-08-02 15:52:04 -05:00

83 lines
2.3 KiB
Rust

use super::base64::{operate, ActionType, CHARACTER_SET_DESC};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct DecodeBase64;
impl Command for DecodeBase64 {
fn name(&self) -> &str {
"decode base64"
}
fn signature(&self) -> Signature {
Signature::build("decode base64")
.named(
"character-set",
SyntaxShape::String,
CHARACTER_SET_DESC,
Some('c'),
)
.switch(
"binary",
"Output a binary value instead of decoding payload as UTF-8",
Some('b'),
)
.rest(
"rest",
SyntaxShape::CellPath,
"optionally base64 decode data by column paths",
)
.category(Category::Hash)
}
fn usage(&self) -> &str {
"Base64 decode a value"
}
fn extra_usage(&self) -> &str {
r#"Will attempt to decode binary payload as an UTF-8 string by default. Use the `--binary(-b)` argument to force binary output."#
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Base64 decode a value and output as UTF-8 string",
example: "echo 'U29tZSBEYXRh' | decode base64",
result: Some(Value::string("Some Data", Span::test_data())),
},
Example {
description: "Base64 decode a value and output as binary",
example: "echo 'U29tZSBEYXRh' | decode base64 --binary",
result: Some(Value::binary(
[0x53, 0x6f, 0x6d, 0x65, 0x20, 0x44, 0x61, 0x74, 0x61],
Span::test_data(),
)),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(ActionType::Decode, engine_state, stack, call, input)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_examples() {
crate::test_examples(DecodeBase64)
}
}