difftreelog
feat unwrap_type macro
in: master
4 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -205,6 +205,9 @@
[[package]]
name = "jrsonnet-types"
version = "0.3.3"
+dependencies = [
+ "peg",
+]
[[package]]
name = "jsonnet"
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth1use crate::{1use crate::{2 equals,2 equals,3 error::{Error::*, Result},3 error::{Error::*, Result},4 evaluate, parse_args, primitive_equals, push, throw,4 parse_args, primitive_equals, push, throw,5 typed::CheckType,6 with_state, ArrValue, Context, FuncVal, LazyVal, Val,5 with_state, ArrValue, Context, FuncVal, LazyVal, Val,7};6};8use format::{format_arr, format_obj};7use format::{format_arr, format_obj};crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -146,6 +146,8 @@
($ctx: expr, $fn_name: expr, $args: expr, $total_args: expr, [
$($id: expr, $name: ident: $ty: expr $(=>$match: path)?);+ $(;)?
], $handler:block) => {{
+ use $crate::{error::Error::*, throw, evaluate, push, typed::CheckType};
+
let args = $args;
if args.len() > $total_args {
throw!(TooManyArgsFunctionHas($total_args));
crates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -8,6 +8,18 @@
use jrsonnet_types::{ComplexValType, ValType};
use thiserror::Error;
+#[macro_export]
+macro_rules! unwrap_type {
+ ($desc: expr, $value: expr, $typ: expr => $match: path) => {{
+ use $crate::{push, typed::CheckType};
+ push(None, $desc, || Ok($typ.check(&$value)?))?;
+ match $value {
+ $match(v) => v,
+ _ => unreachable!(),
+ }
+ }}
+}
+
#[derive(Debug, Error, Clone)]
pub enum TypeError {
#[error("expected {0}, got {1}")]