1use jrsonnet_evaluator::function::{CallLocation, FuncVal, PreparedFuncVal};2use jrsonnet_evaluator::typed::{ComplexValType, FromUntyped, Typed, ValType};3use jrsonnet_evaluator::{Error, Result, Thunk, Val};45#[derive(Default, Clone)]6pub enum KeyF {7 #[default]8 Identity,9 Prepared(PreparedFuncVal),10 PrepareFailure(Error),11}12impl KeyF {13 pub fn is_identity(&self) -> bool {14 matches!(self, Self::Identity)15 }16 fn new(val: FuncVal) -> Self {17 if val.is_identity() {18 Self::Identity19 } else {20 PreparedFuncVal::new(val, 1, &[]).map_or_else(Self::PrepareFailure, Self::Prepared)21 }22 }23 pub fn eval(&self, val: impl Into<Thunk<Val>>) -> Result<Val> {24 match self {25 KeyF::Identity => val.into().evaluate(),26 KeyF::Prepared(p) => p.call(CallLocation::native(), &[val.into()], &[]),27 KeyF::PrepareFailure(e) => Err(e.clone()),28 }29 }30}3132impl Typed for KeyF {33 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);34}35impl FromUntyped for KeyF {36 fn from_untyped(untyped: Val) -> Result<Self> {37 FuncVal::from_untyped(untyped).map(Self::new)38 }39}