git.delta.rocks / jrsonnet / refs/commits / 9b331948fca2

difftreelog

feat(evaluator) Err instead of panic on unknown variable

Лач2020-06-25parent: #012ef80.patch.diff
in: master

3 files changed

modifiedcrates/jsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
52 }))52 }))
53 }53 }
5454
55 pub fn binding(&self, name: &str) -> LazyVal {55 pub fn binding(&self, name: &str) -> Result<LazyVal> {
56 self.0.bindings.get(name).cloned().unwrap_or_else(|| {56 self.0.bindings.get(name).cloned().ok_or_else(|| {
57 panic!("can't find {} in {:?}", name, self);57 create_error::<()>(Error::UnknownVariable(name.to_owned()))
58 .err()
59 .unwrap()
58 })60 })
59 }61 }
60 pub fn into_future(self, ctx: FutureContext) -> Context {62 pub fn into_future(self, ctx: FutureContext) -> Context {
modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
7 TypeMismatch(&'static str, Vec<ValType>, ValType),7 TypeMismatch(&'static str, Vec<ValType>, ValType),
8 NoSuchField(String),8 NoSuchField(String),
9
10 UnknownVariable(String),
911
10 UnknownFunctionParameter(String),12 UnknownFunctionParameter(String),
11 BindingParameterASecondTime(String),13 BindingParameterASecondTime(String),
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
397 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,397 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,
398 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,398 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
399 Var(name) => push(locexpr, "var".to_owned(), || {399 Var(name) => push(locexpr, "var".to_owned(), || {
400 Val::Lazy(context.binding(&name)).unwrap_if_lazy()400 Val::Lazy(context.binding(&name)?).unwrap_if_lazy()
401 })?,401 })?,
402 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {402 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
403 let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;403 let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;