git.delta.rocks / jrsonnet / refs/commits / 78d82dfdf2a1

difftreelog

feat derive(Typed) for struct

Yaroslav Bolyukin2022-04-04parent: #d710b4f.patch.diff
in: master

9 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -70,8 +70,9 @@
 
 [[package]]
 name = "clap"
-version = "3.0.0-beta.2"
-source = "git+https://github.com/clap-rs/clap?rev=f0c5ea5e1503de5c8e74d8c047a799cf51498e83#f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+version = "3.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "71c47df61d9e16dc010b55dba1952a57d8c215dbb533fd13cdd13369aac73b1c"
 dependencies = [
  "atty",
  "bitflags",
@@ -82,27 +83,28 @@
  "strsim",
  "termcolor",
  "textwrap",
- "vec_map",
 ]
 
 [[package]]
+name = "clap_complete"
+version = "3.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df6f3613c0a3cddfd78b41b10203eb322cb29b600cbdf808a7d3db95691b8e25"
+dependencies = [
+ "clap",
+]
+
+[[package]]
 name = "clap_derive"
-version = "3.0.0-beta.2"
-source = "git+https://github.com/clap-rs/clap?rev=f0c5ea5e1503de5c8e74d8c047a799cf51498e83#f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
+version = "3.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
 dependencies = [
  "heck",
  "proc-macro-error",
  "proc-macro2",
  "quote",
  "syn",
-]
-
-[[package]]
-name = "clap_generate"
-version = "3.0.0-beta.2"
-source = "git+https://github.com/clap-rs/clap?rev=f0c5ea5e1503de5c8e74d8c047a799cf51498e83#f0c5ea5e1503de5c8e74d8c047a799cf51498e83"
-dependencies = [
- "clap",
 ]
 
 [[package]]
@@ -148,12 +150,9 @@
 
 [[package]]
 name = "heck"
-version = "0.3.3"
+version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
-dependencies = [
- "unicode-segmentation",
-]
+checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
 
 [[package]]
 name = "hermit-abi"
@@ -185,7 +184,7 @@
 version = "0.4.2"
 dependencies = [
  "clap",
- "clap_generate",
+ "clap_complete",
  "gcmodule",
  "jrsonnet-cli",
  "jrsonnet-evaluator",
@@ -214,6 +213,7 @@
  "bincode",
  "gcmodule",
  "jrsonnet-interner",
+ "jrsonnet-macros",
  "jrsonnet-parser",
  "jrsonnet-stdlib",
  "jrsonnet-types",
@@ -236,6 +236,15 @@
 ]
 
 [[package]]
+name = "jrsonnet-macros"
+version = "0.4.2"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
 name = "jrsonnet-parser"
 version = "0.4.2"
 dependencies = [
@@ -301,6 +310,12 @@
 checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
 
 [[package]]
+name = "memchr"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
+
+[[package]]
 name = "mimalloc-sys"
 version = "0.1.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -321,9 +336,12 @@
 
 [[package]]
 name = "os_str_bytes"
-version = "3.1.0"
+version = "6.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d"
+checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
+dependencies = [
+ "memchr",
+]
 
 [[package]]
 name = "parking_lot"
@@ -357,9 +375,9 @@
 
 [[package]]
 name = "peg"
-version = "0.7.0"
+version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "07c0b841ea54f523f7aa556956fbd293bcbe06f2e67d2eb732b7278aaf1d166a"
+checksum = "af728fe826811af3b38c37e93de6d104485953ea373d656eebae53d6987fcd2c"
 dependencies = [
  "peg-macros",
  "peg-runtime",
@@ -367,9 +385,9 @@
 
 [[package]]
 name = "peg-macros"
-version = "0.7.0"
+version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b5aa52829b8decbef693af90202711348ab001456803ba2a98eb4ec8fb70844c"
+checksum = "4536be147b770b824895cbad934fccce8e49f14b4c4946eaa46a6e4a12fcdc16"
 dependencies = [
  "peg-runtime",
  "proc-macro2",
@@ -378,9 +396,9 @@
 
 [[package]]
 name = "peg-runtime"
-version = "0.7.0"
+version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c719dcf55f09a3a7e764c6649ab594c18a177e3599c467983cdf644bfc0a4088"
+checksum = "f9b0efd3ba03c3a409d44d60425f279ec442bcf0b9e63ff4e410da31c8b0f69f"
 
 [[package]]
 name = "proc-macro-error"
@@ -536,12 +554,9 @@
 
 [[package]]
 name = "textwrap"
-version = "0.14.2"
+version = "0.15.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
-dependencies = [
- "unicode-width",
-]
+checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
 
 [[package]]
 name = "thiserror"
@@ -562,12 +577,6 @@
  "quote",
  "syn",
 ]
-
-[[package]]
-name = "unicode-segmentation"
-version = "1.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
 
 [[package]]
 name = "unicode-width"
@@ -580,12 +589,6 @@
 version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
-
-[[package]]
-name = "vec_map"
-version = "0.8.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
 
 [[package]]
 name = "version_check"
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -179,7 +179,7 @@
 	}
 }
 
-pub type Result<V> = std::result::Result<V, LocError>;
+pub type Result<V, E = LocError> = std::result::Result<V, E>;
 
 #[macro_export]
 macro_rules! throw {
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -8,6 +8,7 @@
 };
 use gcmodule::Trace;
 use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::builtin;
 use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
 use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
 
@@ -377,6 +378,7 @@
 	pub has_default: bool,
 }
 
+/// Do not implement it directly, instead use #[builtin] macro
 pub trait Builtin: Trace {
 	fn name(&self) -> &str;
 	fn params(&self) -> &[BuiltinParam];
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -174,7 +174,11 @@
 	pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)
 }
 pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
-	EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))
+	EVAL_STATE.with(|s| {
+		f(s.borrow().as_ref().expect(
+			"missing evaluation state, some functions should be called inside of run_in_state call",
+		))
+	})
 }
 pub fn push_frame<T>(
 	e: Option<&ExprLocation>,
@@ -728,12 +732,15 @@
 	}
 
 	macro_rules! eval {
-		($str: expr) => {
-			EvaluationState::default()
-				.with_stdlib()
-				.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())
-				.unwrap()
-		};
+		($str: expr) => {{
+			let evaluator = EvaluationState::default();
+			evaluator.with_stdlib();
+			evaluator.run_in_state(|| {
+				evaluator
+					.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())
+					.unwrap()
+			})
+		}};
 	}
 	macro_rules! eval_json {
 		($str: expr) => {{
@@ -1265,4 +1272,47 @@
 		assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);
 		assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);
 	}
+
+	mod derive_typed {
+		use crate::{typed::Typed, EvaluationState};
+		use std::path::PathBuf;
+
+		#[derive(Typed, PartialEq, Debug)]
+		struct MyTyped {
+			a: u32,
+			b: String,
+		}
+
+		#[test]
+		fn test() {
+			let es = EvaluationState::default();
+			let val = eval!("{a: 14, b: 'Hello, world!'}");
+			let typed = es.run_in_state(|| MyTyped::try_from(val).unwrap());
+
+			assert_eq!(
+				typed,
+				MyTyped {
+					a: 14,
+					b: "Hello, world!".to_string()
+				}
+			);
+			es.settings_mut().globals.insert(
+				"mytyped".into(),
+				es.run_in_state(|| typed.try_into()).unwrap(),
+			);
+
+			let v = es
+				.evaluate_snippet_raw(
+					PathBuf::from("raw.jsonnet").into(),
+					"
+				mytyped == {a: 14, b: 'Hello, world!'}
+			"
+					.into(),
+				)
+				.unwrap()
+				.as_bool()
+				.unwrap();
+			assert!(v)
+		}
+	}
 }
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,6 +1,7 @@
 use std::convert::{TryFrom, TryInto};
 
 use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::Typed;
 use jrsonnet_types::{ComplexValType, ValType};
 
 use crate::{
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -8,20 +8,8 @@
 	push_description_frame, Val,
 };
 use gcmodule::Trace;
-use jrsonnet_types::{ComplexValType, ValType};
+pub 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_frame, typed::CheckType};
-		push_frame(None, $desc, || Ok($typ.check(&$value)?))?;
-		match $value {
-			$match(v) => v,
-			_ => unreachable!(),
-		}
-	}};
-}
 
 #[derive(Debug, Error, Clone, Trace)]
 pub enum TypeError {
@@ -136,7 +124,7 @@
 impl Display for ValuePathItem {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		match self {
-			Self::Field(name) => write!(f, ".{}", name)?,
+			Self::Field(name) => write!(f, ".{:?}", name)?,
 			Self::Index(idx) => write!(f, "[{}]", idx)?,
 		}
 		Ok(())
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -91,7 +91,7 @@
 	Normal(Cc<FuncDesc>),
 	/// Standard library function
 	StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),
-
+	/// User-provided function
 	Builtin(Cc<TraceBox<dyn Builtin>>),
 }
 
@@ -99,8 +99,10 @@
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		match self {
 			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),
-			Self::StaticBuiltin(arg0) => f.debug_tuple("Intrinsic").field(&arg0.name()).finish(),
-			Self::Builtin(arg0) => f.debug_tuple("Intrinsic").field(&arg0.name()).finish(),
+			Self::StaticBuiltin(arg0) => {
+				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()
+			}
+			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),
 		}
 	}
 }
@@ -338,6 +340,49 @@
 }
 
 impl Val {
+	pub fn as_bool(&self) -> Option<bool> {
+		match self {
+			Val::Bool(v) => Some(*v),
+			_ => None,
+		}
+	}
+	pub fn as_null(&self) -> Option<()> {
+		match self {
+			Val::Null => Some(()),
+			_ => None,
+		}
+	}
+	pub fn as_str(&self) -> Option<IStr> {
+		match self {
+			Val::Str(s) => Some(s.clone()),
+			_ => None,
+		}
+	}
+	pub fn as_num(&self) -> Option<f64> {
+		match self {
+			Val::Num(n) => Some(*n),
+			_ => None,
+		}
+	}
+	pub fn as_arr(&self) -> Option<ArrValue> {
+		match self {
+			Val::Arr(a) => Some(a.clone()),
+			_ => None,
+		}
+	}
+	pub fn as_obj(&self) -> Option<ObjValue> {
+		match self {
+			Val::Obj(o) => Some(o.clone()),
+			_ => None,
+		}
+	}
+	pub fn as_func(&self) -> Option<FuncVal> {
+		match self {
+			Val::Func(f) => Some(f.clone()),
+			_ => None,
+		}
+	}
+
 	/// Creates `Val::Num` after checking for numeric overflow.
 	/// As numbers are `f64`, we can just check for their finity.
 	pub fn new_checked_num(num: f64) -> Result<Self> {
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,8 +1,8 @@
 use quote::{quote, quote_spanned};
 use syn::{
 	parenthesized, parse::Parse, parse_macro_input, punctuated::Punctuated, spanned::Spanned,
-	token::Comma, FnArg, GenericArgument, Ident, ItemFn, Pat, PatType, Path, PathArguments, Token,
-	Type,
+	token::Comma, DeriveInput, FnArg, GenericArgument, Ident, ItemFn, Pat, PatType, Path,
+	PathArguments, Token, Type,
 };
 
 fn is_location_arg(t: &PatType) -> bool {
@@ -254,3 +254,86 @@
 	})
 	.into()
 }
+
+#[proc_macro_derive(Typed)]
+pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
+	let input = parse_macro_input!(item as DeriveInput);
+	let data = match &input.data {
+		syn::Data::Struct(s) => s,
+		_ => {
+			return syn::Error::new(input.span(), "only structs supported")
+				.to_compile_error()
+				.into()
+		}
+	};
+
+	let ident = &input.ident;
+
+	let fields_def = data.fields.iter().map(|f| {
+		let name = f
+			.ident
+			.as_ref()
+			.expect("only named fields supported")
+			.to_string();
+		let ty = &f.ty;
+		quote! {
+			(#name, #ty::TYPE),
+		}
+	});
+	let fields_parse = data.fields.iter().map(|f| {
+		let ident = f.ident.as_ref().unwrap();
+		let name = ident.to_string();
+		let ty = &f.ty;
+		quote! {
+			#ident: #ty::try_from(obj.get(#name.into())?.expect("shape is correct"))?,
+		}
+	});
+	let fields_serialize = data.fields.iter().map(|f| {
+		let ident = f.ident.as_ref().unwrap();
+		let name = ident.to_string();
+		quote! {
+			out.member(#name.into()).value(self.#ident.try_into()?);
+		}
+	});
+	let field_count = data.fields.len();
+
+	quote! {
+		const _: () = {
+			use ::jrsonnet_evaluator::{
+				typed::{ComplexValType, Typed, CheckType},
+				Val,
+				error::LocError,
+				obj::ObjValueBuilder,
+			};
+
+			const ITEMS: [(&'static str, &'static ComplexValType); #field_count] = [
+				#(#fields_def)*
+			];
+			impl Typed for #ident {
+				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);
+			}
+
+			impl TryFrom<Val> for #ident {
+				type Error = LocError;
+				fn try_from(value: Val) -> Result<Self, Self::Error> {
+					<Self as Typed>::TYPE.check(&value)?;
+					let obj = value.as_obj().expect("shape is correct");
+
+					Ok(Self {
+						#(#fields_parse)*
+					})
+				}
+			}
+			impl TryInto<Val> for #ident {
+				type Error = LocError;
+				fn try_into(self) -> Result<Val, Self::Error> {
+					let mut out = ObjValueBuilder::new();
+					#(#fields_serialize)*
+					Ok(Val::Obj(out.build()))
+				}
+			}
+			()
+		};
+	}
+	.into()
+}
modifiedcrates/jrsonnet-types/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-types/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use gcmodule::Trace;4use std::fmt::Display;56#[macro_export]7macro_rules! ty {8	((Array<number>)) => {{9		$crate::ComplexValType::ArrayRef(&$crate::ComplexValType::Simple($crate::ValType::Num))10	}};11	((Array<ubyte>)) => {{12		$crate::ComplexValType::ArrayRef(&$crate::ComplexValType::BoundedNumber(Some(0.0), Some(255.0)))13	}};14	(array) => {15		$crate::ComplexValType::Simple($crate::ValType::Arr)16	};17	(boolean) => {18		$crate::ComplexValType::Simple($crate::ValType::Bool)19	};20	(null) => {21		$crate::ComplexValType::Simple($crate::ValType::Null)22	};23	(string) => {24		$crate::ComplexValType::Simple($crate::ValType::Str)25	};26	(char) => {27		$crate::ComplexValType::Char28	};29	(number) => {30		$crate::ComplexValType::Simple($crate::ValType::Num)31	};32	(BoundedNumber<($min:expr), ($max:expr)>) => {{33		$crate::ComplexValType::BoundedNumber($min, $max)34	}};35	(object) => {36		$crate::ComplexValType::Simple($crate::ValType::Obj)37	};38	(any) => {39		$crate::ComplexValType::Any40	};41	(function) => {42		$crate::ComplexValType::Simple($crate::ValType::Func)43	};44	(($($a:tt) |+)) => {{45		static CONTENTS: &'static [&'static $crate::ComplexValType] = &[46			$(&ty!($a)),+47		];48		$crate::ComplexValType::UnionRef(CONTENTS)49	}};50	(($($a:tt) &+)) => {{51		static CONTENTS: &'static [&'static $crate::ComplexValType] = &[52			$(&ty!($a)),+53		];54		$crate::ComplexValType::SumRef(CONTENTS)55	}};56}5758#[test]59fn test() {60	assert_eq!(61		ty!((Array<number>)),62		ComplexValType::ArrayRef(&ComplexValType::Simple(ValType::Num))63	);64	assert_eq!(ty!(array), ComplexValType::Simple(ValType::Arr));65	assert_eq!(ty!(any), ComplexValType::Any);66	assert_eq!(67		ty!((string | number)),68		ComplexValType::UnionRef(&[69			&ComplexValType::Simple(ValType::Str),70			&ComplexValType::Simple(ValType::Num)71		])72	);73	assert_eq!(74		format!("{}", ty!(((string & number) | (object & null)))),75		"string & number | object & null"76	);77	assert_eq!(format!("{}", ty!((string | array))), "string | array");78	assert_eq!(79		format!("{}", ty!(((string & number) | array))),80		"string & number | array"81	);82}8384#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]85pub enum ValType {86	Bool,87	Null,88	Str,89	Num,90	Arr,91	Obj,92	Func,93}9495impl ValType {96	pub const fn name(&self) -> &'static str {97		use ValType::*;98		match self {99			Bool => "boolean",100			Null => "null",101			Str => "string",102			Num => "number",103			Arr => "array",104			Obj => "object",105			Func => "function",106		}107	}108}109110impl Display for ValType {111	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {112		write!(f, "{}", self.name())113	}114}115116#[derive(Debug, Clone, PartialEq, Trace)]117#[skip_trace]118pub enum ComplexValType {119	Any,120	Char,121	Simple(ValType),122	BoundedNumber(Option<f64>, Option<f64>),123	Array(Box<ComplexValType>),124	ArrayRef(&'static ComplexValType),125	ObjectRef(&'static [(&'static str, ComplexValType)]),126	Union(Vec<ComplexValType>),127	UnionRef(&'static [&'static ComplexValType]),128	Sum(Vec<ComplexValType>),129	SumRef(&'static [&'static ComplexValType]),130}131132impl From<ValType> for ComplexValType {133	fn from(s: ValType) -> Self {134		Self::Simple(s)135	}136}137138fn write_union<'i>(139	f: &mut std::fmt::Formatter<'_>,140	is_union: bool,141	union: impl Iterator<Item = &'i ComplexValType>,142) -> std::fmt::Result {143	for (i, v) in union.enumerate() {144		let should_add_braces =145			matches!(v, ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union);146		if i != 0 {147			write!(f, " {} ", if is_union { '|' } else { '&' })?;148		}149		if should_add_braces {150			write!(f, "(")?;151		}152		write!(f, "{}", v)?;153		if should_add_braces {154			write!(f, ")")?;155		}156	}157	Ok(())158}159160fn print_array(a: &ComplexValType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {161	if *a == ComplexValType::Any {162		write!(f, "array")?163	} else {164		write!(f, "Array<{}>", a)?165	}166	Ok(())167}168169impl Display for ComplexValType {170	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {171		match self {172			ComplexValType::Any => write!(f, "any")?,173			ComplexValType::Simple(s) => write!(f, "{}", s)?,174			ComplexValType::Char => write!(f, "char")?,175			ComplexValType::BoundedNumber(a, b) => write!(176				f,177				"BoundedNumber<{}, {}>",178				a.map(|e| e.to_string()).unwrap_or_else(|| "".into()),179				b.map(|e| e.to_string()).unwrap_or_else(|| "".into())180			)?,181			ComplexValType::ArrayRef(a) => print_array(a, f)?,182			ComplexValType::Array(a) => print_array(a, f)?,183			ComplexValType::ObjectRef(fields) => {184				write!(f, "{{")?;185				for (i, (k, v)) in fields.iter().enumerate() {186					if i != 0 {187						write!(f, ", ")?;188					}189					write!(f, "{}: {}", k, v)?;190				}191				write!(f, "}}")?;192			}193			ComplexValType::Union(v) => write_union(f, true, v.iter())?,194			ComplexValType::UnionRef(v) => write_union(f, true, v.iter().copied())?,195			ComplexValType::Sum(v) => write_union(f, false, v.iter())?,196			ComplexValType::SumRef(v) => write_union(f, false, v.iter().copied())?,197		};198		Ok(())199	}200}201202peg::parser! {203pub grammar parser() for str {204	rule number() -> f64205		= n:$(['0'..='9']+) { n.parse().unwrap() }206207	rule any_ty() -> ComplexValType = "any" { ComplexValType::Any }208	rule char_ty() -> ComplexValType = "character" { ComplexValType::Char }209	rule bool_ty() -> ComplexValType = "boolean" { ComplexValType::Simple(ValType::Bool) }210	rule null_ty() -> ComplexValType = "null" { ComplexValType::Simple(ValType::Null) }211	rule str_ty() -> ComplexValType = "string" { ComplexValType::Simple(ValType::Str) }212	rule num_ty() -> ComplexValType = "number" { ComplexValType::Simple(ValType::Num) }213	rule simple_array_ty() -> ComplexValType = "array" { ComplexValType::Simple(ValType::Arr) }214	rule simple_object_ty() -> ComplexValType = "object" { ComplexValType::Simple(ValType::Obj) }215	rule simple_function_ty() -> ComplexValType = "function" { ComplexValType::Simple(ValType::Func) }216217	rule array_ty() -> ComplexValType218		= "Array<" t:ty() ">" { ComplexValType::Array(Box::new(t)) }219220	rule bounded_number_ty() -> ComplexValType221		= "BoundedNumber<" a:number() ", " b:number() ">" { ComplexValType::BoundedNumber(Some(a), Some(b)) }222223	rule ty_basic() -> ComplexValType224		= any_ty()225		/ char_ty()226		/ bool_ty()227		/ null_ty()228		/ str_ty()229		/ num_ty()230		/ simple_array_ty()231		/ simple_object_ty()232		/ simple_function_ty()233		/ array_ty()234		/ bounded_number_ty()235236	pub rule ty() -> ComplexValType237		= precedence! {238			a:(@) " | " b:@ {239				match a {240					ComplexValType::Union(mut a) => {241						a.push(b);242						ComplexValType::Union(a)243					}244					_ => ComplexValType::Union(vec![a, b]),245				}246			}247			--248			a:(@) " & " b:@ {249				match a {250					ComplexValType::Sum(mut a) => {251						a.push(b);252						ComplexValType::Sum(a)253					}254					_ => ComplexValType::Sum(vec![a, b]),255				}256			}257			--258			"(" t:ty() ")" { t }259			t:ty_basic() { t }260		}261}262}263264#[cfg(test)]265pub mod tests {266	use super::parser;267268	#[test]269	fn precedence() {270		assert_eq!(271			parser::ty("(any & any) | (any | any) & any")272				.unwrap()273				.to_string(),274			"any & any | (any | any) & any"275		);276	}277278	#[test]279	fn array() {280		assert_eq!(parser::ty("Array<any>").unwrap().to_string(), "array");281		assert_eq!(282			parser::ty("Array<number>").unwrap().to_string(),283			"Array<number>"284		);285	}286	#[test]287	fn bounded_number() {288		assert_eq!(289			parser::ty("BoundedNumber<1, 2>").unwrap().to_string(),290			"BoundedNumber<1, 2>"291		);292	}293}
after · crates/jrsonnet-types/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use gcmodule::Trace;4use std::fmt::Display;56#[macro_export]7macro_rules! ty {8	((Array<number>)) => {{9		$crate::ComplexValType::ArrayRef(&$crate::ComplexValType::Simple($crate::ValType::Num))10	}};11	((Array<ubyte>)) => {{12		$crate::ComplexValType::ArrayRef(&$crate::ComplexValType::BoundedNumber(Some(0.0), Some(255.0)))13	}};14	(array) => {15		$crate::ComplexValType::Simple($crate::ValType::Arr)16	};17	(boolean) => {18		$crate::ComplexValType::Simple($crate::ValType::Bool)19	};20	(null) => {21		$crate::ComplexValType::Simple($crate::ValType::Null)22	};23	(string) => {24		$crate::ComplexValType::Simple($crate::ValType::Str)25	};26	(char) => {27		$crate::ComplexValType::Char28	};29	(number) => {30		$crate::ComplexValType::Simple($crate::ValType::Num)31	};32	(BoundedNumber<($min:expr), ($max:expr)>) => {{33		$crate::ComplexValType::BoundedNumber($min, $max)34	}};35	(object) => {36		$crate::ComplexValType::Simple($crate::ValType::Obj)37	};38	(any) => {39		$crate::ComplexValType::Any40	};41	(function) => {42		$crate::ComplexValType::Simple($crate::ValType::Func)43	};44	(($($a:tt) |+)) => {{45		static CONTENTS: &'static [&'static $crate::ComplexValType] = &[46			$(&ty!($a)),+47		];48		$crate::ComplexValType::UnionRef(CONTENTS)49	}};50	(($($a:tt) &+)) => {{51		static CONTENTS: &'static [&'static $crate::ComplexValType] = &[52			$(&ty!($a)),+53		];54		$crate::ComplexValType::SumRef(CONTENTS)55	}};56}5758#[test]59fn test() {60	assert_eq!(61		ty!((Array<number>)),62		ComplexValType::ArrayRef(&ComplexValType::Simple(ValType::Num))63	);64	assert_eq!(ty!(array), ComplexValType::Simple(ValType::Arr));65	assert_eq!(ty!(any), ComplexValType::Any);66	assert_eq!(67		ty!((string | number)),68		ComplexValType::UnionRef(&[69			&ComplexValType::Simple(ValType::Str),70			&ComplexValType::Simple(ValType::Num)71		])72	);73	assert_eq!(74		format!("{}", ty!(((string & number) | (object & null)))),75		"string & number | object & null"76	);77	assert_eq!(format!("{}", ty!((string | array))), "string | array");78	assert_eq!(79		format!("{}", ty!(((string & number) | array))),80		"string & number | array"81	);82}8384#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]85pub enum ValType {86	Bool,87	Null,88	Str,89	Num,90	Arr,91	Obj,92	Func,93}9495impl ValType {96	pub const fn name(&self) -> &'static str {97		use ValType::*;98		match self {99			Bool => "boolean",100			Null => "null",101			Str => "string",102			Num => "number",103			Arr => "array",104			Obj => "object",105			Func => "function",106		}107	}108}109110impl Display for ValType {111	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {112		write!(f, "{}", self.name())113	}114}115116#[derive(Debug, Clone, PartialEq, Trace)]117#[skip_trace]118pub enum ComplexValType {119	Any,120	Char,121	Simple(ValType),122	BoundedNumber(Option<f64>, Option<f64>),123	Array(Box<ComplexValType>),124	ArrayRef(&'static ComplexValType),125	ObjectRef(&'static [(&'static str, &'static ComplexValType)]),126	Union(Vec<ComplexValType>),127	UnionRef(&'static [&'static ComplexValType]),128	Sum(Vec<ComplexValType>),129	SumRef(&'static [&'static ComplexValType]),130}131132impl From<ValType> for ComplexValType {133	fn from(s: ValType) -> Self {134		Self::Simple(s)135	}136}137138fn write_union<'i>(139	f: &mut std::fmt::Formatter<'_>,140	is_union: bool,141	union: impl Iterator<Item = &'i ComplexValType>,142) -> std::fmt::Result {143	for (i, v) in union.enumerate() {144		let should_add_braces =145			matches!(v, ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union);146		if i != 0 {147			write!(f, " {} ", if is_union { '|' } else { '&' })?;148		}149		if should_add_braces {150			write!(f, "(")?;151		}152		write!(f, "{}", v)?;153		if should_add_braces {154			write!(f, ")")?;155		}156	}157	Ok(())158}159160fn print_array(a: &ComplexValType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {161	if *a == ComplexValType::Any {162		write!(f, "array")?163	} else {164		write!(f, "Array<{}>", a)?165	}166	Ok(())167}168169impl Display for ComplexValType {170	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {171		match self {172			ComplexValType::Any => write!(f, "any")?,173			ComplexValType::Simple(s) => write!(f, "{}", s)?,174			ComplexValType::Char => write!(f, "char")?,175			ComplexValType::BoundedNumber(a, b) => write!(176				f,177				"BoundedNumber<{}, {}>",178				a.map(|e| e.to_string()).unwrap_or_else(|| "".into()),179				b.map(|e| e.to_string()).unwrap_or_else(|| "".into())180			)?,181			ComplexValType::ArrayRef(a) => print_array(a, f)?,182			ComplexValType::Array(a) => print_array(a, f)?,183			ComplexValType::ObjectRef(fields) => {184				write!(f, "{{")?;185				for (i, (k, v)) in fields.iter().enumerate() {186					if i != 0 {187						write!(f, ", ")?;188					}189					write!(f, "{}: {}", k, v)?;190				}191				write!(f, "}}")?;192			}193			ComplexValType::Union(v) => write_union(f, true, v.iter())?,194			ComplexValType::UnionRef(v) => write_union(f, true, v.iter().copied())?,195			ComplexValType::Sum(v) => write_union(f, false, v.iter())?,196			ComplexValType::SumRef(v) => write_union(f, false, v.iter().copied())?,197		};198		Ok(())199	}200}201202peg::parser! {203pub grammar parser() for str {204	rule number() -> f64205		= n:$(['0'..='9']+) { n.parse().unwrap() }206207	rule any_ty() -> ComplexValType = "any" { ComplexValType::Any }208	rule char_ty() -> ComplexValType = "character" { ComplexValType::Char }209	rule bool_ty() -> ComplexValType = "boolean" { ComplexValType::Simple(ValType::Bool) }210	rule null_ty() -> ComplexValType = "null" { ComplexValType::Simple(ValType::Null) }211	rule str_ty() -> ComplexValType = "string" { ComplexValType::Simple(ValType::Str) }212	rule num_ty() -> ComplexValType = "number" { ComplexValType::Simple(ValType::Num) }213	rule simple_array_ty() -> ComplexValType = "array" { ComplexValType::Simple(ValType::Arr) }214	rule simple_object_ty() -> ComplexValType = "object" { ComplexValType::Simple(ValType::Obj) }215	rule simple_function_ty() -> ComplexValType = "function" { ComplexValType::Simple(ValType::Func) }216217	rule array_ty() -> ComplexValType218		= "Array<" t:ty() ">" { ComplexValType::Array(Box::new(t)) }219220	rule bounded_number_ty() -> ComplexValType221		= "BoundedNumber<" a:number() ", " b:number() ">" { ComplexValType::BoundedNumber(Some(a), Some(b)) }222223	rule ty_basic() -> ComplexValType224		= any_ty()225		/ char_ty()226		/ bool_ty()227		/ null_ty()228		/ str_ty()229		/ num_ty()230		/ simple_array_ty()231		/ simple_object_ty()232		/ simple_function_ty()233		/ array_ty()234		/ bounded_number_ty()235236	pub rule ty() -> ComplexValType237		= precedence! {238			a:(@) " | " b:@ {239				match a {240					ComplexValType::Union(mut a) => {241						a.push(b);242						ComplexValType::Union(a)243					}244					_ => ComplexValType::Union(vec![a, b]),245				}246			}247			--248			a:(@) " & " b:@ {249				match a {250					ComplexValType::Sum(mut a) => {251						a.push(b);252						ComplexValType::Sum(a)253					}254					_ => ComplexValType::Sum(vec![a, b]),255				}256			}257			--258			"(" t:ty() ")" { t }259			t:ty_basic() { t }260		}261}262}263264#[cfg(test)]265pub mod tests {266	use super::parser;267268	#[test]269	fn precedence() {270		assert_eq!(271			parser::ty("(any & any) | (any | any) & any")272				.unwrap()273				.to_string(),274			"any & any | (any | any) & any"275		);276	}277278	#[test]279	fn array() {280		assert_eq!(parser::ty("Array<any>").unwrap().to_string(), "array");281		assert_eq!(282			parser::ty("Array<number>").unwrap().to_string(),283			"Array<number>"284		);285	}286	#[test]287	fn bounded_number() {288		assert_eq!(289			parser::ty("BoundedNumber<1, 2>").unwrap().to_string(),290			"BoundedNumber<1, 2>"291		);292	}293}