encode/decode for multiple alphabets (#13428)

Based on the discussion in #13419.


## Description

Reworks the `decode`/`encode` commands by adding/changing the following
bases:

- `base32`
- `base32hex`
- `hex`
- `new-base64`

The `hex` base is compatible with the previous version of `hex` out of
the box (it only adds more flags). `base64` isn't, so the PR adds a new
version and deprecates the old one.

All commands have `string -> binary` signature for decoding and `string
| binary -> string` signature for encoding. A few `base64` encodings,
which are not a part of the
[RFC4648](https://datatracker.ietf.org/doc/html/rfc4648#section-6), have
been dropped.


## Example usage

```Nushell
~/fork/nushell> "string" | encode base32 | decode base32 | decode
string
```

```Nushell
~/fork/nushell> "ORSXG5A=" | decode base32
# `decode` always returns a binary value
Length: 4 (0x4) bytes | printable whitespace ascii_other non_ascii
00000000:   74 65 73 74                                          test
```


## User-Facing Changes

- New commands: `encode/decode base32/base32hex`.
- `encode hex` gets a `--lower` flag.
- `encode/decode base64` deprecated in favor of `encode/decode
new-base64`.
This commit is contained in:
Andrej Kolčin
2024-08-23 19:18:51 +03:00
committed by GitHub
parent 39b0f3bdda
commit 0560826414
23 changed files with 1122 additions and 210 deletions

View File

@ -1,10 +1,11 @@
use super::base64::{operate, ActionType, Base64CommandArguments, CHARACTER_SET_DESC};
use nu_engine::command_prelude::*;
use nu_protocol::{report_warning_new, ParseWarning};
#[derive(Clone)]
pub struct DecodeBase64;
pub struct DecodeBase64Old;
impl Command for DecodeBase64 {
impl Command for DecodeBase64Old {
fn name(&self) -> &str {
"decode base64"
}
@ -77,6 +78,16 @@ impl Command for DecodeBase64 {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
report_warning_new(
engine_state,
&ParseWarning::DeprecatedWarning {
old_command: "decode base64".into(),
new_suggestion: "the new `decode new-base64` version".into(),
span: call.head,
url: "`help decode new-base64`".into(),
},
);
let character_set: Option<Spanned<String>> =
call.get_flag(engine_state, stack, "character-set")?;
let binary = call.has_flag(engine_state, stack, "binary")?;
@ -114,6 +125,6 @@ mod tests {
#[test]
fn test_examples() {
crate::test_examples(DecodeBase64)
crate::test_examples(DecodeBase64Old)
}
}

View File

@ -1,10 +1,11 @@
use super::base64::{operate, ActionType, Base64CommandArguments, CHARACTER_SET_DESC};
use nu_engine::command_prelude::*;
use nu_protocol::{report_warning_new, ParseWarning};
#[derive(Clone)]
pub struct EncodeBase64;
pub struct EncodeBase64Old;
impl Command for EncodeBase64 {
impl Command for EncodeBase64Old {
fn name(&self) -> &str {
"encode base64"
}
@ -81,6 +82,16 @@ impl Command for EncodeBase64 {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
report_warning_new(
engine_state,
&ParseWarning::DeprecatedWarning {
old_command: "encode base64".into(),
new_suggestion: "the new `encode new-base64` version".into(),
span: call.head,
url: "`help encode new-base64`".into(),
},
);
let character_set: Option<Spanned<String>> =
call.get_flag(engine_state, stack, "character-set")?;
let binary = call.has_flag(engine_state, stack, "binary")?;
@ -118,6 +129,6 @@ mod tests {
#[test]
fn test_examples() {
crate::test_examples(EncodeBase64)
crate::test_examples(EncodeBase64Old)
}
}

View File

@ -6,6 +6,6 @@ mod encode_base64;
mod encoding;
pub use self::decode::Decode;
pub use self::decode_base64::DecodeBase64;
pub use self::decode_base64::DecodeBase64Old;
pub use self::encode::Encode;
pub use self::encode_base64::EncodeBase64;
pub use self::encode_base64::EncodeBase64Old;