From ced22d8710e301bace7fed3b8fe89d706120182e Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 24 Jan 2021 11:37:06 +0000 Subject: [PATCH] refactor: Option<&ExprLocation> -> &Option<_> --- --- a/crates/jrsonnet-evaluator/src/builtin/mod.rs +++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs @@ -22,7 +22,7 @@ fn std_format(str: IStr, vals: Val) -> Result { push( - &Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)), + Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)), || format!("std.format of {}", str), || { Ok(match vals { @@ -34,7 +34,7 @@ ) } -type Builtin = fn(context: Context, loc: &Option, args: &ArgsDesc) -> Result; +type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result; type BuiltinsType = HashMap, Builtin>; @@ -78,7 +78,7 @@ }; } -fn builtin_length(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_length(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "length", args, 1, [ 0, x: ty!((string | object | array)); ], { @@ -96,7 +96,7 @@ }) } -fn builtin_type(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_type(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "type", args, 1, [ 0, x: ty!(any); ], { @@ -106,7 +106,7 @@ fn builtin_make_array( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "makeArray", args, 2, [ @@ -126,7 +126,7 @@ fn builtin_codepoint( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "codepoint", args, 1, [ @@ -138,7 +138,7 @@ fn builtin_object_fields_ex( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "objectFieldsEx", args, 2, [ @@ -157,7 +157,7 @@ fn builtin_object_has_ex( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "objectHasEx", args, 3, [ @@ -175,7 +175,7 @@ } // faster -fn builtin_slice(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "slice", args, 4, [ 0, indexable: ty!((string | array)); 1, index: ty!((number | null)); @@ -216,7 +216,7 @@ // faster fn builtin_primitive_equals( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "primitiveEquals", args, 2, [ @@ -228,7 +228,7 @@ } // faster -fn builtin_equals(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "equals", args, 2, [ 0, a: ty!(any); 1, b: ty!(any); @@ -237,7 +237,7 @@ }) } -fn builtin_modulo(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_modulo(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "modulo", args, 2, [ 0, a: ty!(number) => Val::Num; 1, b: ty!(number) => Val::Num; @@ -246,7 +246,7 @@ }) } -fn builtin_mod(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_mod(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "mod", args, 2, [ 0, a: ty!((number | string)); 1, b: ty!(any); @@ -259,7 +259,7 @@ }) } -fn builtin_floor(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_floor(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "floor", args, 1, [ 0, x: ty!(number) => Val::Num; ], { @@ -267,7 +267,7 @@ }) } -fn builtin_log(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "log", args, 1, [ 0, n: ty!(number) => Val::Num; ], { @@ -275,7 +275,7 @@ }) } -fn builtin_pow(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_pow(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "pow", args, 2, [ 0, x: ty!(number) => Val::Num; 1, n: ty!(number) => Val::Num; @@ -284,7 +284,7 @@ }) } -fn builtin_ext_var(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "extVar", args, 1, [ 0, x: ty!(string) => Val::Str; ], { @@ -292,7 +292,7 @@ }) } -fn builtin_native(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_native(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "native", args, 1, [ 0, x: ty!(string) => Val::Str; ], { @@ -300,7 +300,7 @@ }) } -fn builtin_filter(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_filter(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "filter", args, 2, [ 0, func: ty!(function) => Val::Func; 1, arr: ty!(array) => Val::Arr; @@ -318,7 +318,7 @@ }) } -fn builtin_foldl(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "foldl", args, 3, [ 0, func: ty!(function) => Val::Func; 1, arr: ty!(array) => Val::Arr; @@ -332,7 +332,7 @@ }) } -fn builtin_foldr(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_foldr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "foldr", args, 3, [ 0, func: ty!(function) => Val::Func; 1, arr: ty!(array) => Val::Arr; @@ -349,7 +349,7 @@ #[allow(non_snake_case)] fn builtin_sort_impl( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "sort", args, 2, [ @@ -364,7 +364,7 @@ } // faster -fn builtin_format(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "format", args, 2, [ 0, str: ty!(string) => Val::Str; 1, vals: ty!(any) @@ -373,7 +373,7 @@ }) } -fn builtin_range(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_range(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "range", args, 2, [ 0, from: ty!(number) => Val::Num; 1, to: ty!(number) => Val::Num; @@ -386,7 +386,7 @@ }) } -fn builtin_char(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_char(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "char", args, 1, [ 0, n: ty!(number) => Val::Num; ], { @@ -400,7 +400,7 @@ fn builtin_encode_utf8( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "encodeUTF8", args, 1, [ @@ -410,7 +410,7 @@ }) } -fn builtin_md5(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_md5(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "md5", args, 1, [ 0, str: ty!(string) => Val::Str; ], { @@ -418,7 +418,7 @@ }) } -fn builtin_trace(context: Context, loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_trace(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "trace", args, 2, [ 0, str: ty!(string) => Val::Str; 1, rest: ty!(any); @@ -435,7 +435,7 @@ }) } -fn builtin_base64(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "base64", args, 1, [ 0, input: ty!((string | (Array))); ], { @@ -453,7 +453,7 @@ }) } -fn builtin_join(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "join", args, 2, [ 0, sep: ty!((string | array)); 1, arr: ty!(array) => Val::Arr; @@ -513,7 +513,7 @@ // faster fn builtin_escape_string_json( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "escapeStringJson", args, 1, [ @@ -526,7 +526,7 @@ // faster fn builtin_manifest_json_ex( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "manifestJsonEx", args, 2, [ @@ -541,7 +541,7 @@ } // faster -fn builtin_reverse(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "reverse", args, 1, [ 0, value: ty!(array) => Val::Arr; ], { @@ -549,7 +549,7 @@ }) } -fn builtin_id(context: Context, _loc: &Option, args: &ArgsDesc) -> Result { +fn builtin_id(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result { parse_args!(context, "id", args, 1, [ 0, v: ty!(any); ], { @@ -560,7 +560,7 @@ // faster fn builtin_str_replace( context: Context, - _loc: &Option, + _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { parse_args!(context, "strReplace", args, 3, [ @@ -583,10 +583,9 @@ }) } -#[allow(clippy::cognitive_complexity)] pub fn call_builtin( context: Context, - loc: &Option, + loc: Option<&ExprLocation>, name: &str, args: &ArgsDesc, ) -> Result { --- a/crates/jrsonnet-evaluator/src/evaluate.rs +++ b/crates/jrsonnet-evaluator/src/evaluate.rs @@ -385,7 +385,7 @@ context: Context, value: &LocExpr, args: &ArgsDesc, - loc: &Option, + loc: Option<&ExprLocation>, tailstrict: bool, ) -> Result { let value = evaluate(context.clone(), value)?; @@ -430,7 +430,7 @@ BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?, UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?, Var(name) => push( - loc, + loc.as_ref(), || format!("variable <{}>", name), || Ok(context.binding(name.clone())?.evaluate()?), )?, @@ -448,7 +448,7 @@ (Val::Obj(v), Val::Str(s)) => { let sn = s.clone(); push( - loc, + loc.as_ref(), || format!("field <{}> access", sn), || { if let Some(v) = v.get(s.clone())? { @@ -540,14 +540,14 @@ &evaluate(context.clone(), s)?, &Val::Obj(evaluate_object(context, t)?), )?, - Apply(value, args, tailstrict) => evaluate_apply(context, value, args, loc, *tailstrict)?, + Apply(value, args, tailstrict) => evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?, Function(params, body) => { evaluate_method(context, "anonymous".into(), params.clone(), body.clone()) } Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))), AssertExpr(AssertStmt(value, msg), returned) => { let assertion_result = push( - &value.1, + value.1.as_ref(), || "assertion condition".to_owned(), || { evaluate(context.clone(), value)? @@ -558,7 +558,7 @@ evaluate(context, returned)? } else { push( - &value.1, + value.1.as_ref(), || "assertion failure".to_owned(), || { if let Some(msg) = msg { @@ -571,7 +571,7 @@ } } ErrorStmt(e) => push( - loc, + loc.as_ref(), || "error statement".to_owned(), || { throw!(RuntimeError( @@ -585,7 +585,7 @@ cond_else, } => { if push( - loc, + loc.as_ref(), || "if condition".to_owned(), || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"), )? { @@ -605,7 +605,7 @@ let import_location = Rc::make_mut(&mut tmp); import_location.pop(); push( - loc, + loc.as_ref(), || format!("import {:?}", path), || with_state(|s| s.import_file(import_location, path)), )? --- a/crates/jrsonnet-evaluator/src/function.rs +++ b/crates/jrsonnet-evaluator/src/function.rs @@ -160,7 +160,7 @@ throw!(IntrinsicArgumentReorderingIsNotSupportedYet); } } - let $name = push(&None, || format!("evaluating argument"), || { + let $name = push(None, || format!("evaluating argument"), || { let value = evaluate($ctx.clone(), &$name.1)?; $ty.check(&value)?; Ok(value) --- a/crates/jrsonnet-evaluator/src/lib.rs +++ b/crates/jrsonnet-evaluator/src/lib.rs @@ -128,7 +128,7 @@ EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap())) } pub(crate) fn push( - e: &Option, + e: Option<&ExprLocation>, frame_desc: impl FnOnce() -> String, f: impl FnOnce() -> Result, ) -> Result { --- a/crates/jrsonnet-evaluator/src/typed.rs +++ b/crates/jrsonnet-evaluator/src/typed.rs @@ -78,7 +78,7 @@ } fn push_type( - location: &Option, + location: Option<&ExprLocation>, error_reason: impl Fn() -> String, path: impl Fn() -> ValuePathItem, item: impl Fn() -> Result<()>, @@ -162,7 +162,7 @@ Val::Arr(a) => { for (i, item) in a.iter().enumerate() { push_type( - &None, + None, || format!("array index {}", i), || ValuePathItem::Index(i as u64), || Ok(elem_type.check(&item.clone()?)?), @@ -176,7 +176,7 @@ Val::Arr(a) => { for (i, item) in a.iter().enumerate() { push_type( - &None, + None, || format!("array index {}", i), || ValuePathItem::Index(i as u64), || Ok(elem_type.check(&item.clone()?)?), @@ -191,7 +191,7 @@ for (k, v) in elems.iter() { if let Some(got_v) = obj.get((*k).into())? { push_type( - &None, + None, || format!("property {}", k), || ValuePathItem::Field((*k).into()), || v.check(&got_v), --- a/crates/jrsonnet-evaluator/src/val.rs +++ b/crates/jrsonnet-evaluator/src/val.rs @@ -102,7 +102,7 @@ pub fn evaluate( &self, call_ctx: Context, - loc: &Option, + loc: Option<&ExprLocation>, args: &ArgsDesc, tailstrict: bool, ) -> Result { -- gitstuff