git.delta.rocks / jrsonnet / refs/commits / df5053db3f0e

difftreelog

refactor do not allocate for BuiltinParam vec

mqrttluxYaroslav Bolyukin2026-03-21parent: #6a2bca3.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -11,18 +11,7 @@
 
 use self::destructure::destruct;
 use crate::{
-	arr::ArrValue,
-	bail,
-	destructure::evaluate_dest,
-	error::{suggest_object_fields, ErrorKind::*},
-	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
-	function::{CallLocation, FuncDesc, FuncVal},
-	gc::WithCapacityExt as _,
-	in_frame,
-	typed::Typed,
-	val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},
-	with_state, Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
-	ResultExt, SupThis, Unbound, Val,
+	Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt, SupThis, Unbound, Val, arr::ArrValue, bail, destructure::evaluate_dest, error::{ErrorKind::*, suggest_object_fields}, evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op}, function::{CallLocation, FuncDesc, FuncVal, builtin::{ParamDefault, ParamName, ParamParse}}, gc::WithCapacityExt as _, in_frame, typed::Typed, val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk}, with_state
 };
 pub mod destructure;
 pub mod operator;
@@ -88,6 +77,15 @@
 	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {
 		name,
 		ctx,
+		params_parse: params
+			.iter()
+			.map(|p| {
+				ParamParse::new(
+					p.0.name().map_or(ParamName::ANONYMOUS, ParamName::new),
+					ParamDefault::exists(p.1.is_some()),
+				)
+			})
+			.collect(),
 		params,
 		body,
 	})))
modifiedcrates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -1,22 +1,17 @@
-use std::{any::Any, borrow::Cow};
+use std::any::Any;
 
-use jrsonnet_gcmodule::{cc_dyn, Trace, TraceBox};
+use jrsonnet_gcmodule::{cc_dyn, Acyclic, Trace, TraceBox};
 use jrsonnet_interner::IStr;
 
 use super::{arglike::ArgsLike, parse::parse_builtin_call, CallLocation};
 use crate::{Context, Result, Val};
 
-/// Can't have `str` | `IStr`, because constant `BuiltinParam` causes
-/// `E0492: constant functions cannot refer to interior mutable data`
-#[derive(Clone, Trace)]
-pub struct ParamName(Option<Cow<'static, str>>);
+#[derive(Clone, Acyclic)]
+pub struct ParamName(Option<IStr>);
 impl ParamName {
 	pub const ANONYMOUS: Self = Self(None);
-	pub const fn new_static(name: &'static str) -> Self {
-		Self(Some(Cow::Borrowed(name)))
-	}
-	pub fn new_dynamic(name: String) -> Self {
-		Self(Some(Cow::Owned(name)))
+	pub fn new(name: IStr) -> Self {
+		Self(Some(name))
 	}
 	pub fn as_str(&self) -> Option<&str> {
 		self.0.as_deref()
@@ -33,7 +28,7 @@
 	}
 }
 
-#[derive(Clone, Copy, Debug, Trace)]
+#[derive(Clone, Copy, Debug, Acyclic)]
 pub enum ParamDefault {
 	None,
 	Exists,
@@ -49,13 +44,26 @@
 	}
 }
 
-#[derive(Clone, Trace)]
-pub struct BuiltinParam {
+#[macro_export]
+macro_rules! params {
+	(@name unnamed) => { ParamName::ANONYMOUS };
+	(@name named $name:literal) => { ParamName::new($crate::IStr::from($name)) };
+	($($(#[$meta:meta])* [$kind:ident $(($lit:literal))? => $default:expr]),* $(,)?) => {
+		thread_local! {
+			static PARAMS: [ParamParse; { const N: usize = <[u8]>::len(&[$($(#[$meta])* 0u8),*]); N }] = [
+				$($(#[$meta])* ParamParse::new(params!(@name $kind $($lit)?), $default)),*
+			];
+		}
+	};
+}
+
+#[derive(Clone, Acyclic)]
+pub struct ParamParse {
 	name: ParamName,
 	default: ParamDefault,
 }
-impl BuiltinParam {
-	pub const fn new(name: ParamName, default: ParamDefault) -> Self {
+impl ParamParse {
+	pub fn new(name: ParamName, default: ParamDefault) -> Self {
 		Self { name, default }
 	}
 	/// Parameter name for named call parsing
@@ -81,7 +89,7 @@
 		self.0.name()
 	}
 
-	fn params(&self) -> &[BuiltinParam] {
+	fn params(&self) -> &[ParamParse] {
 		self.0.params()
 	}
 
@@ -101,7 +109,7 @@
 	/// Function name to be used in stack traces
 	fn name(&self) -> &str;
 	/// Parameter names for named calls
-	fn params(&self) -> &[BuiltinParam];
+	fn params(&self) -> &[ParamParse];
 	/// Call the builtin
 	fn call(&self, ctx: Context, loc: CallLocation<'_>, args: &dyn ArgsLike) -> Result<Val>;
 
@@ -118,7 +126,7 @@
 
 #[derive(Trace)]
 pub struct NativeCallback {
-	pub(crate) params: Vec<BuiltinParam>,
+	pub(crate) params: Vec<ParamParse>,
 	handler: TraceBox<dyn NativeCallbackHandler>,
 }
 impl NativeCallback {
@@ -127,8 +135,8 @@
 		Self {
 			params: params
 				.into_iter()
-				.map(|n| BuiltinParam {
-					name: ParamName::new_dynamic(n),
+				.map(|n| ParamParse {
+					name: ParamName::new(n.into()),
 					default: ParamDefault::None,
 				})
 				.collect(),
@@ -144,7 +152,7 @@
 		"<native>"
 	}
 
-	fn params(&self) -> &[BuiltinParam] {
+	fn params(&self) -> &[ParamParse] {
 		&self.params
 	}
 
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -1,6 +1,7 @@
 use std::{fmt::Debug, rc::Rc};
 
 pub use arglike::{ArgLike, ArgsLike, TlaArg};
+use educe::Educe;
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
 pub use jrsonnet_macros::builtin;
@@ -8,7 +9,7 @@
 
 use self::{
 	arglike::OptionalContext,
-	builtin::{Builtin, BuiltinParam, ParamDefault, ParamName, StaticBuiltin},
+	builtin::{Builtin, ParamParse, StaticBuiltin},
 	native::NativeDesc,
 	parse::{parse_default_function_call, parse_function_call},
 };
@@ -40,7 +41,8 @@
 }
 
 /// Represents Jsonnet function defined in code.
-#[derive(Debug, Trace, PartialEq)]
+#[derive(Trace, Educe)]
+#[educe(Debug, PartialEq)]
 pub struct FuncDesc {
 	/// # Example
 	///
@@ -67,6 +69,9 @@
 	pub params: ParamsDesc,
 	/// Function body
 	pub body: Rc<Spanned<Expr>>,
+
+	#[educe(PartialEq = false, Debug = false)]
+	pub(crate) params_parse: Vec<ParamParse>,
 }
 impl FuncDesc {
 	/// Create body context, but fill arguments without defaults with lazy error
@@ -134,25 +139,13 @@
 		Self::StaticBuiltin(static_builtin)
 	}
 
-	pub fn params(&self) -> Vec<BuiltinParam> {
+	pub fn params(&self) -> &[ParamParse] {
 		match self {
-			Self::Id => ID.params().to_vec(),
-			Self::StaticBuiltin(i) => i.params().to_vec(),
-			Self::Builtin(i) => i.params().to_vec(),
-			Self::Normal(p) => p
-				.params
-				.iter()
-				.map(|p| {
-					BuiltinParam::new(
-						p.0.name()
-							.as_ref()
-							.map(IStr::to_string)
-							.map_or(ParamName::ANONYMOUS, ParamName::new_dynamic),
-						ParamDefault::exists(p.1.is_some()),
-					)
-				})
-				.collect(),
-			Self::Thunk(_) => vec![],
+			Self::Id => ID.params(),
+			Self::StaticBuiltin(i) => i.params(),
+			Self::Builtin(i) => i.params(),
+			Self::Normal(p) => &p.params_parse,
+			Self::Thunk(_) => &[],
 		}
 	}
 	/// Amount of non-default required arguments
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -4,7 +4,7 @@
 use jrsonnet_parser::ParamsDesc;
 use rustc_hash::FxHashMap;
 
-use super::{arglike::ArgsLike, builtin::BuiltinParam};
+use super::{arglike::ArgsLike, builtin::ParamParse};
 use crate::{
 	bail,
 	destructure::destruct,
@@ -147,7 +147,7 @@
 /// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
 pub fn parse_builtin_call(
 	ctx: Context,
-	params: &[BuiltinParam],
+	params: &[ParamParse],
 	args: &dyn ArgsLike,
 	tailstrict: bool,
 ) -> Result<Vec<Option<Thunk<Val>>>> {
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-macros/src/lib.rs
1use std::string::String;23use proc_macro2::TokenStream;4use quote::{quote, quote_spanned};5use syn::{6	parenthesized,7	parse::{Parse, ParseStream},8	parse_macro_input,9	punctuated::Punctuated,10	spanned::Spanned,11	token::{self, Comma},12	Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,13	LitStr, Pat, Path, PathArguments, Result, ReturnType, Token, Type,14};1516fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>17where18	Ident: PartialEq<I>,19{20	let attrs = attrs21		.iter()22		.filter(|a| a.path().is_ident(&ident))23		.collect::<Vec<_>>();24	if attrs.len() > 1 {25		return Err(Error::new(26			attrs[1].span(),27			"this attribute may be specified only once",28		));29	} else if attrs.is_empty() {30		return Ok(None);31	}32	let attr = attrs[0];33	let attr = attr.parse_args::<A>()?;3435	Ok(Some(attr))36}37fn remove_attr<I>(attrs: &mut Vec<Attribute>, ident: I)38where39	Ident: PartialEq<I>,40{41	attrs.retain(|a| !a.path().is_ident(&ident));42}4344fn path_is(path: &Path, needed: &str) -> bool {45	path.leading_colon.is_none()46		&& !path.segments.is_empty()47		&& path.segments.iter().last().unwrap().ident == needed48}4950fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {51	match ty {52		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {53			let args = &path.path.segments.iter().last().unwrap().arguments;54			Some(args)55		}56		_ => None,57	}58}5960fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {61	let Some(args) = type_is_path(ty, "Option") else {62		return Ok(None);63	};64	// It should have only on angle-bracketed param ("<String>"):65	let PathArguments::AngleBracketed(params) = args else {66		return Err(Error::new(args.span(), "missing option generic"));67	};68	let generic_arg = params.args.iter().next().unwrap();69	// This argument must be a type:70	let GenericArgument::Type(ty) = generic_arg else {71		return Err(Error::new(72			generic_arg.span(),73			"option generic should be a type",74		));75	};76	Ok(Some(ty))77}7879struct Field {80	attrs: Vec<Attribute>,81	name: Ident,82	_colon: Token![:],83	ty: Type,84}85impl Parse for Field {86	fn parse(input: ParseStream) -> syn::Result<Self> {87		Ok(Self {88			attrs: input.call(Attribute::parse_outer)?,89			name: input.parse()?,90			_colon: input.parse()?,91			ty: input.parse()?,92		})93	}94}9596mod kw {97	syn::custom_keyword!(fields);98	syn::custom_keyword!(rename);99	syn::custom_keyword!(alias);100	syn::custom_keyword!(flatten);101	syn::custom_keyword!(add);102	syn::custom_keyword!(hide);103	syn::custom_keyword!(ok);104}105106struct BuiltinAttrs {107	fields: Vec<Field>,108}109impl Parse for BuiltinAttrs {110	fn parse(input: ParseStream) -> syn::Result<Self> {111		if input.is_empty() {112			return Ok(Self { fields: Vec::new() });113		}114		input.parse::<kw::fields>()?;115		let fields;116		parenthesized!(fields in input);117		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;118		Ok(Self {119			fields: p.into_iter().collect(),120		})121	}122}123124enum Optionality {125	Required,126	Optional,127	Default(Expr),128}129130enum ArgInfo {131	Normal {132		ty: Box<Type>,133		optionality: Optionality,134		name: Option<String>,135		cfg_attrs: Vec<Attribute>,136	},137	Lazy {138		is_option: bool,139		name: Option<String>,140	},141	Context,142	Location,143	This,144}145146impl ArgInfo {147	fn parse(name: &str, arg: &mut FnArg) -> Result<Self> {148		let FnArg::Typed(arg) = arg else {149			unreachable!()150		};151		let ident = match &arg.pat as &Pat {152			Pat::Ident(i) => Some(i.ident.clone()),153			_ => None,154		};155		let ty = &arg.ty;156		if type_is_path(ty, "Context").is_some() {157			return Ok(Self::Context);158		} else if type_is_path(ty, "CallLocation").is_some() {159			return Ok(Self::Location);160		} else if type_is_path(ty, "Thunk").is_some() {161			return Ok(Self::Lazy {162				is_option: false,163				name: ident.map(|v| v.to_string()),164			});165		}166167		match ty as &Type {168			Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),169			_ => {}170		}171172		let (optionality, ty) = if let Some(default) = parse_attr::<_, _>(&arg.attrs, "default")? {173			remove_attr(&mut arg.attrs, "default");174			(Optionality::Default(default), ty.clone())175		} else if let Some(ty) = extract_type_from_option(ty)? {176			if type_is_path(ty, "Thunk").is_some() {177				return Ok(Self::Lazy {178					is_option: true,179					name: ident.map(|v| v.to_string()),180				});181			}182183			(Optionality::Optional, Box::new(ty.clone()))184		} else {185			(Optionality::Required, ty.clone())186		};187188		let cfg_attrs = arg189			.attrs190			.iter()191			.filter(|a| a.path().is_ident("cfg"))192			.cloned()193			.collect();194195		Ok(Self::Normal {196			ty,197			optionality,198			name: ident.map(|v| v.to_string()),199			cfg_attrs,200		})201	}202}203204#[proc_macro_attribute]205pub fn builtin(206	attr: proc_macro::TokenStream,207	item: proc_macro::TokenStream,208) -> proc_macro::TokenStream {209	let attr = parse_macro_input!(attr as BuiltinAttrs);210	let item_fn = parse_macro_input!(item as ItemFn);211212	match builtin_inner(attr, item_fn) {213		Ok(v) => v.into(),214		Err(e) => e.into_compile_error().into(),215	}216}217218#[allow(clippy::too_many_lines)]219fn builtin_inner(attr: BuiltinAttrs, mut fun: ItemFn) -> syn::Result<TokenStream> {220	let ReturnType::Type(_, result) = &fun.sig.output else {221		return Err(Error::new(222			fun.sig.span(),223			"builtin should return something",224		));225	};226227	let name = fun.sig.ident.to_string();228	let args = fun229		.sig230		.inputs231		.iter_mut()232		.map(|arg| ArgInfo::parse(&name, arg))233		.collect::<Result<Vec<_>>>()?;234235	let params_desc = args.iter().filter_map(|a| match a {236		ArgInfo::Normal {237			optionality,238			name,239			cfg_attrs,240			..241		} => {242			let name = name243				.as_ref()244				.map_or_else(|| quote! {None}, |n| quote! {ParamName::new_static(#n)});245			let default = match optionality {246				Optionality::Required => quote!(ParamDefault::None),247				Optionality::Optional => quote!(ParamDefault::Exists),248				Optionality::Default(e) => quote!(ParamDefault::Literal(stringify!(#e))),249			};250			Some(quote! {251				#(#cfg_attrs)*252				BuiltinParam::new(#name, #default),253			})254		}255		ArgInfo::Lazy { is_option, name } => {256			let name = name257				.as_ref()258				.map_or_else(|| quote! {None}, |n| quote! {ParamName::new_static(#n)});259			Some(quote! {260				BuiltinParam::new(#name, ParamDefault::exists(#is_option)),261			})262		}263		ArgInfo::Context | ArgInfo::Location | ArgInfo::This => None,264	});265266	let mut id = 0usize;267	let pass = args268		.iter()269		.map(|a| match a {270			ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {271				let cid = id;272				id += 1;273				(quote! {#cid}, a)274			}275			ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {276				(quote! {compile_error!("should not use id")}, a)277			}278		})279		.map(|(id, a)| match a {280			ArgInfo::Normal {281				ty,282				optionality,283				name,284				cfg_attrs,285			} => {286				let name = name.as_ref().map_or("<unnamed>", String::as_str);287				let eval = quote! {jrsonnet_evaluator::in_description_frame(288					|| format!("argument <{}> evaluation", #name),289					|| <#ty>::from_untyped(value.evaluate()?),290				)?};291				let value = match optionality {292					Optionality::Required => quote! {{293						let value = parsed[#id].as_ref().expect("args shape is checked");294						#eval295					},},296					Optionality::Optional => quote! {if let Some(value) = &parsed[#id] {297						Some(#eval)298					} else {299						None300					},},301					Optionality::Default(expr) => quote! {if let Some(value) = &parsed[#id] {302						#eval303					} else {304						let v: #ty = #expr;305						v306					},},307				};308				quote! {309					#(#cfg_attrs)*310					#value311				}312			}313			ArgInfo::Lazy { is_option, .. } => {314				if *is_option {315					quote! {if let Some(value) = &parsed[#id] {316						Some(value.clone())317					} else {318						None319					},}320				} else {321					quote! {322						parsed[#id].as_ref().expect("args shape is correct").clone(),323					}324				}325			}326			ArgInfo::Context => quote! {ctx.clone(),},327			ArgInfo::Location => quote! {location,},328			ArgInfo::This => quote! {self,},329		});330331	let fields = attr.fields.iter().map(|field| {332		let attrs = &field.attrs;333		let name = &field.name;334		let ty = &field.ty;335		quote! {336			#(#attrs)*337			pub #name: #ty,338		}339	});340341	let name = &fun.sig.ident;342	let vis = &fun.vis;343	let static_ext = if attr.fields.is_empty() {344		quote! {345			impl #name {346				pub const INST: &'static dyn StaticBuiltin = &#name {};347			}348			impl StaticBuiltin for #name {}349		}350	} else {351		quote! {}352	};353	let static_derive_copy = if attr.fields.is_empty() {354		quote! {, Copy}355	} else {356		quote! {}357	};358359	Ok(quote! {360		#fun361362		#[doc(hidden)]363		#[allow(non_camel_case_types)]364		#[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]365		#vis struct #name {366			#(#fields)*367		}368		const _: () = {369			use ::jrsonnet_evaluator::{370				State, Val,371				function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName, ParamDefault}, CallLocation, ArgsLike, parse::parse_builtin_call},372				Result, Context, typed::Typed,373				parser::Span,374			};375			const PARAMS: &'static [BuiltinParam] = &[376				#(#params_desc)*377			];378379			#static_ext380			impl Builtin for #name381			where382				Self: 'static383			{384				fn name(&self) -> &str {385					stringify!(#name)386				}387				fn params(&self) -> &[BuiltinParam] {388					PARAMS389				}390				#[allow(unused_variables)]391				fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {392					let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;393394					let result: #result = #name(#(#pass)*);395					<_ as Typed>::into_result(result)396				}397				fn as_any(&self) -> &dyn ::std::any::Any {398					self399				}400			}401		};402	})403}404405#[derive(Default)]406#[allow(clippy::struct_excessive_bools)]407struct TypedAttr {408	rename: Option<String>,409	aliases: Vec<String>,410	flatten: bool,411	/// flatten(ok) strategy for flattened optionals412	/// field would be None in case of any parsing error (as in serde)413	flatten_ok: bool,414	// Should it be `field+:` instead of `field:`415	add: bool,416	// Should it be `field::` instead of `field:`417	hide: bool,418}419impl Parse for TypedAttr {420	fn parse(input: ParseStream) -> syn::Result<Self> {421		let mut out = Self::default();422		loop {423			let lookahead = input.lookahead1();424			if lookahead.peek(kw::rename) {425				input.parse::<kw::rename>()?;426				input.parse::<Token![=]>()?;427				let name = input.parse::<LitStr>()?;428				if out.rename.is_some() {429					return Err(Error::new(430						name.span(),431						"rename attribute may only be specified once",432					));433				}434				out.rename = Some(name.value());435			} else if lookahead.peek(kw::alias) {436				input.parse::<kw::alias>()?;437				input.parse::<Token![=]>()?;438				let alias = input.parse::<LitStr>()?;439				out.aliases.push(alias.value());440			} else if lookahead.peek(kw::flatten) {441				input.parse::<kw::flatten>()?;442				out.flatten = true;443				if input.peek(token::Paren) {444					let content;445					parenthesized!(content in input);446					let lookahead = content.lookahead1();447					if lookahead.peek(kw::ok) {448						content.parse::<kw::ok>()?;449						out.flatten_ok = true;450					} else {451						return Err(lookahead.error());452					}453				}454			} else if lookahead.peek(kw::add) {455				input.parse::<kw::add>()?;456				out.add = true;457			} else if lookahead.peek(kw::hide) {458				input.parse::<kw::hide>()?;459				out.hide = true;460			} else if input.is_empty() {461				break;462			} else {463				return Err(lookahead.error());464			}465			if input.peek(Token![,]) {466				input.parse::<Token![,]>()?;467			} else {468				break;469			}470		}471		Ok(out)472	}473}474475struct TypedField {476	attr: TypedAttr,477	ident: Ident,478	ty: Type,479	is_option: bool,480	is_lazy: bool,481}482impl TypedField {483	fn parse(field: &syn::Field) -> Result<Self> {484		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();485		let Some(ident) = field.ident.clone() else {486			return Err(Error::new(487				field.span(),488				"this field should appear in output object, but it has no visible name",489			));490		};491		let (is_option, ty) = extract_type_from_option(&field.ty)?492			.map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));493		if is_option && attr.flatten {494			if !attr.flatten_ok {495				return Err(Error::new(496					field.span(),497					"strategy should be set when flattening Option",498				));499			}500		} else if attr.flatten_ok {501			return Err(Error::new(502				field.span(),503				"flatten(ok) is only useable on optional fields",504			));505		}506507		let is_lazy = type_is_path(&ty, "Thunk").is_some();508509		Ok(Self {510			attr,511			ident,512			ty,513			is_option,514			is_lazy,515		})516	}517	/// None if this field is flattened in jsonnet output518	fn name(&self) -> Option<String> {519		if self.attr.flatten {520			return None;521		}522		Some(523			self.attr524				.rename525				.clone()526				.unwrap_or_else(|| self.ident.to_string()),527		)528	}529530	fn expand_field(&self) -> Option<TokenStream> {531		if self.is_option {532			return None;533		}534		let name = self.name()?;535		let ty = &self.ty;536		Some(quote! {537			(#name, <#ty as Typed>::TYPE)538		})539	}540541	fn expand_parse(&self) -> TokenStream {542		if self.is_option {543			self.expand_parse_optional()544		} else {545			self.expand_parse_mandatory()546		}547	}548549	fn expand_parse_optional(&self) -> TokenStream {550		let ident = &self.ident;551		let ty = &self.ty;552553		// optional flatten is handled in same way as serde554		if self.attr.flatten {555			return quote! {556				#ident: <#ty as TypedObj>::parse(&obj).ok(),557			};558		}559560		let name = self.name().unwrap();561		let aliases = &self.attr.aliases;562563		quote! {564			#ident: {565				let __value = if let Some(__v) = obj.get(#name.into())? {566					Some(__v)567				} #(else if let Some(__v) = obj.get(#aliases.into())? {568					Some(__v)569				})* else {570					None571				};572573				__value.map(<#ty as Typed>::from_untyped).transpose()?574			},575		}576	}577578	fn expand_parse_mandatory(&self) -> TokenStream {579		let ident = &self.ident;580		let ty = &self.ty;581582		// optional flatten is handled in same way as serde583		if self.attr.flatten {584			return quote! {585				#ident: <#ty as TypedObj>::parse(&obj)?,586			};587		}588589		let name = self.name().unwrap();590		let aliases = &self.attr.aliases;591592		let error_text = if aliases.is_empty() {593			// clippy does not understand name variable usage in quote! macro594			#[allow(clippy::redundant_clone)]595			name.clone()596		} else {597			format!("{name} (alias {})", aliases.join(", "))598		};599600		quote! {601			#ident: {602				let __value = if let Some(__v) = obj.get(#name.into())? {603					__v604				} #(else if let Some(__v) = obj.get(#aliases.into())? {605					__v606				})* else {607					return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());608				};609610				<#ty as Typed>::from_untyped(__value)?611			},612		}613	}614615	fn expand_serialize(&self) -> TokenStream {616		let ident = &self.ident;617		let ty = &self.ty;618		self.name().map_or_else(619			|| {620				if self.is_option {621					quote! {622						if let Some(value) = self.#ident {623							<#ty as TypedObj>::serialize(value, out)?;624						}625					}626				} else {627					quote! {628						<#ty as TypedObj>::serialize(self.#ident, out)?;629					}630				}631			},632			|name| {633				let hide = if self.attr.hide {634					quote! {.hide()}635				} else {636					quote! {}637				};638				let add = if self.attr.add {639					quote! {.add()}640				} else {641					quote! {}642				};643				let value = if self.is_lazy {644					quote! {645						out.field(#name)646							#hide647							#add648							.try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;649					}650				} else {651					quote! {652						out.field(#name)653							#hide654							#add655							.try_value(<#ty as Typed>::into_untyped(value)?)?;656					}657				};658				if self.is_option {659					quote! {660						if let Some(value) = self.#ident {661							#value662						}663					}664				} else {665					quote! {666						{667							let value = self.#ident;668							#value669						}670					}671				}672			},673		)674	}675}676677#[proc_macro_derive(Typed, attributes(typed))]678pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {679	let input = parse_macro_input!(item as DeriveInput);680681	match derive_typed_inner(input) {682		Ok(v) => v.into(),683		Err(e) => e.to_compile_error().into(),684	}685}686687fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {688	let syn::Data::Struct(data) = &input.data else {689		return Err(Error::new(input.span(), "only structs supported"));690	};691692	let ident = &input.ident;693	let fields = data694		.fields695		.iter()696		.map(TypedField::parse)697		.collect::<Result<Vec<_>>>()?;698699	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();700701	let typed = {702		let fields = fields703			.iter()704			.filter_map(TypedField::expand_field)705			.collect::<Vec<_>>();706		quote! {707			impl #impl_generics Typed for #ident #ty_generics #where_clause {708				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[709					#(#fields,)*710				]);711712				fn from_untyped(value: Val) -> JrResult<Self> {713					let obj = value.as_obj().expect("shape is correct");714					Self::parse(&obj)715				}716717				fn into_untyped(value: Self) -> JrResult<Val> {718					let mut out = ObjValueBuilder::new();719					value.serialize(&mut out)?;720					Ok(Val::Obj(out.build()))721				}722723			}724		}725	};726727	let fields_parse = fields.iter().map(TypedField::expand_parse);728	let fields_serialize = fields729		.iter()730		.map(TypedField::expand_serialize)731		.collect::<Vec<_>>();732733	Ok(quote! {734		const _: () = {735			use ::jrsonnet_evaluator::{736				typed::{ComplexValType, Typed, TypedObj, CheckType},737				Val, State,738				error::{ErrorKind, Result as JrResult},739				ObjValueBuilder, ObjValue,740			};741742			#typed743744			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {745				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {746					#(#fields_serialize)*747748					Ok(())749				}750				fn parse(obj: &ObjValue) -> JrResult<Self> {751					Ok(Self {752						#(#fields_parse)*753					})754				}755			}756		};757	})758}759760struct FormatInput {761	formatting: LitStr,762	arguments: Vec<Expr>,763}764impl Parse for FormatInput {765	fn parse(input: ParseStream) -> Result<Self> {766		let formatting = input.parse()?;767		let mut arguments = Vec::new();768769		while input.peek(Token![,]) {770			input.parse::<Token![,]>()?;771			if input.is_empty() {772				// Trailing comma773				break;774			}775			let expr = input.parse()?;776			arguments.push(expr);777		}778779		if !input.is_empty() {780			return Err(syn::Error::new(input.span(), "unexpected trailing input"));781		}782783		Ok(Self {784			formatting,785			arguments,786		})787	}788}789fn is_format_str(i: &str) -> bool {790	let mut is_plain = true;791	// -1 = {792	// +1 = }793	let mut is_bracket = 0i8;794	for ele in i.chars() {795		match ele {796			'{' if is_bracket == -1 => {797				is_bracket = 0;798			}799			'}' if is_bracket == -1 => {800				is_plain = false;801				break;802			}803			'}' if is_bracket == 1 => {804				is_bracket = 0;805			}806			'{' if is_bracket == 1 => {807				is_plain = false;808				break;809			}810			'{' => {811				is_bracket = -1;812			}813			'}' => {814				is_bracket = 1;815			}816			_ if is_bracket != 0 => {817				is_plain = false;818				break;819			}820			_ => {}821		}822	}823	!is_plain || is_bracket != 0824}825impl FormatInput {826	fn expand(self) -> TokenStream {827		let format = self.formatting;828		if is_format_str(&format.value()) {829			let args = self.arguments;830			quote! {831				::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))832			}833		} else {834			if let Some(first) = self.arguments.first() {835				return syn::Error::new(836					first.span(),837					"string has no formatting codes, it should not have the arguments",838				)839				.into_compile_error();840			}841			quote! {842				::jrsonnet_evaluator::IStr::from(#format)843			}844		}845	}846}847848/// `IStr` formatting helper849///850/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`851/// This macro looks for formatting codes in the input string, and uses852/// `format!()` only when necessary853#[proc_macro]854pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {855	let input = parse_macro_input!(input as FormatInput);856	input.expand().into()857}858859/// Create Thunk using closure syntax860#[proc_macro]861#[allow(non_snake_case)]862pub fn Thunk(input: proc_macro::TokenStream) -> proc_macro::TokenStream {863	let input = parse_macro_input!(input as ExprClosure);864865	let span = input.inputs.span();866	let move_check = input.capture.is_none().then(|| {867		quote_spanned! {span => {868			compile_error!("Thunk! needs to be called with move closure");869		}}870	});871872	let (env, closure, args) = syn_dissect_closure::split_env(input);873874	let trace_check = args.iter().map(|el| {875		let span = el.span();876		quote_spanned! {span => ::jrsonnet_evaluator::gc::assert_trace(&#el);}877	});878879	quote! {{880		#move_check881		#(#trace_check)*882		::jrsonnet_evaluator::Thunk::new(::jrsonnet_evaluator::val::MemoizedClosureThunk::new(#env, #closure))883	}}.into()884}
after · crates/jrsonnet-macros/src/lib.rs
1use std::string::String;23use proc_macro2::TokenStream;4use quote::{quote, quote_spanned};5use syn::{6	parenthesized,7	parse::{Parse, ParseStream},8	parse_macro_input,9	punctuated::Punctuated,10	spanned::Spanned,11	token::{self, Comma},12	Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,13	LitStr, Pat, Path, PathArguments, Result, ReturnType, Token, Type,14};1516fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>17where18	Ident: PartialEq<I>,19{20	let attrs = attrs21		.iter()22		.filter(|a| a.path().is_ident(&ident))23		.collect::<Vec<_>>();24	if attrs.len() > 1 {25		return Err(Error::new(26			attrs[1].span(),27			"this attribute may be specified only once",28		));29	} else if attrs.is_empty() {30		return Ok(None);31	}32	let attr = attrs[0];33	let attr = attr.parse_args::<A>()?;3435	Ok(Some(attr))36}37fn remove_attr<I>(attrs: &mut Vec<Attribute>, ident: I)38where39	Ident: PartialEq<I>,40{41	attrs.retain(|a| !a.path().is_ident(&ident));42}4344fn path_is(path: &Path, needed: &str) -> bool {45	path.leading_colon.is_none()46		&& !path.segments.is_empty()47		&& path.segments.iter().last().unwrap().ident == needed48}4950fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {51	match ty {52		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {53			let args = &path.path.segments.iter().last().unwrap().arguments;54			Some(args)55		}56		_ => None,57	}58}5960fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {61	let Some(args) = type_is_path(ty, "Option") else {62		return Ok(None);63	};64	// It should have only on angle-bracketed param ("<String>"):65	let PathArguments::AngleBracketed(params) = args else {66		return Err(Error::new(args.span(), "missing option generic"));67	};68	let generic_arg = params.args.iter().next().unwrap();69	// This argument must be a type:70	let GenericArgument::Type(ty) = generic_arg else {71		return Err(Error::new(72			generic_arg.span(),73			"option generic should be a type",74		));75	};76	Ok(Some(ty))77}7879struct Field {80	attrs: Vec<Attribute>,81	name: Ident,82	_colon: Token![:],83	ty: Type,84}85impl Parse for Field {86	fn parse(input: ParseStream) -> syn::Result<Self> {87		Ok(Self {88			attrs: input.call(Attribute::parse_outer)?,89			name: input.parse()?,90			_colon: input.parse()?,91			ty: input.parse()?,92		})93	}94}9596mod kw {97	syn::custom_keyword!(fields);98	syn::custom_keyword!(rename);99	syn::custom_keyword!(alias);100	syn::custom_keyword!(flatten);101	syn::custom_keyword!(add);102	syn::custom_keyword!(hide);103	syn::custom_keyword!(ok);104}105106struct BuiltinAttrs {107	fields: Vec<Field>,108}109impl Parse for BuiltinAttrs {110	fn parse(input: ParseStream) -> syn::Result<Self> {111		if input.is_empty() {112			return Ok(Self { fields: Vec::new() });113		}114		input.parse::<kw::fields>()?;115		let fields;116		parenthesized!(fields in input);117		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;118		Ok(Self {119			fields: p.into_iter().collect(),120		})121	}122}123124enum Optionality {125	Required,126	Optional,127	Default(Expr),128}129130enum ArgInfo {131	Normal {132		ty: Box<Type>,133		optionality: Optionality,134		name: Option<String>,135		cfg_attrs: Vec<Attribute>,136	},137	Lazy {138		is_option: bool,139		name: Option<String>,140	},141	Context,142	Location,143	This,144}145146impl ArgInfo {147	fn parse(name: &str, arg: &mut FnArg) -> Result<Self> {148		let FnArg::Typed(arg) = arg else {149			unreachable!()150		};151		let ident = match &arg.pat as &Pat {152			Pat::Ident(i) => Some(i.ident.clone()),153			_ => None,154		};155		let ty = &arg.ty;156		if type_is_path(ty, "Context").is_some() {157			return Ok(Self::Context);158		} else if type_is_path(ty, "CallLocation").is_some() {159			return Ok(Self::Location);160		} else if type_is_path(ty, "Thunk").is_some() {161			return Ok(Self::Lazy {162				is_option: false,163				name: ident.map(|v| v.to_string()),164			});165		}166167		match ty as &Type {168			Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),169			_ => {}170		}171172		let (optionality, ty) = if let Some(default) = parse_attr::<_, _>(&arg.attrs, "default")? {173			remove_attr(&mut arg.attrs, "default");174			(Optionality::Default(default), ty.clone())175		} else if let Some(ty) = extract_type_from_option(ty)? {176			if type_is_path(ty, "Thunk").is_some() {177				return Ok(Self::Lazy {178					is_option: true,179					name: ident.map(|v| v.to_string()),180				});181			}182183			(Optionality::Optional, Box::new(ty.clone()))184		} else {185			(Optionality::Required, ty.clone())186		};187188		let cfg_attrs = arg189			.attrs190			.iter()191			.filter(|a| a.path().is_ident("cfg"))192			.cloned()193			.collect();194195		Ok(Self::Normal {196			ty,197			optionality,198			name: ident.map(|v| v.to_string()),199			cfg_attrs,200		})201	}202}203204#[proc_macro_attribute]205pub fn builtin(206	attr: proc_macro::TokenStream,207	item: proc_macro::TokenStream,208) -> proc_macro::TokenStream {209	let attr = parse_macro_input!(attr as BuiltinAttrs);210	let item_fn = parse_macro_input!(item as ItemFn);211212	match builtin_inner(attr, item_fn) {213		Ok(v) => v.into(),214		Err(e) => e.into_compile_error().into(),215	}216}217218#[allow(clippy::too_many_lines)]219fn builtin_inner(attr: BuiltinAttrs, mut fun: ItemFn) -> syn::Result<TokenStream> {220	let ReturnType::Type(_, result) = &fun.sig.output else {221		return Err(Error::new(222			fun.sig.span(),223			"builtin should return something",224		));225	};226227	let name = fun.sig.ident.to_string();228	let args = fun229		.sig230		.inputs231		.iter_mut()232		.map(|arg| ArgInfo::parse(&name, arg))233		.collect::<Result<Vec<_>>>()?;234235	let params_desc = args.iter().filter_map(|a| match a {236		ArgInfo::Normal {237			optionality,238			name,239			cfg_attrs,240			..241		} => {242			let name = name.as_ref().map_or_else(|| quote! {unnamed}, |n| quote! {named(#n)});243			let default = match optionality {244				Optionality::Required => quote!(ParamDefault::None),245				Optionality::Optional => quote!(ParamDefault::Exists),246				Optionality::Default(e) => quote!(ParamDefault::Literal(stringify!(#e))),247			};248			Some(quote! {249				#(#cfg_attrs)*250				[#name => #default],251			})252		}253		ArgInfo::Lazy { is_option, name } => {254			let name = name.as_ref().map_or_else(|| quote! {unnamed}, |n| quote! {named(#n)});255			Some(quote! {256				[#name => ParamDefault::exists(#is_option)],257			})258		}259		ArgInfo::Context | ArgInfo::Location | ArgInfo::This => None,260	});261262	let mut id = 0usize;263	let pass = args264		.iter()265		.map(|a| match a {266			ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {267				let cid = id;268				id += 1;269				(quote! {#cid}, a)270			}271			ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {272				(quote! {compile_error!("should not use id")}, a)273			}274		})275		.map(|(id, a)| match a {276			ArgInfo::Normal {277				ty,278				optionality,279				name,280				cfg_attrs,281			} => {282				let name = name.as_ref().map_or("<unnamed>", String::as_str);283				let eval = quote! {jrsonnet_evaluator::in_description_frame(284					|| format!("argument <{}> evaluation", #name),285					|| <#ty>::from_untyped(value.evaluate()?),286				)?};287				let value = match optionality {288					Optionality::Required => quote! {{289						let value = parsed[#id].as_ref().expect("args shape is checked");290						#eval291					},},292					Optionality::Optional => quote! {if let Some(value) = &parsed[#id] {293						Some(#eval)294					} else {295						None296					},},297					Optionality::Default(expr) => quote! {if let Some(value) = &parsed[#id] {298						#eval299					} else {300						let v: #ty = #expr;301						v302					},},303				};304				quote! {305					#(#cfg_attrs)*306					#value307				}308			}309			ArgInfo::Lazy { is_option, .. } => {310				if *is_option {311					quote! {if let Some(value) = &parsed[#id] {312						Some(value.clone())313					} else {314						None315					},}316				} else {317					quote! {318						parsed[#id].as_ref().expect("args shape is correct").clone(),319					}320				}321			}322			ArgInfo::Context => quote! {ctx.clone(),},323			ArgInfo::Location => quote! {location,},324			ArgInfo::This => quote! {self,},325		});326327	let fields = attr.fields.iter().map(|field| {328		let attrs = &field.attrs;329		let name = &field.name;330		let ty = &field.ty;331		quote! {332			#(#attrs)*333			pub #name: #ty,334		}335	});336337	let name = &fun.sig.ident;338	let vis = &fun.vis;339	let static_ext = if attr.fields.is_empty() {340		quote! {341			impl #name {342				pub const INST: &'static dyn StaticBuiltin = &#name {};343			}344			impl StaticBuiltin for #name {}345		}346	} else {347		quote! {}348	};349	let static_derive_copy = if attr.fields.is_empty() {350		quote! {, Copy}351	} else {352		quote! {}353	};354355	Ok(quote! {356		#fun357358		#[doc(hidden)]359		#[allow(non_camel_case_types)]360		#[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]361		#vis struct #name {362			#(#fields)*363		}364		const _: () = {365			use ::jrsonnet_evaluator::{366				State, Val,367				function::{builtin::{Builtin, StaticBuiltin, ParamParse, ParamName, ParamDefault}, CallLocation, ArgsLike, parse::parse_builtin_call},368				Result, Context, typed::Typed,369				parser::Span, params,370			};371			params!(372				#(#params_desc)*373			);374375			#static_ext376			impl Builtin for #name377			where378				Self: 'static379			{380				fn name(&self) -> &str {381					stringify!(#name)382				}383				fn params(&self) -> &[ParamParse] {384					/// Safety: ParamParse contains IStr, which is thread-local, thus neither Send or Sync385					/// The result of this transmute can not outlive the thread, thus 'static here is equivalent to the386					/// nightly-only 'thread387					PARAMS.with(|p| unsafe { std::mem::transmute::<&[ParamParse], &'static [ParamParse]>(p.as_slice()) })388				}389				#[allow(unused_variables)]390				fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {391					let parsed = parse_builtin_call(ctx.clone(), self.params(), args, false)?;392393					let result: #result = #name(#(#pass)*);394					<_ as Typed>::into_result(result)395				}396				fn as_any(&self) -> &dyn ::std::any::Any {397					self398				}399			}400		};401	})402}403404#[derive(Default)]405#[allow(clippy::struct_excessive_bools)]406struct TypedAttr {407	rename: Option<String>,408	aliases: Vec<String>,409	flatten: bool,410	/// flatten(ok) strategy for flattened optionals411	/// field would be None in case of any parsing error (as in serde)412	flatten_ok: bool,413	// Should it be `field+:` instead of `field:`414	add: bool,415	// Should it be `field::` instead of `field:`416	hide: bool,417}418impl Parse for TypedAttr {419	fn parse(input: ParseStream) -> syn::Result<Self> {420		let mut out = Self::default();421		loop {422			let lookahead = input.lookahead1();423			if lookahead.peek(kw::rename) {424				input.parse::<kw::rename>()?;425				input.parse::<Token![=]>()?;426				let name = input.parse::<LitStr>()?;427				if out.rename.is_some() {428					return Err(Error::new(429						name.span(),430						"rename attribute may only be specified once",431					));432				}433				out.rename = Some(name.value());434			} else if lookahead.peek(kw::alias) {435				input.parse::<kw::alias>()?;436				input.parse::<Token![=]>()?;437				let alias = input.parse::<LitStr>()?;438				out.aliases.push(alias.value());439			} else if lookahead.peek(kw::flatten) {440				input.parse::<kw::flatten>()?;441				out.flatten = true;442				if input.peek(token::Paren) {443					let content;444					parenthesized!(content in input);445					let lookahead = content.lookahead1();446					if lookahead.peek(kw::ok) {447						content.parse::<kw::ok>()?;448						out.flatten_ok = true;449					} else {450						return Err(lookahead.error());451					}452				}453			} else if lookahead.peek(kw::add) {454				input.parse::<kw::add>()?;455				out.add = true;456			} else if lookahead.peek(kw::hide) {457				input.parse::<kw::hide>()?;458				out.hide = true;459			} else if input.is_empty() {460				break;461			} else {462				return Err(lookahead.error());463			}464			if input.peek(Token![,]) {465				input.parse::<Token![,]>()?;466			} else {467				break;468			}469		}470		Ok(out)471	}472}473474struct TypedField {475	attr: TypedAttr,476	ident: Ident,477	ty: Type,478	is_option: bool,479	is_lazy: bool,480}481impl TypedField {482	fn parse(field: &syn::Field) -> Result<Self> {483		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();484		let Some(ident) = field.ident.clone() else {485			return Err(Error::new(486				field.span(),487				"this field should appear in output object, but it has no visible name",488			));489		};490		let (is_option, ty) = extract_type_from_option(&field.ty)?491			.map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));492		if is_option && attr.flatten {493			if !attr.flatten_ok {494				return Err(Error::new(495					field.span(),496					"strategy should be set when flattening Option",497				));498			}499		} else if attr.flatten_ok {500			return Err(Error::new(501				field.span(),502				"flatten(ok) is only useable on optional fields",503			));504		}505506		let is_lazy = type_is_path(&ty, "Thunk").is_some();507508		Ok(Self {509			attr,510			ident,511			ty,512			is_option,513			is_lazy,514		})515	}516	/// None if this field is flattened in jsonnet output517	fn name(&self) -> Option<String> {518		if self.attr.flatten {519			return None;520		}521		Some(522			self.attr523				.rename524				.clone()525				.unwrap_or_else(|| self.ident.to_string()),526		)527	}528529	fn expand_field(&self) -> Option<TokenStream> {530		if self.is_option {531			return None;532		}533		let name = self.name()?;534		let ty = &self.ty;535		Some(quote! {536			(#name, <#ty as Typed>::TYPE)537		})538	}539540	fn expand_parse(&self) -> TokenStream {541		if self.is_option {542			self.expand_parse_optional()543		} else {544			self.expand_parse_mandatory()545		}546	}547548	fn expand_parse_optional(&self) -> TokenStream {549		let ident = &self.ident;550		let ty = &self.ty;551552		// optional flatten is handled in same way as serde553		if self.attr.flatten {554			return quote! {555				#ident: <#ty as TypedObj>::parse(&obj).ok(),556			};557		}558559		let name = self.name().unwrap();560		let aliases = &self.attr.aliases;561562		quote! {563			#ident: {564				let __value = if let Some(__v) = obj.get(#name.into())? {565					Some(__v)566				} #(else if let Some(__v) = obj.get(#aliases.into())? {567					Some(__v)568				})* else {569					None570				};571572				__value.map(<#ty as Typed>::from_untyped).transpose()?573			},574		}575	}576577	fn expand_parse_mandatory(&self) -> TokenStream {578		let ident = &self.ident;579		let ty = &self.ty;580581		// optional flatten is handled in same way as serde582		if self.attr.flatten {583			return quote! {584				#ident: <#ty as TypedObj>::parse(&obj)?,585			};586		}587588		let name = self.name().unwrap();589		let aliases = &self.attr.aliases;590591		let error_text = if aliases.is_empty() {592			// clippy does not understand name variable usage in quote! macro593			#[allow(clippy::redundant_clone)]594			name.clone()595		} else {596			format!("{name} (alias {})", aliases.join(", "))597		};598599		quote! {600			#ident: {601				let __value = if let Some(__v) = obj.get(#name.into())? {602					__v603				} #(else if let Some(__v) = obj.get(#aliases.into())? {604					__v605				})* else {606					return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());607				};608609				<#ty as Typed>::from_untyped(__value)?610			},611		}612	}613614	fn expand_serialize(&self) -> TokenStream {615		let ident = &self.ident;616		let ty = &self.ty;617		self.name().map_or_else(618			|| {619				if self.is_option {620					quote! {621						if let Some(value) = self.#ident {622							<#ty as TypedObj>::serialize(value, out)?;623						}624					}625				} else {626					quote! {627						<#ty as TypedObj>::serialize(self.#ident, out)?;628					}629				}630			},631			|name| {632				let hide = if self.attr.hide {633					quote! {.hide()}634				} else {635					quote! {}636				};637				let add = if self.attr.add {638					quote! {.add()}639				} else {640					quote! {}641				};642				let value = if self.is_lazy {643					quote! {644						out.field(#name)645							#hide646							#add647							.try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;648					}649				} else {650					quote! {651						out.field(#name)652							#hide653							#add654							.try_value(<#ty as Typed>::into_untyped(value)?)?;655					}656				};657				if self.is_option {658					quote! {659						if let Some(value) = self.#ident {660							#value661						}662					}663				} else {664					quote! {665						{666							let value = self.#ident;667							#value668						}669					}670				}671			},672		)673	}674}675676#[proc_macro_derive(Typed, attributes(typed))]677pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {678	let input = parse_macro_input!(item as DeriveInput);679680	match derive_typed_inner(input) {681		Ok(v) => v.into(),682		Err(e) => e.to_compile_error().into(),683	}684}685686fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {687	let syn::Data::Struct(data) = &input.data else {688		return Err(Error::new(input.span(), "only structs supported"));689	};690691	let ident = &input.ident;692	let fields = data693		.fields694		.iter()695		.map(TypedField::parse)696		.collect::<Result<Vec<_>>>()?;697698	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();699700	let typed = {701		let fields = fields702			.iter()703			.filter_map(TypedField::expand_field)704			.collect::<Vec<_>>();705		quote! {706			impl #impl_generics Typed for #ident #ty_generics #where_clause {707				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[708					#(#fields,)*709				]);710711				fn from_untyped(value: Val) -> JrResult<Self> {712					let obj = value.as_obj().expect("shape is correct");713					Self::parse(&obj)714				}715716				fn into_untyped(value: Self) -> JrResult<Val> {717					let mut out = ObjValueBuilder::new();718					value.serialize(&mut out)?;719					Ok(Val::Obj(out.build()))720				}721722			}723		}724	};725726	let fields_parse = fields.iter().map(TypedField::expand_parse);727	let fields_serialize = fields728		.iter()729		.map(TypedField::expand_serialize)730		.collect::<Vec<_>>();731732	Ok(quote! {733		const _: () = {734			use ::jrsonnet_evaluator::{735				typed::{ComplexValType, Typed, TypedObj, CheckType},736				Val, State,737				error::{ErrorKind, Result as JrResult},738				ObjValueBuilder, ObjValue,739			};740741			#typed742743			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {744				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {745					#(#fields_serialize)*746747					Ok(())748				}749				fn parse(obj: &ObjValue) -> JrResult<Self> {750					Ok(Self {751						#(#fields_parse)*752					})753				}754			}755		};756	})757}758759struct FormatInput {760	formatting: LitStr,761	arguments: Vec<Expr>,762}763impl Parse for FormatInput {764	fn parse(input: ParseStream) -> Result<Self> {765		let formatting = input.parse()?;766		let mut arguments = Vec::new();767768		while input.peek(Token![,]) {769			input.parse::<Token![,]>()?;770			if input.is_empty() {771				// Trailing comma772				break;773			}774			let expr = input.parse()?;775			arguments.push(expr);776		}777778		if !input.is_empty() {779			return Err(syn::Error::new(input.span(), "unexpected trailing input"));780		}781782		Ok(Self {783			formatting,784			arguments,785		})786	}787}788fn is_format_str(i: &str) -> bool {789	let mut is_plain = true;790	// -1 = {791	// +1 = }792	let mut is_bracket = 0i8;793	for ele in i.chars() {794		match ele {795			'{' if is_bracket == -1 => {796				is_bracket = 0;797			}798			'}' if is_bracket == -1 => {799				is_plain = false;800				break;801			}802			'}' if is_bracket == 1 => {803				is_bracket = 0;804			}805			'{' if is_bracket == 1 => {806				is_plain = false;807				break;808			}809			'{' => {810				is_bracket = -1;811			}812			'}' => {813				is_bracket = 1;814			}815			_ if is_bracket != 0 => {816				is_plain = false;817				break;818			}819			_ => {}820		}821	}822	!is_plain || is_bracket != 0823}824impl FormatInput {825	fn expand(self) -> TokenStream {826		let format = self.formatting;827		if is_format_str(&format.value()) {828			let args = self.arguments;829			quote! {830				::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))831			}832		} else {833			if let Some(first) = self.arguments.first() {834				return syn::Error::new(835					first.span(),836					"string has no formatting codes, it should not have the arguments",837				)838				.into_compile_error();839			}840			quote! {841				::jrsonnet_evaluator::IStr::from(#format)842			}843		}844	}845}846847/// `IStr` formatting helper848///849/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`850/// This macro looks for formatting codes in the input string, and uses851/// `format!()` only when necessary852#[proc_macro]853pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {854	let input = parse_macro_input!(input as FormatInput);855	input.expand().into()856}857858/// Create Thunk using closure syntax859#[proc_macro]860#[allow(non_snake_case)]861pub fn Thunk(input: proc_macro::TokenStream) -> proc_macro::TokenStream {862	let input = parse_macro_input!(input as ExprClosure);863864	let span = input.inputs.span();865	let move_check = input.capture.is_none().then(|| {866		quote_spanned! {span => {867			compile_error!("Thunk! needs to be called with move closure");868		}}869	});870871	let (env, closure, args) = syn_dissect_closure::split_env(input);872873	let trace_check = args.iter().map(|el| {874		let span = el.span();875		quote_spanned! {span => ::jrsonnet_evaluator::gc::assert_trace(&#el);}876	});877878	quote! {{879		#move_check880		#(#trace_check)*881		::jrsonnet_evaluator::Thunk::new(::jrsonnet_evaluator::val::MemoizedClosureThunk::new(#env, #closure))882	}}.into()883}