difftreelog
refactor unify throw & throw_runtime
in: master
13 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -266,14 +266,13 @@
#[macro_export]
macro_rules! throw {
- ($e: expr) => {
- return Err($e.into())
+ ($w:ident$(::$i:ident)*$(($($tt:tt)*))?) => {
+ return Err($w$(::$i)*$(($($tt)*))?.into())
};
-}
-
-#[macro_export]
-macro_rules! throw_runtime {
- ($($tt:tt)*) => {
- return Err($crate::error::Error::RuntimeError(format!($($tt)*).into()).into())
+ ($l:literal) => {
+ return Err($crate::error::Error::RuntimeError($l.into()).into())
+ };
+ ($l:literal, $($tt:tt)*) => {
+ return Err($crate::error::Error::RuntimeError(format!($l, $($tt)*).into()).into())
};
}
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -32,7 +32,7 @@
Destruct::Array { start, rest, end } => {
use jrsonnet_parser::DestructRest;
- use crate::{throw_runtime, val::ArrValue};
+ use crate::{throw, val::ArrValue};
#[derive(Trace)]
struct DataThunk {
@@ -47,14 +47,14 @@
let v = self.parent.evaluate(s)?;
let arr = match v {
Val::Arr(a) => a,
- _ => throw_runtime!("expected array"),
+ _ => throw!("expected array"),
};
if !self.has_rest {
if arr.len() != self.min_len {
- throw_runtime!("expected {} elements, got {}", self.min_len, arr.len())
+ throw!("expected {} elements, got {}", self.min_len, arr.len())
}
} else if arr.len() < self.min_len {
- throw_runtime!(
+ throw!(
"expected at least {} elements, but array was only {}",
self.min_len,
arr.len()
@@ -163,7 +163,7 @@
}
#[cfg(feature = "exp-destruct")]
Destruct::Object { fields, rest } => {
- use crate::{obj::ObjValue, throw_runtime};
+ use crate::{obj::ObjValue, throw};
#[derive(Trace)]
struct DataThunk {
@@ -178,17 +178,17 @@
let v = self.parent.evaluate(s)?;
let obj = match v {
Val::Obj(o) => o,
- _ => throw_runtime!("expected object"),
+ _ => throw!("expected object"),
};
for field in &self.field_names {
if !obj.has_field_ex(field.clone(), true) {
- throw_runtime!("missing field: {}", field);
+ throw!("missing field: {}", field);
}
}
if !self.has_rest {
let len = obj.len();
if len != self.field_names.len() {
- throw_runtime!("too many fields, and rest not found");
+ throw!("too many fields, and rest not found");
}
}
Ok(obj)
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -150,13 +150,13 @@
(Num(v1), BitXor, Num(v2)) => Num(f64::from((*v1 as i32) ^ (*v2 as i32))),
(Num(v1), Lhs, Num(v2)) => {
if *v2 < 0.0 {
- throw!(RuntimeError("shift by negative exponent".into()))
+ throw!("shift by negative exponent")
}
Num(f64::from((*v1 as i32) << (*v2 as i32)))
}
(Num(v1), Rhs, Num(v2)) => {
if *v2 < 0.0 {
- throw!(RuntimeError("shift by negative exponent".into()))
+ throw!("shift by negative exponent")
}
Num(f64::from((*v1 as i32) >> (*v2 as i32)))
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -72,7 +72,7 @@
}
Self::Object(out)
}
- Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+ Val::Func(_) => throw!("tried to manifest function"),
})
}
}
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -591,9 +591,7 @@
),
Val::Str(s) => {
if s.chars().count() != 1 {
- throw!(RuntimeError(
- format!("%c expected 1 char string, got {}", s.chars().count()).into(),
- ));
+ throw!("%c expected 1 char string, got {}", s.chars().count(),);
}
tmp_out.push_str(&s);
}
crates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
@@ -341,7 +341,7 @@
}
}
}
- Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+ Val::Func(_) => throw!("tried to manifest function"),
}
Ok(())
}
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -6,7 +6,7 @@
use jrsonnet_types::{ComplexValType, ValType};
use crate::{
- error::{Error::*, Result},
+ error::Result,
function::{FuncDesc, FuncVal},
throw,
typed::CheckType,
@@ -41,13 +41,10 @@
Val::Num(n) => {
#[allow(clippy::float_cmp)]
if n.trunc() != n {
- throw!(RuntimeError(
- format!(
- "cannot convert number with fractional part to {}",
- stringify!($ty)
- )
- .into()
- ))
+ throw!(
+ "cannot convert number with fractional part to {}",
+ stringify!($ty)
+ )
}
Ok(n as Self)
}
@@ -99,13 +96,10 @@
Val::Num(n) => {
#[allow(clippy::float_cmp)]
if n.trunc() != n {
- throw!(RuntimeError(
- format!(
- "cannot convert number with fractional part to {}",
- stringify!($ty)
- )
- .into()
- ))
+ throw!(
+ "cannot convert number with fractional part to {}",
+ stringify!($ty)
+ )
}
Ok(Self(n as $ty))
}
@@ -167,7 +161,7 @@
fn into_untyped(value: Self, _: State) -> Result<Val> {
if value > u32::MAX as Self {
- throw!(RuntimeError("number is too large".into()))
+ throw!("number is too large")
}
Ok(Val::Num(value as f64))
}
@@ -178,9 +172,7 @@
Val::Num(n) => {
#[allow(clippy::float_cmp)]
if n.trunc() != n {
- throw!(RuntimeError(
- "cannot convert number with fractional part to usize".into()
- ))
+ throw!("cannot convert number with fractional part to usize")
}
Ok(n as Self)
}
@@ -440,7 +432,7 @@
<Self as Typed>::TYPE.check(s, &value)?;
match value {
Val::Func(FuncVal::Normal(desc)) => Ok(desc),
- Val::Func(_) => throw!(RuntimeError("expected normal function, not builtin".into())),
+ Val::Func(_) => throw!("expected normal function, not builtin"),
_ => unreachable!(),
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -629,7 +629,7 @@
if num.is_finite() {
Ok(Self::Num(num))
} else {
- throw!(RuntimeError("overflow".into()))
+ throw!("overflow")
}
}
@@ -843,14 +843,14 @@
(Val::Null, Val::Null) => true,
(Val::Str(a), Val::Str(b)) => a == b,
(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,
- (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(
- "primitiveEquals operates on primitive types, got array".into(),
- )),
- (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(
- "primitiveEquals operates on primitive types, got object".into(),
- )),
+ (Val::Arr(_), Val::Arr(_)) => {
+ throw!("primitiveEquals operates on primitive types, got array")
+ }
+ (Val::Obj(_), Val::Obj(_)) => {
+ throw!("primitiveEquals operates on primitive types, got object")
+ }
(a, b) if is_function_like(a) && is_function_like(b) => {
- throw!(RuntimeError("cannot test equality of functions".into()))
+ throw!("cannot test equality of functions")
}
(_, _) => false,
})
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth1use jrsonnet_evaluator::{2 error::Result,3 function::{builtin, FuncVal},4 throw_runtime,5 typed::{Any, BoundedUsize, Typed, VecVal},6 val::{equals, ArrValue, IndexableVal},7 IStr, State, Val,8};9use jrsonnet_gcmodule::Cc;1011#[builtin]12pub fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {13 let mut out = Vec::with_capacity(sz);14 for i in 0..sz {15 out.push(func.evaluate_simple(s.clone(), &(i as f64,))?);16 }17 Ok(VecVal(Cc::new(out)))18}1920#[builtin]21pub fn builtin_slice(22 indexable: IndexableVal,23 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,24 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,25 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,26) -> Result<Any> {27 indexable.slice(index, end, step).map(Val::from).map(Any)28}2930#[builtin]31pub fn builtin_map(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {32 arr.map(s.clone(), |val| {33 func.evaluate_simple(s.clone(), &(Any(val),))34 })35}3637#[builtin]38pub fn builtin_flatmap(s: State, func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {39 match arr {40 IndexableVal::Str(str) => {41 let mut out = String::new();42 for c in str.chars() {43 match func.evaluate_simple(s.clone(), &(c.to_string(),))? {44 Val::Str(o) => out.push_str(&o),45 Val::Null => continue,46 _ => throw_runtime!("in std.join all items should be strings"),47 };48 }49 Ok(IndexableVal::Str(out.into()))50 }51 IndexableVal::Arr(a) => {52 let mut out = Vec::new();53 for el in a.iter(s.clone()) {54 let el = el?;55 match func.evaluate_simple(s.clone(), &(Any(el),))? {56 Val::Arr(o) => {57 for oe in o.iter(s.clone()) {58 out.push(oe?);59 }60 }61 Val::Null => continue,62 _ => throw_runtime!("in std.join all items should be arrays"),63 };64 }65 Ok(IndexableVal::Arr(out.into()))66 }67 }68}6970#[builtin]71pub fn builtin_filter(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {72 arr.filter(s.clone(), |val| {73 bool::from_untyped(74 func.evaluate_simple(s.clone(), &(Any(val.clone()),))?,75 s.clone(),76 )77 })78}7980#[builtin]81pub fn builtin_foldl(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {82 let mut acc = init.0;83 for i in arr.iter(s.clone()) {84 acc = func.evaluate_simple(s.clone(), &(Any(acc), Any(i?)))?;85 }86 Ok(Any(acc))87}8889#[builtin]90pub fn builtin_foldr(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {91 let mut acc = init.0;92 for i in arr.iter(s.clone()).rev() {93 acc = func.evaluate_simple(s.clone(), &(Any(i?), Any(acc)))?;94 }95 Ok(Any(acc))96}9798#[builtin]99pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {100 if to < from {101 return Ok(ArrValue::new_eager());102 }103 Ok(ArrValue::new_range(from, to))104}105106#[builtin]107pub fn builtin_join(s: State, sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {108 Ok(match sep {109 IndexableVal::Arr(joiner_items) => {110 let mut out = Vec::new();111112 let mut first = true;113 for item in arr.iter(s.clone()) {114 let item = item?.clone();115 if let Val::Arr(items) = item {116 if !first {117 out.reserve(joiner_items.len());118 // TODO: extend119 for item in joiner_items.iter(s.clone()) {120 out.push(item?);121 }122 }123 first = false;124 out.reserve(items.len());125 for item in items.iter(s.clone()) {126 out.push(item?);127 }128 } else if matches!(item, Val::Null) {129 continue;130 } else {131 throw_runtime!("in std.join all items should be arrays");132 }133 }134135 IndexableVal::Arr(out.into())136 }137 IndexableVal::Str(sep) => {138 let mut out = String::new();139140 let mut first = true;141 for item in arr.iter(s) {142 let item = item?.clone();143 if let Val::Str(item) = item {144 if !first {145 out += &sep;146 }147 first = false;148 out += &item;149 } else if matches!(item, Val::Null) {150 continue;151 } else {152 throw_runtime!("in std.join all items should be strings");153 }154 }155156 IndexableVal::Str(out.into())157 }158 })159}160161#[builtin]162pub fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {163 Ok(value.reversed())164}165166#[builtin]167pub fn builtin_any(s: State, arr: ArrValue) -> Result<bool> {168 for v in arr.iter(s.clone()) {169 let v = bool::from_untyped(v?, s.clone())?;170 if v {171 return Ok(true);172 }173 }174 Ok(false)175}176177#[builtin]178pub fn builtin_all(s: State, arr: ArrValue) -> Result<bool> {179 for v in arr.iter(s.clone()) {180 let v = bool::from_untyped(v?, s.clone())?;181 if !v {182 return Ok(false);183 }184 }185 Ok(true)186}187188#[builtin]189pub fn builtin_member(s: State, arr: IndexableVal, x: Any) -> Result<bool> {190 match arr {191 IndexableVal::Str(str) => {192 let x: IStr = IStr::from_untyped(x.0, s)?;193 Ok(!x.is_empty() && str.contains(&*x))194 }195 IndexableVal::Arr(a) => {196 for item in a.iter(s.clone()) {197 let item = item?;198 if equals(s.clone(), &item, &x.0)? {199 return Ok(true);200 }201 }202 Ok(false)203 }204 }205}206207#[builtin]208pub fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {209 let mut count = 0;210 for item in &arr {211 if equals(s.clone(), &item.0, &v.0)? {212 count += 1;213 }214 }215 Ok(count)216}crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -8,7 +8,7 @@
error::{Error::*, Result},
function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},
gc::{GcHashMap, TraceBox},
- tb, throw_runtime,
+ tb, throw,
trace::PathResolver,
typed::{Any, Either, Either2, Either4, VecVal, M1},
val::{equals, ArrValue},
@@ -500,7 +500,7 @@
true
}
}
- _ => throw_runtime!("both arguments should be of the same type"),
+ _ => throw!("both arguments should be of the same type"),
})
}
@@ -534,7 +534,7 @@
true
}
}
- _ => throw_runtime!("both arguments should be of the same type"),
+ _ => throw!("both arguments should be of the same type"),
})
}
crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -1,7 +1,7 @@
use jrsonnet_evaluator::{
error::Result,
function::{builtin, FuncVal},
- throw_runtime,
+ throw,
typed::Any,
val::ArrValue,
State, Val,
@@ -41,9 +41,9 @@
(Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,
(Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}
(Val::Str(_) | Val::Num(_), _) => {
- throw_runtime!("sort elements should have the same types")
+ throw!("sort elements should have the same types")
}
- _ => throw_runtime!("sort key should either be a string or a number"),
+ _ => throw!("sort key should either be a string or a number"),
}
}
Ok(sort_type)
tests/tests/common.rsdiffbeforeafterboth--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -1,7 +1,7 @@
use jrsonnet_evaluator::{
error::Result,
function::{builtin, FuncVal},
- throw_runtime, ObjValueBuilder, State, Thunk, Val,
+ throw, ObjValueBuilder, State, Thunk, Val,
};
use jrsonnet_stdlib::StateExt;
@@ -11,7 +11,7 @@
let a = &$a;
let b = &$b;
if a != b {
- ::jrsonnet_evaluator::throw_runtime!("assertion failed: a != b\na={:#?}\nb={:#?}", a, b)
+ ::jrsonnet_evaluator::throw!("assertion failed: a != b\na={:#?}\nb={:#?}", a, b)
}
}};
}
@@ -20,7 +20,7 @@
macro_rules! ensure {
($v:expr $(,)?) => {
if !$v {
- ::jrsonnet_evaluator::throw_runtime!("assertion failed: {}", stringify!($v))
+ ::jrsonnet_evaluator::throw!("assertion failed: {}", stringify!($v))
}
};
}
@@ -29,7 +29,7 @@
macro_rules! ensure_val_eq {
($s:expr, $a:expr, $b:expr) => {{
if !::jrsonnet_evaluator::val::equals($s.clone(), &$a.clone(), &$b.clone())? {
- ::jrsonnet_evaluator::throw_runtime!(
+ ::jrsonnet_evaluator::throw!(
"assertion failed: a != b\na={:#?}\nb={:#?}",
$a.to_json(
$s.clone(),
@@ -52,7 +52,7 @@
fn assert_throw(s: State, lazy: Thunk<Val>, message: String) -> Result<bool> {
match lazy.evaluate(s) {
Ok(_) => {
- throw_runtime!("expected argument to throw on evaluation, but it returned instead")
+ throw!("expected argument to throw on evaluation, but it returned instead")
}
Err(e) => {
let error = format!("{}", e.error());
tests/tests/sanity.rsdiffbeforeafterboth--- a/tests/tests/sanity.rs
+++ b/tests/tests/sanity.rs
@@ -1,4 +1,4 @@
-use jrsonnet_evaluator::{error::Result, throw_runtime, State, Val};
+use jrsonnet_evaluator::{error::Result, throw, State, Val};
use jrsonnet_stdlib::StateExt;
mod common;
@@ -23,7 +23,7 @@
{
let e = match s.evaluate_snippet("snip".to_owned(), "assert 1 == 2: 'fail'; null") {
- Ok(_) => throw_runtime!("assertion should fail"),
+ Ok(_) => throw!("assertion should fail"),
Err(e) => e,
};
let e = s.stringify_err(&e);
@@ -31,7 +31,7 @@
}
{
let e = match s.evaluate_snippet("snip".to_owned(), "std.assertEqual(1, 2)") {
- Ok(_) => throw_runtime!("assertion should fail"),
+ Ok(_) => throw!("assertion should fail"),
Err(e) => e,
};
let e = s.stringify_err(&e);