difftreelog
feat byte array specialization
in: master
2 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth1use crate::function::StaticBuiltin;1use crate::function::StaticBuiltin;2use crate::typed::{Any, PositiveF64, VecVal, M1};2use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};3use crate::{3use crate::{4 builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},4 builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},5 equals,5 equals,449}449}450450451#[jrsonnet_macros::builtin]451#[jrsonnet_macros::builtin]452fn builtin_encode_utf8(str: IStr) -> Result<VecVal> {452fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {453 Ok(VecVal(453 Ok(Bytes(str.bytes().map(|b| b).collect::<Vec<u8>>().into()))454 str.bytes()455 .map(|b| Val::Num(b as f64))456 .collect::<Vec<Val>>(),457 ))458}454}459455460#[jrsonnet_macros::builtin]456#[jrsonnet_macros::builtin]461fn builtin_decode_utf8(arr: Vec<u8>) -> Result<String> {457fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {462 Ok(String::from_utf8(arr).map_err(|_| RuntimeError("bad utf8".into()))?)458 Ok(std::str::from_utf8(&arr.0)459 .map_err(|_| RuntimeError("bad utf8".into()))?460 .into())463}461}464462465#[jrsonnet_macros::builtin]463#[jrsonnet_macros::builtin]485}483}486484487#[jrsonnet_macros::builtin]485#[jrsonnet_macros::builtin]488fn builtin_base64(input: Either![Vec<u8>, IStr]) -> Result<String> {486fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {489 use Either2::*;487 use Either2::*;490 Ok(match input {488 Ok(match input {491 A(a) => base64::encode(a),489 A(a) => base64::encode(a.0),492 B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),490 B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),493 })491 })494}492}495493496#[jrsonnet_macros::builtin]494#[jrsonnet_macros::builtin]497fn builtin_base64_decode_bytes(input: IStr) -> Result<Vec<u8>> {495fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {498 Ok(base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?)496 Ok(Bytes(497 base64::decode(&input.as_bytes())498 .map_err(|_| RuntimeError("bad base64".into()))?499 .into(),500 ))499}501}500502501#[jrsonnet_macros::builtin]503#[jrsonnet_macros::builtin]crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth1use std::convert::{TryFrom, TryInto};1use std::{2 convert::{TryFrom, TryInto},3 rc::Rc,4};253use gcmodule::Cc;6use gcmodule::Cc;4use jrsonnet_interner::IStr;7use jrsonnet_interner::IStr;295 }298 }296}299}300301/// Specialization302pub struct Bytes(pub Rc<[u8]>);303304impl Typed for Bytes {305 const TYPE: &'static ComplexValType =306 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));307}308impl TryFrom<Val> for Bytes {309 type Error = LocError;310311 fn try_from(value: Val) -> Result<Self> {312 match value {313 Val::Arr(ArrValue::Bytes(bytes)) => Ok(Self(bytes)),314 _ => {315 <Self as Typed>::TYPE.check(&value)?;316 match value {317 Val::Arr(a) => {318 let mut out = Vec::with_capacity(a.len());319 for e in a.iter() {320 let r = e?;321 out.push(u8::try_from(r)?);322 }323 Ok(Self(out.into()))324 }325 _ => unreachable!(),326 }327 }328 }329 }330}331impl TryFrom<Bytes> for Val {332 type Error = LocError;333334 fn try_from(value: Bytes) -> Result<Self> {335 Ok(Val::Arr(ArrValue::Bytes(value.0)))336 }337}297338298pub struct M1;339pub struct M1;299impl Typed for M1 {340impl Typed for M1 {