nushell/crates/nu-command/src/strings/encode_decode/encode_base64.rs
Leon 90849a067f
Fix encode base64 type signature and examples (#7515)
# Description

See title.

# User-Facing Changes

See title.

# 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-17 11:57:16 -06:00

82 lines
2.4 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, Type, Value,
};
#[derive(Clone)]
pub struct EncodeBase64;
impl Command for EncodeBase64 {
fn name(&self) -> &str {
"encode base64"
}
fn signature(&self) -> Signature {
Signature::build("encode base64")
.input_output_types(vec![
(Type::String, Type::String),
(Type::Binary, Type::String),
])
.vectorizes_over_list(true)
.named(
"character-set",
SyntaxShape::String,
CHARACTER_SET_DESC,
Some('c'),
)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, encode data at the given cell paths",
)
.output_type(Type::String)
.category(Category::Hash)
}
fn usage(&self) -> &str {
"Encode a string or binary value using Base64"
}
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 base64",
result: Some(Value::string("CfkRAp1041vYQVbFY1aIwA==", Span::test_data())),
},
Example {
description: "Encode a string with default settings",
example: "'Some Data' | encode base64",
result: Some(Value::string("U29tZSBEYXRh", Span::test_data())),
},
Example {
description: "Encode a string with the binhex character set",
example: "'Some Data' | encode base64 --character-set binhex",
result: Some(Value::string(r#"7epXB5"%A@4J"#, Span::test_data())),
},
]
}
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(EncodeBase64)
}
}