nushell/crates/nu-command/src/strings/encode_decode/encode_base64.rs
Dan Davison df94052180
Declare input and output types of commands (#6796)
* Add failing test that list of ints and floats is List<Number>

* Start defining subtype relation

* Make it possible to declare input and output types for commands

- Enforce them in tests

* Declare input and output types of commands

* Add formatted signatures to `help commands` table

* Revert SyntaxShape::Table -> Type::Table change

* Revert unnecessary derive(Hash) on SyntaxShape

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
2022-11-10 10:55:05 +13:00

74 lines
2.0 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)])
.vectorizes_over_list(true)
.named(
"character-set",
SyntaxShape::String,
CHARACTER_SET_DESC,
Some('c'),
)
.rest(
"rest",
SyntaxShape::CellPath,
"optionally base64 encode data by column paths",
)
.output_type(Type::String)
.category(Category::Hash)
}
fn usage(&self) -> &str {
"Base64 encode a value"
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Base64 encode a string with default settings",
example: "echo 'Some Data' | encode base64",
result: Some(Value::string("U29tZSBEYXRh", Span::test_data())),
},
Example {
description: "Base64 encode a string with the binhex character set",
example: "echo '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)
}
}