mirror of
https://github.com/nushell/nushell.git
synced 2025-01-10 16:28:50 +01:00
Fix encode/decode todo's (#13683)
Mistakes have been made. I forgot about a bunch of `todo`s in the helper functions. So, this PR replaces them with proper errors. It also adds tests for parse-time evaluation, because one `todo` I missed was in a `run_const` function.
This commit is contained in:
parent
525eac1afd
commit
3f332bef35
@ -53,7 +53,7 @@ impl Command for DecodeHex {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
todo!()
|
||||
super::decode(data_encoding::HEXLOWER_PERMISSIVE, call.span(), input)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,14 +54,17 @@ fn get_string(input: PipelineData, call_span: Span) -> Result<(String, Span), Sh
|
||||
match val {
|
||||
Value::String { val, .. } => Ok((val, span)),
|
||||
|
||||
_ => {
|
||||
todo!("Invalid type")
|
||||
}
|
||||
value => Err(ShellError::TypeMismatch {
|
||||
err_message: "binary or string".to_owned(),
|
||||
span: call_span,
|
||||
}),
|
||||
}
|
||||
}
|
||||
PipelineData::ListStream(..) => {
|
||||
todo!()
|
||||
}
|
||||
PipelineData::ListStream(list, ..) => Err(ShellError::PipelineMismatch {
|
||||
exp_input_type: "binary or string".to_owned(),
|
||||
dst_span: call_span,
|
||||
src_span: list.span(),
|
||||
}),
|
||||
PipelineData::ByteStream(stream, ..) => {
|
||||
let span = stream.span();
|
||||
Ok((stream.into_string()?, span))
|
||||
@ -80,20 +83,23 @@ fn get_binary(input: PipelineData, call_span: Span) -> Result<(Vec<u8>, Span), S
|
||||
Value::Binary { val, .. } => Ok((val, span)),
|
||||
Value::String { val, .. } => Ok((val.into_bytes(), span)),
|
||||
|
||||
_ => {
|
||||
todo!("Invalid type")
|
||||
}
|
||||
value => Err(ShellError::TypeMismatch {
|
||||
err_message: "binary or string".to_owned(),
|
||||
span: call_span,
|
||||
}),
|
||||
}
|
||||
}
|
||||
PipelineData::ListStream(..) => {
|
||||
todo!()
|
||||
}
|
||||
PipelineData::ListStream(list, ..) => Err(ShellError::PipelineMismatch {
|
||||
exp_input_type: "binary or string".to_owned(),
|
||||
dst_span: call_span,
|
||||
src_span: list.span(),
|
||||
}),
|
||||
PipelineData::ByteStream(stream, ..) => {
|
||||
let span = stream.span();
|
||||
Ok((stream.into_bytes()?, span))
|
||||
}
|
||||
PipelineData::Empty => {
|
||||
todo!("Can't have empty data");
|
||||
}
|
||||
PipelineData::Empty => Err(ShellError::PipelineEmpty {
|
||||
dst_span: call_span,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
@ -2,16 +2,14 @@ use nu_test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn canonical() {
|
||||
for value in super::random_bytes() {
|
||||
let outcome = nu!("{} | encode base32 | decode base32 | to nuon", value);
|
||||
assert_eq!(outcome.out, value);
|
||||
super::test_canonical("base32");
|
||||
super::test_canonical("base32 --nopad");
|
||||
}
|
||||
|
||||
let outcome = nu!(
|
||||
"{} | encode base32 --nopad | decode base32 --nopad | to nuon",
|
||||
value
|
||||
);
|
||||
assert_eq!(outcome.out, value);
|
||||
}
|
||||
#[test]
|
||||
fn const_() {
|
||||
super::test_const("base32");
|
||||
super::test_const("base32 --nopad");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -2,16 +2,14 @@ use nu_test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn canonical() {
|
||||
for value in super::random_bytes() {
|
||||
let outcome = nu!("{} | encode base32hex | decode base32hex | to nuon", value);
|
||||
assert_eq!(outcome.out, value);
|
||||
super::test_canonical("base32hex");
|
||||
super::test_canonical("base32hex --nopad");
|
||||
}
|
||||
|
||||
let outcome = nu!(
|
||||
"{} | encode base32hex --nopad | decode base32hex --nopad | to nuon",
|
||||
value
|
||||
);
|
||||
assert_eq!(outcome.out, value);
|
||||
}
|
||||
#[test]
|
||||
fn const_() {
|
||||
super::test_const("base32hex");
|
||||
super::test_const("base32hex --nopad");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -2,31 +2,18 @@ use nu_test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn canonical() {
|
||||
for value in super::random_bytes() {
|
||||
let outcome = nu!(
|
||||
"{} | encode new-base64 | decode new-base64 | to nuon",
|
||||
value
|
||||
);
|
||||
assert_eq!(outcome.out, value);
|
||||
super::test_canonical("new-base64");
|
||||
super::test_canonical("new-base64 --url");
|
||||
super::test_canonical("new-base64 --nopad");
|
||||
super::test_canonical("new-base64 --url --nopad");
|
||||
}
|
||||
|
||||
let outcome = nu!(
|
||||
"{} | encode new-base64 --url | decode new-base64 --url | to nuon",
|
||||
value
|
||||
);
|
||||
assert_eq!(outcome.out, value);
|
||||
|
||||
let outcome = nu!(
|
||||
"{} | encode new-base64 --nopad | decode new-base64 --nopad | to nuon",
|
||||
value
|
||||
);
|
||||
assert_eq!(outcome.out, value);
|
||||
|
||||
let outcome = nu!(
|
||||
"{} | encode new-base64 --url --nopad | decode new-base64 --url --nopad | to nuon",
|
||||
value
|
||||
);
|
||||
assert_eq!(outcome.out, value);
|
||||
}
|
||||
#[test]
|
||||
fn const_() {
|
||||
super::test_const("new-base64");
|
||||
super::test_const("new-base64 --url");
|
||||
super::test_const("new-base64 --nopad");
|
||||
super::test_const("new-base64 --url --nopad");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -2,10 +2,12 @@ use nu_test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn canonical() {
|
||||
for value in super::random_bytes() {
|
||||
let outcome = nu!("{} | encode hex | decode hex | to nuon", value);
|
||||
assert_eq!(outcome.out, value);
|
||||
}
|
||||
super::test_canonical("hex");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_() {
|
||||
super::test_const("hex");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -2,13 +2,15 @@ use data_encoding::HEXUPPER;
|
||||
use rand::prelude::*;
|
||||
use rand_chacha::ChaCha8Rng;
|
||||
|
||||
use nu_test_support::nu;
|
||||
|
||||
mod base32;
|
||||
mod base32hex;
|
||||
mod base64;
|
||||
mod hex;
|
||||
|
||||
/// Generate a few random binaries.
|
||||
pub fn random_bytes() -> Vec<String> {
|
||||
fn random_bytes() -> Vec<String> {
|
||||
const NUM: usize = 32;
|
||||
let mut rng = ChaCha8Rng::seed_from_u64(4);
|
||||
|
||||
@ -17,8 +19,26 @@ pub fn random_bytes() -> Vec<String> {
|
||||
let length = rng.gen_range(0..512);
|
||||
let mut bytes = vec![0u8; length];
|
||||
rng.fill_bytes(&mut bytes);
|
||||
let hex_bytes = HEXUPPER.encode(&bytes);
|
||||
format!("0x[{}]", hex_bytes)
|
||||
HEXUPPER.encode(&bytes)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn test_canonical(cmd: &str) {
|
||||
for value in random_bytes() {
|
||||
let outcome = nu!("0x[{}] | encode {1} | decode {1} | to nuon", value, cmd);
|
||||
let nuon_value = format!("0x[{value}]");
|
||||
assert_eq!(outcome.out, nuon_value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_const(cmd: &str) {
|
||||
for value in random_bytes() {
|
||||
let outcome = nu!(
|
||||
r#"const out = (0x[{}] | encode {1} | decode {1} | encode hex); $out"#,
|
||||
value,
|
||||
cmd
|
||||
);
|
||||
assert_eq!(outcome.out, value);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user