difftreelog
build use non-deprecated base64 api
in: master
1 file changed
crates/jrsonnet-stdlib/src/encoding.rsdiffbeforeafterboth1use base64::{engine::general_purpose::STANDARD, Engine};1use jrsonnet_evaluator::{2use jrsonnet_evaluator::{2 error::{ErrorKind::RuntimeError, Result},3 error::{ErrorKind::RuntimeError, Result},3 function::builtin,4 function::builtin,21pub fn builtin_base64(input: Either![IStr, IBytes]) -> String {22pub fn builtin_base64(input: Either![IStr, IBytes]) -> String {22 use Either2::*;23 use Either2::*;23 match input {24 match input {24 A(l) => base64::encode(l.as_bytes()),25 A(l) => STANDARD.encode(l.as_bytes()),25 B(a) => base64::encode(a.as_slice()),26 B(a) => STANDARD.encode(a.as_slice()),26 }27 }27}28}282929#[builtin]30#[builtin]30pub fn builtin_base64_decode_bytes(str: IStr) -> Result<IBytes> {31pub fn builtin_base64_decode_bytes(str: IStr) -> Result<IBytes> {31 Ok(base64::decode(str.as_bytes())32 Ok(STANDARD33 .decode(str.as_bytes())32 .map_err(|_| RuntimeError("bad base64".into()))?34 .map_err(|e| RuntimeError(format!("invalid base64: {e}").into()))?33 .as_slice()35 .as_slice()34 .into())36 .into())35}37}363837#[builtin]39#[builtin]38pub fn builtin_base64_decode(str: IStr) -> Result<String> {40pub fn builtin_base64_decode(str: IStr) -> Result<String> {39 let bytes = base64::decode(str.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;41 let bytes = STANDARD42 .decode(str.as_bytes())43 .map_err(|e| RuntimeError(format!("invalid base64: {e}").into()))?;40 Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)44 Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)41}45}4246