difftreelog
feat feature-gate regex
in: master
3 files changed
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -30,9 +30,7 @@
# Bigint type
exp-bigint = ["jrsonnet-evaluator/exp-bigint", "jrsonnet-cli/exp-bigint"]
# std.regex and co.
-exp-regex = [
- "jrsonnet-stdlib/exp-regex",
-]
+exp-regex = ["jrsonnet-cli/exp-regex"]
# obj?.field, obj?.['field']
exp-null-coaelse = [
"jrsonnet-evaluator/exp-null-coaelse",
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth1use std::{collections::BTreeMap, marker::PhantomData, ops::Deref};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9 arr::{ArrValue, BytesArray},10 bail,11 function::{native::NativeDesc, FuncDesc, FuncVal},12 typed::CheckType,13 val::{IndexableVal, ThunkMapper},14 ObjValue, ObjValueBuilder, Result, Thunk, Val,15};1617#[derive(Trace)]18struct FromUntyped<K: Trace>(PhantomData<fn() -> K>);19impl<K> ThunkMapper<Val> for FromUntyped<K>20where21 K: Typed + Trace,22{23 type Output = K;2425 fn map(self, from: Val) -> Result<Self::Output> {26 K::from_untyped(from)27 }28}29impl<K: Trace> Default for FromUntyped<K> {30 fn default() -> Self {31 Self(PhantomData)32 }33}3435pub trait TypedObj: Typed {36 fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;37 fn parse(obj: &ObjValue) -> Result<Self>;38 fn into_object(self) -> Result<ObjValue> {39 let mut builder = ObjValueBuilder::new();40 self.serialize(&mut builder)?;41 Ok(builder.build())42 }43}4445pub trait Typed: Sized {46 const TYPE: &'static ComplexValType;47 fn into_untyped(typed: Self) -> Result<Val>;48 fn into_lazy_untyped(typed: Self) -> Thunk<Val> {49 Thunk::from(Self::into_untyped(typed))50 }51 fn from_untyped(untyped: Val) -> Result<Self>;52 fn from_lazy_untyped(lazy: Thunk<Val>) -> Result<Self> {53 Self::from_untyped(lazy.evaluate()?)54 }5556 // Whatever caller should use `into_lazy_untyped` instead of `into_untyped`57 fn provides_lazy() -> bool {58 false59 }6061 // Whatever caller should use `from_lazy_untyped` instead of `from_untyped` when possible62 fn wants_lazy() -> bool {63 false64 }6566 /// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result67 /// This method returns identity in impl Typed for Result, and should not be overriden68 #[doc(hidden)]69 fn into_result(typed: Self) -> Result<Val> {70 let value = Self::into_untyped(typed)?;71 Ok(value)72 }73}7475impl<T> Typed for Thunk<T>76where77 T: Typed + Trace + Clone,78{79 const TYPE: &'static ComplexValType = &ComplexValType::Lazy(T::TYPE);8081 fn into_untyped(typed: Self) -> Result<Val> {82 T::into_untyped(typed.evaluate()?)83 }8485 fn from_untyped(untyped: Val) -> Result<Self> {86 Self::from_lazy_untyped(Thunk::evaluated(untyped))87 }8889 fn provides_lazy() -> bool {90 true91 }9293 fn into_lazy_untyped(inner: Self) -> Thunk<Val> {94 #[derive(Trace)]95 struct IntoUntyped<K: Trace>(PhantomData<fn() -> K>);96 impl<K> ThunkMapper<K> for IntoUntyped<K>97 where98 K: Typed + Trace,99 {100 type Output = Val;101102 fn map(self, from: K) -> Result<Self::Output> {103 K::into_untyped(from)104 }105 }106 impl<K: Trace> Default for IntoUntyped<K> {107 fn default() -> Self {108 Self(PhantomData)109 }110 }111 inner.map(<IntoUntyped<T>>::default())112 }113114 fn wants_lazy() -> bool {115 true116 }117118 fn from_lazy_untyped(inner: Thunk<Val>) -> Result<Self> {119 Ok(inner.map(<FromUntyped<T>>::default()))120 }121}122123const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS + 1)) - 1) as f64;124125macro_rules! impl_int {126 ($($ty:ty)*) => {$(127 impl Typed for $ty {128 const TYPE: &'static ComplexValType =129 &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));130 fn from_untyped(value: Val) -> Result<Self> {131 <Self as Typed>::TYPE.check(&value)?;132 match value {133 Val::Num(n) => {134 #[allow(clippy::float_cmp)]135 if n.trunc() != n {136 bail!(137 "cannot convert number with fractional part to {}",138 stringify!($ty)139 )140 }141 Ok(n as Self)142 }143 _ => unreachable!(),144 }145 }146 fn into_untyped(value: Self) -> Result<Val> {147 Ok(Val::Num(value as f64))148 }149 }150 )*};151}152153impl_int!(i8 u8 i16 u16 i32 u32);154155macro_rules! impl_bounded_int {156 ($($name:ident = $ty:ty)*) => {$(157 #[derive(Clone, Copy)]158 pub struct $name<const MIN: $ty, const MAX: $ty>($ty);159 impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {160 pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {161 if value >= MIN && value <= MAX {162 Some(Self(value))163 } else {164 None165 }166 }167 pub const fn value(self) -> $ty {168 self.0169 }170 }171 impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {172 type Target = $ty;173 fn deref(&self) -> &Self::Target {174 &self.0175 }176 }177178 impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {179 const TYPE: &'static ComplexValType =180 &ComplexValType::BoundedNumber(181 Some(MIN as f64),182 Some(MAX as f64),183 );184185 fn from_untyped(value: Val) -> Result<Self> {186 <Self as Typed>::TYPE.check(&value)?;187 match value {188 Val::Num(n) => {189 #[allow(clippy::float_cmp)]190 if n.trunc() != n {191 bail!(192 "cannot convert number with fractional part to {}",193 stringify!($ty)194 )195 }196 Ok(Self(n as $ty))197 }198 _ => unreachable!(),199 }200 }201202 fn into_untyped(value: Self) -> Result<Val> {203 Ok(Val::Num(value.0 as f64))204 }205 }206 )*};207}208209impl_bounded_int!(210 BoundedI8 = i8211 BoundedI16 = i16212 BoundedI32 = i32213 BoundedI64 = i64214 BoundedUsize = usize215);216217impl Typed for f64 {218 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);219220 fn into_untyped(value: Self) -> Result<Val> {221 Ok(Val::Num(value))222 }223224 fn from_untyped(value: Val) -> Result<Self> {225 <Self as Typed>::TYPE.check(&value)?;226 match value {227 Val::Num(n) => Ok(n),228 _ => unreachable!(),229 }230 }231}232233pub struct PositiveF64(pub f64);234impl Typed for PositiveF64 {235 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);236237 fn into_untyped(value: Self) -> Result<Val> {238 Ok(Val::Num(value.0))239 }240241 fn from_untyped(value: Val) -> Result<Self> {242 <Self as Typed>::TYPE.check(&value)?;243 match value {244 Val::Num(n) => Ok(Self(n)),245 _ => unreachable!(),246 }247 }248}249impl Typed for usize {250 const TYPE: &'static ComplexValType =251 &ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));252253 fn into_untyped(value: Self) -> Result<Val> {254 if value > MAX_SAFE_INTEGER as Self {255 bail!("number is too large")256 }257 Ok(Val::Num(value as f64))258 }259260 fn from_untyped(value: Val) -> Result<Self> {261 <Self as Typed>::TYPE.check(&value)?;262 match value {263 Val::Num(n) => {264 #[allow(clippy::float_cmp)]265 if n.trunc() != n {266 bail!("cannot convert number with fractional part to usize")267 }268 Ok(n as Self)269 }270 _ => unreachable!(),271 }272 }273}274275impl Typed for IStr {276 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);277278 fn into_untyped(value: Self) -> Result<Val> {279 Ok(Val::string(value))280 }281282 fn from_untyped(value: Val) -> Result<Self> {283 <Self as Typed>::TYPE.check(&value)?;284 match value {285 Val::Str(s) => Ok(s.into_flat()),286 _ => unreachable!(),287 }288 }289}290291impl Typed for String {292 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);293294 fn into_untyped(value: Self) -> Result<Val> {295 Ok(Val::string(value))296 }297298 fn from_untyped(value: Val) -> Result<Self> {299 <Self as Typed>::TYPE.check(&value)?;300 match value {301 Val::Str(s) => Ok(s.to_string()),302 _ => unreachable!(),303 }304 }305}306307impl Typed for StrValue {308 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);309310 fn into_untyped(value: Self) -> Result<Val> {311 Ok(Val::Str(value))312 }313314 fn from_untyped(value: Val) -> Result<Self> {315 <Self as Typed>::TYPE.check(&value)?;316 match value {317 Val::Str(s) => Ok(s),318 _ => unreachable!(),319 }320 }321}322323impl Typed for char {324 const TYPE: &'static ComplexValType = &ComplexValType::Char;325326 fn into_untyped(value: Self) -> Result<Val> {327 Ok(Val::string(value))328 }329330 fn from_untyped(value: Val) -> Result<Self> {331 <Self as Typed>::TYPE.check(&value)?;332 match value {333 Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),334 _ => unreachable!(),335 }336 }337}338339impl<T> Typed for Vec<T>340where341 T: Typed,342{343 const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);344345 fn into_untyped(value: Self) -> Result<Val> {346 Ok(Val::Arr(347 value348 .into_iter()349 .map(T::into_untyped)350 .collect::<Result<ArrValue>>()?,351 ))352 }353354 fn from_untyped(value: Val) -> Result<Self> {355 let Val::Arr(a) = value else {356 <Self as Typed>::TYPE.check(&value)?;357 unreachable!("typecheck should fail")358 };359 a.iter()360 .map(|r| r.and_then(T::from_untyped))361 .collect::<Result<Vec<T>>>()362 }363}364365impl<K: Typed + Ord, V: Typed> Typed for BTreeMap<K, V> {366 const TYPE: &'static ComplexValType = &ComplexValType::AttrsOf(V::TYPE);367368 fn into_untyped(typed: Self) -> Result<Val> {369 let mut out = ObjValueBuilder::with_capacity(typed.len());370 for (k, v) in typed {371 let Some(key) = K::into_untyped(k)?.as_str() else {372 bail!("map key should serialize to string");373 };374 let value = V::into_untyped(v)?;375 out.field(key).value(value);376 }377 Ok(Val::Obj(out.build()))378 }379380 fn from_untyped(value: Val) -> Result<Self> {381 Self::TYPE.check(&value)?;382 let obj = value.as_obj().expect("typecheck should fail");383384 let mut out = BTreeMap::new();385 if V::wants_lazy() {386 for key in obj.fields_ex(387 false,388 #[cfg(feature = "exp-preserve-order")]389 false,390 ) {391 let value = obj.get_lazy(key.clone()).expect("field exists");392 let value = V::from_lazy_untyped(value)?;393 let key = K::from_untyped(Val::Str(key.into()))?;394 let _ = out.insert(key, value);395 }396 } else {397 for (key, value) in obj.iter(398 #[cfg(feature = "exp-preserve-order")]399 false,400 ) {401 let key = K::from_untyped(Val::Str(key.into()))?;402 let value = V::from_untyped(value?)?;403 let _ = out.insert(key, value);404 }405 }406 Ok(out)407 }408}409410impl Typed for Val {411 const TYPE: &'static ComplexValType = &ComplexValType::Any;412413 fn into_untyped(typed: Self) -> Result<Val> {414 Ok(typed)415 }416 fn from_untyped(untyped: Val) -> Result<Self> {417 Ok(untyped)418 }419}420421// Hack422#[doc(hidden)]423impl<T> Typed for Result<T>424where425 T: Typed,426{427 const TYPE: &'static ComplexValType = &ComplexValType::Any;428429 fn into_untyped(_typed: Self) -> Result<Val> {430 panic!("do not use this conversion")431 }432433 fn from_untyped(_untyped: Val) -> Result<Self> {434 panic!("do not use this conversion")435 }436437 fn into_result(typed: Self) -> Result<Val> {438 typed.map(T::into_untyped)?439 }440}441442/// Specialization443impl Typed for IBytes {444 const TYPE: &'static ComplexValType =445 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));446447 fn into_untyped(value: Self) -> Result<Val> {448 Ok(Val::Arr(ArrValue::bytes(value)))449 }450451 fn from_untyped(value: Val) -> Result<Self> {452 match &value {453 Val::Arr(a) => {454 if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {455 return Ok(bytes.0.as_slice().into());456 };457 <Self as Typed>::TYPE.check(&value)?;458 // Any::downcast_ref::<ByteArray>(&a);459 let mut out = Vec::with_capacity(a.len());460 for e in a.iter() {461 let r = e?;462 out.push(u8::from_untyped(r)?);463 }464 Ok(out.as_slice().into())465 }466 _ => {467 <Self as Typed>::TYPE.check(&value)?;468 unreachable!()469 }470 }471 }472}473474pub struct M1;475impl Typed for M1 {476 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));477478 fn into_untyped(_: Self) -> Result<Val> {479 Ok(Val::Num(-1.0))480 }481482 fn from_untyped(value: Val) -> Result<Self> {483 <Self as Typed>::TYPE.check(&value)?;484 Ok(Self)485 }486}487488macro_rules! decl_either {489 ($($name: ident, $($id: ident)*);*) => {$(490 #[derive(Clone)]491 pub enum $name<$($id),*> {492 $($id($id)),*493 }494 impl<$($id),*> Typed for $name<$($id),*>495 where496 $($id: Typed,)*497 {498 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);499500 fn into_untyped(value: Self) -> Result<Val> {501 match value {$(502 $name::$id(v) => $id::into_untyped(v)503 ),*}504 }505506 fn from_untyped(value: Val) -> Result<Self> {507 $(508 if $id::TYPE.check(&value).is_ok() {509 $id::from_untyped(value).map(Self::$id)510 } else511 )* {512 <Self as Typed>::TYPE.check(&value)?;513 unreachable!()514 }515 }516 }517 )*}518}519decl_either!(520 Either1, A;521 Either2, A B;522 Either3, A B C;523 Either4, A B C D;524 Either5, A B C D E;525 Either6, A B C D E F;526 Either7, A B C D E F G527);528#[macro_export]529macro_rules! Either {530 ($a:ty) => {$crate::typed::Either1<$a>};531 ($a:ty, $b:ty) => {$crate::typed::Either2<$a, $b>};532 ($a:ty, $b:ty, $c:ty) => {$crate::typed::Either3<$a, $b, $c>};533 ($a:ty, $b:ty, $c:ty, $d:ty) => {$crate::typed::Either4<$a, $b, $c, $d>};534 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {$crate::typed::Either5<$a, $b, $c, $d, $e>};535 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {$crate::typed::Either6<$a, $b, $c, $d, $e, $f>};536 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {$crate::typed::Either7<$a, $b, $c, $d, $e, $f, $g>};537}538pub use Either;539540pub type MyType = Either![u32, f64, String];541542impl Typed for ArrValue {543 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);544545 fn into_untyped(value: Self) -> Result<Val> {546 Ok(Val::Arr(value))547 }548549 fn from_untyped(value: Val) -> Result<Self> {550 <Self as Typed>::TYPE.check(&value)?;551 match value {552 Val::Arr(a) => Ok(a),553 _ => unreachable!(),554 }555 }556}557558impl Typed for FuncVal {559 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);560561 fn into_untyped(value: Self) -> Result<Val> {562 Ok(Val::Func(value))563 }564565 fn from_untyped(value: Val) -> Result<Self> {566 <Self as Typed>::TYPE.check(&value)?;567 match value {568 Val::Func(a) => Ok(a),569 _ => unreachable!(),570 }571 }572}573574impl Typed for Cc<FuncDesc> {575 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);576577 fn into_untyped(value: Self) -> Result<Val> {578 Ok(Val::Func(FuncVal::Normal(value)))579 }580581 fn from_untyped(value: Val) -> Result<Self> {582 <Self as Typed>::TYPE.check(&value)?;583 match value {584 Val::Func(FuncVal::Normal(desc)) => Ok(desc),585 Val::Func(_) => bail!("expected normal function, not builtin"),586 _ => unreachable!(),587 }588 }589}590591impl Typed for ObjValue {592 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);593594 fn into_untyped(value: Self) -> Result<Val> {595 Ok(Val::Obj(value))596 }597598 fn from_untyped(value: Val) -> Result<Self> {599 <Self as Typed>::TYPE.check(&value)?;600 match value {601 Val::Obj(a) => Ok(a),602 _ => unreachable!(),603 }604 }605}606607impl Typed for bool {608 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);609610 fn into_untyped(value: Self) -> Result<Val> {611 Ok(Val::Bool(value))612 }613614 fn from_untyped(value: Val) -> Result<Self> {615 <Self as Typed>::TYPE.check(&value)?;616 match value {617 Val::Bool(a) => Ok(a),618 _ => unreachable!(),619 }620 }621}622impl Typed for IndexableVal {623 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[624 &ComplexValType::Simple(ValType::Arr),625 &ComplexValType::Simple(ValType::Str),626 ]);627628 fn into_untyped(value: Self) -> Result<Val> {629 match value {630 IndexableVal::Str(s) => Ok(Val::string(s)),631 IndexableVal::Arr(a) => Ok(Val::Arr(a)),632 }633 }634635 fn from_untyped(value: Val) -> Result<Self> {636 <Self as Typed>::TYPE.check(&value)?;637 value.into_indexable()638 }639}640641pub struct Null;642impl Typed for Null {643 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);644645 fn into_untyped(_: Self) -> Result<Val> {646 Ok(Val::Null)647 }648649 fn from_untyped(value: Val) -> Result<Self> {650 <Self as Typed>::TYPE.check(&value)?;651 Ok(Self)652 }653}654655pub struct NativeFn<D: NativeDesc>(D::Value);656impl<D: NativeDesc> Deref for NativeFn<D> {657 type Target = D::Value;658659 fn deref(&self) -> &Self::Target {660 &self.0661 }662}663impl<D: NativeDesc> Typed for NativeFn<D> {664 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);665666 fn into_untyped(_typed: Self) -> Result<Val> {667 bail!("can only convert functions from jsonnet to native")668 }669670 fn from_untyped(untyped: Val) -> Result<Self> {671 Ok(Self(672 untyped673 .as_func()674 .expect("shape is checked")675 .into_native::<D>(),676 ))677 }678}crates/jrsonnet-stdlib/src/regex.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/regex.rs
+++ b/crates/jrsonnet-stdlib/src/regex.rs
@@ -45,7 +45,7 @@
let mut named_captures = ObjValueBuilder::with_capacity(regex.capture_names().len());
let Some(captured) = regex.captures(&str) else {
- return Ok(Val::Null)
+ return Ok(Val::Null);
};
for ele in captured.iter().skip(1) {
@@ -62,15 +62,14 @@
.flat_map(|(i, v)| Some((i, v?)))
{
let capture = captures[i].clone();
- named_captures.member(name.into()).value(capture)?;
+ named_captures.field(name).try_value(capture)?;
}
- out.member("string".into())
- .value_unchecked(Val::Str(captured.get(0).unwrap().as_str().into()));
- out.member("captures".into())
- .value_unchecked(Val::Arr(captures.into()));
- out.member("namedCaptures".into())
- .value_unchecked(Val::Obj(named_captures.build()));
+ out.field("string")
+ .value(Val::Str(captured.get(0).unwrap().as_str().into()));
+ out.field("captures").value(Val::Arr(captures.into()));
+ out.field("namedCaptures")
+ .value(Val::Obj(named_captures.build()));
Ok(Val::Obj(out.build()))
}