Finish Base64

This commit is contained in:
Andrej Kolchin 2024-07-23 21:23:20 +03:00
parent 1f062c3b90
commit 7a5365d7f4

View File

@ -1,5 +1,34 @@
use data_encoding::Encoding;
use nu_engine::command_prelude::*; use nu_engine::command_prelude::*;
fn get_encoding_from_flags(url: bool, nopad: bool) -> Encoding {
match (url, nopad) {
(false, false) => data_encoding::BASE64,
(false, true) => data_encoding::BASE64_NOPAD,
(true, false) => data_encoding::BASE64URL,
(true, true) => data_encoding::BASE64URL_NOPAD,
}
}
fn get_encoding(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<Encoding, ShellError> {
let url = call.has_flag(engine_state, stack, "url")?;
let nopad = call.has_flag(engine_state, stack, "nopad")?;
Ok(get_encoding_from_flags(url, nopad))
}
fn get_encoding_const(working_set: &StateWorkingSet, call: &Call) -> Result<Encoding, ShellError> {
let url = call.has_flag_const(working_set, "url")?;
let nopad = call.has_flag_const(working_set, "nopad")?;
Ok(get_encoding_from_flags(url, nopad))
}
#[derive(Clone)] #[derive(Clone)]
pub struct DecodeBase64; pub struct DecodeBase64;
@ -12,12 +41,13 @@ impl Command for DecodeBase64 {
Signature::build("decode base64") Signature::build("decode base64")
.input_output_types(vec![(Type::String, Type::Binary)]) .input_output_types(vec![(Type::String, Type::Binary)])
.allow_variants_without_examples(true) .allow_variants_without_examples(true)
.required("encoding", SyntaxShape::String, "encoding to use") .switch("url", "Decode the URL-safe Base64 version.", None)
.switch("nopad", "Reject padding.", None)
.category(Category::Formats) .category(Category::Formats)
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Decode a value." "TODO"
} }
fn extra_usage(&self) -> &str { fn extra_usage(&self) -> &str {
@ -39,7 +69,8 @@ impl Command for DecodeBase64 {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
todo!() let encoding = get_encoding(engine_state, stack, call)?;
super::decode(encoding, call.head, input)
} }
fn run_const( fn run_const(
@ -48,7 +79,8 @@ impl Command for DecodeBase64 {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
todo!() let encoding = get_encoding_const(working_set, call)?;
super::decode(encoding, call.head, input)
} }
} }
@ -72,7 +104,7 @@ impl Command for EncodeBase64 {
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Encode a value." "TODO"
} }
fn extra_usage(&self) -> &str { fn extra_usage(&self) -> &str {
@ -94,7 +126,8 @@ impl Command for EncodeBase64 {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
todo!() let encoding = get_encoding(engine_state, stack, call)?;
super::encode(encoding, call.head, input)
} }
fn run_const( fn run_const(
@ -103,7 +136,8 @@ impl Command for EncodeBase64 {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
todo!() let encoding = get_encoding_const(working_set, call)?;
super::encode(encoding, call.head, input)
} }
} }