git.delta.rocks / jrsonnet / refs/commits / e46de0a3e23e

difftreelog

fix accept null as std.slice argument/in slicing syntax

Yaroslav Bolyukin2024-11-03parent: #7160d47.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
655 desc: &'static str,655 desc: &'static str,
656 ) -> Result<Option<T>> {656 ) -> Result<Option<T>> {
657 if let Some(value) = expr {657 if let Some(value) = expr {
658 Ok(Some(in_frame(658 Ok(in_frame(
659 loc,659 loc,
660 || format!("slice {desc}"),660 || format!("slice {desc}"),
661 || T::from_untyped(evaluate(ctx.clone(), value)?),661 || <Option<T>>::from_untyped(evaluate(ctx.clone(), value)?),
662 )?))662 )?)
663 } else {663 } else {
664 Ok(None)664 Ok(None)
665 }665 }
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
655 }655 }
656}656}
657
658impl<T> Typed for Option<T>
659where
660 T: Typed,
661{
662 const TYPE: &'static ComplexValType =
663 &ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);
664
665 fn into_untyped(typed: Self) -> Result<Val> {
666 typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))
667 }
668
669 fn from_untyped(untyped: Val) -> Result<Self> {
670 if matches!(untyped, Val::Null) {
671 Ok(None)
672 } else {
673 T::from_untyped(untyped).map(Some)
674 }
675 }
676}
657677
658pub struct NativeFn<D: NativeDesc>(D::Value);678pub struct NativeFn<D: NativeDesc>(D::Value);
659impl<D: NativeDesc> Deref for NativeFn<D> {679impl<D: NativeDesc> Deref for NativeFn<D> {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
48#[builtin]48#[builtin]
49pub fn builtin_slice(49pub fn builtin_slice(
50 indexable: IndexableVal,50 indexable: IndexableVal,
51 index: Option<i32>,51 index: Option<Option<i32>>,
52 end: Option<i32>,52 end: Option<Option<i32>>,
53 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,53 step: Option<Option<BoundedUsize<1, { i32::MAX as usize }>>>,
54) -> Result<Val> {54) -> Result<Val> {
55 indexable.slice(index, end, step).map(Val::from)55 indexable
56 .slice(index.flatten(), end.flatten(), step.flatten())
57 .map(Val::from)
56}58}
5759