From 90849a067fcaf936c074b929c928f4aa6f5e72d9 Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 18 Dec 2022 03:57:16 +1000 Subject: [PATCH] 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. --- .../src/strings/encode_decode/base64.rs | 2 +- .../src/strings/encode_decode/encode_base64.rs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/strings/encode_decode/base64.rs b/crates/nu-command/src/strings/encode_decode/base64.rs index 95f93a3ea0..e9d5c4f5a5 100644 --- a/crates/nu-command/src/strings/encode_decode/base64.rs +++ b/crates/nu-command/src/strings/encode_decode/base64.rs @@ -161,7 +161,7 @@ fn action( } other => Value::Error { error: ShellError::TypeMismatch( - format!("value is {}, not string", other.get_type()), + format!("string or binary, not {}", other.get_type()), other.span().unwrap_or(command_span), ), }, diff --git a/crates/nu-command/src/strings/encode_decode/encode_base64.rs b/crates/nu-command/src/strings/encode_decode/encode_base64.rs index 542beab3f4..c017cdd2f7 100644 --- a/crates/nu-command/src/strings/encode_decode/encode_base64.rs +++ b/crates/nu-command/src/strings/encode_decode/encode_base64.rs @@ -15,7 +15,10 @@ impl Command for EncodeBase64 { fn signature(&self) -> Signature { Signature::build("encode base64") - .input_output_types(vec![(Type::String, Type::String)]) + .input_output_types(vec![ + (Type::String, Type::String), + (Type::Binary, Type::String), + ]) .vectorizes_over_list(true) .named( "character-set", @@ -33,18 +36,23 @@ impl Command for EncodeBase64 { } fn usage(&self) -> &str { - "Base64 encode a value" + "Encode a string or binary value using Base64" } fn examples(&self) -> Vec { vec![ Example { - description: "Base64 encode a string with default settings", + 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: "Base64 encode a string with the binhex character set", + 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())), },