git.delta.rocks / jrsonnet / refs/commits / 0111266c91b4

difftreelog

source

crates/jrsonnet-stdlib/src/keyf.rs1.1 KiBsourcehistory
1use jrsonnet_evaluator::function::{CallLocation, FuncVal, PreparedFuncVal};2use jrsonnet_evaluator::typed::{ComplexValType, 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	fn from_untyped(untyped: Val) -> Result<Self> {35		FuncVal::from_untyped(untyped).map(Self::new)36	}3738	fn into_untyped(_typed: Self) -> Result<Val> {39		unreachable!("unused, todo: port split of Typed trait from #193")40	}41}