1
0
mirror of https://github.com/nushell/nushell.git synced 2025-07-17 14:55:09 +02:00
Files
nushell/crates/nu-command/src/strings/mod.rs
Andrej Kolčin 0560826414 encode/decode for multiple alphabets ()
Based on the discussion in .


## 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`.
2024-08-23 11:18:51 -05:00

76 lines
2.5 KiB
Rust

mod base;
mod char_;
mod detect_columns;
mod encode_decode;
mod format;
mod guess_width;
mod parse;
mod split;
mod str_;
pub use base::{
DecodeBase32, DecodeBase32Hex, DecodeBase64, DecodeHex, EncodeBase32, EncodeBase32Hex,
EncodeBase64, EncodeHex,
};
pub use char_::Char;
pub use detect_columns::*;
pub use encode_decode::*;
pub use format::*;
pub use parse::*;
pub use split::*;
pub use str_::*;
use nu_engine::CallExt;
use nu_protocol::{
engine::{Call, EngineState, Stack, StateWorkingSet},
ShellError,
};
// For handling the grapheme_cluster related flags on some commands.
// This ensures the error messages are consistent.
pub fn grapheme_flags(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<bool, ShellError> {
let g_flag = call.has_flag(engine_state, stack, "grapheme-clusters")?;
// Check for the other flags and produce errors if they exist.
// Note that Nushell already prevents nonexistent flags from being used with commands,
// so this function can be reused for both the --utf-8-bytes commands and the --code-points commands.
if g_flag && call.has_flag(engine_state, stack, "utf-8-bytes")? {
Err(ShellError::IncompatibleParametersSingle {
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
span: call.head,
})?
}
if g_flag && call.has_flag(engine_state, stack, "code-points")? {
Err(ShellError::IncompatibleParametersSingle {
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
span: call.head,
})?
}
// Grapheme cluster usage is decided by the non-default -g flag
Ok(g_flag)
}
// Const version of grapheme_flags
pub fn grapheme_flags_const(
working_set: &StateWorkingSet,
call: &Call,
) -> Result<bool, ShellError> {
let g_flag = call.has_flag_const(working_set, "grapheme-clusters")?;
if g_flag && call.has_flag_const(working_set, "utf-8-bytes")? {
Err(ShellError::IncompatibleParametersSingle {
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
span: call.head,
})?
}
if g_flag && call.has_flag_const(working_set, "code-points")? {
Err(ShellError::IncompatibleParametersSingle {
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
span: call.head,
})?
}
Ok(g_flag)
}