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

difftreelog

feat support hidden/added fields in derive(Typed)

Yaroslav Bolyukin2023-08-06parent: #895de28.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-macros/src/lib.rs
1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4	parenthesized,5	parse::{Parse, ParseStream},6	parse_macro_input,7	punctuated::Punctuated,8	spanned::Spanned,9	token::{self, Comma},10	Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11	PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16	Ident: PartialEq<I>,17{18	let attrs = attrs19		.iter()20		.filter(|a| a.path.is_ident(&ident))21		.collect::<Vec<_>>();22	if attrs.len() > 1 {23		return Err(Error::new(24			attrs[1].span(),25			"this attribute may be specified only once",26		));27	} else if attrs.is_empty() {28		return Ok(None);29	}30	let attr = attrs[0];31	let attr = attr.parse_args::<A>()?;3233	Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37	path.leading_colon.is_none()38		&& !path.segments.is_empty()39		&& path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43	match ty {44		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45			let args = &path.path.segments.iter().last().unwrap().arguments;46			Some(args)47		}48		_ => None,49	}50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53	let Some(args) = type_is_path(ty, "Option") else {54		return Ok(None);55	};56	// It should have only on angle-bracketed param ("<String>"):57	let PathArguments::AngleBracketed(params) = args else {58		return Err(Error::new(args.span(), "missing option generic"));59	};60	let generic_arg = params.args.iter().next().unwrap();61	// This argument must be a type:62	let GenericArgument::Type(ty) = generic_arg else {63		return Err(Error::new(64			generic_arg.span(),65			"option generic should be a type",66		));67	};68	Ok(Some(ty))69}7071struct Field {72	name: Ident,73	_colon: Token![:],74	ty: Type,75}76impl Parse for Field {77	fn parse(input: ParseStream) -> syn::Result<Self> {78		Ok(Self {79			name: input.parse()?,80			_colon: input.parse()?,81			ty: input.parse()?,82		})83	}84}8586mod kw {87	syn::custom_keyword!(fields);88	syn::custom_keyword!(rename);89	syn::custom_keyword!(flatten);90	syn::custom_keyword!(ok);91}9293struct EmptyAttr;94impl Parse for EmptyAttr {95	fn parse(_input: ParseStream) -> Result<Self> {96		Ok(Self)97	}98}99100struct BuiltinAttrs {101	fields: Vec<Field>,102}103impl Parse for BuiltinAttrs {104	fn parse(input: ParseStream) -> syn::Result<Self> {105		if input.is_empty() {106			return Ok(Self { fields: Vec::new() });107		}108		input.parse::<kw::fields>()?;109		let fields;110		parenthesized!(fields in input);111		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;112		Ok(Self {113			fields: p.into_iter().collect(),114		})115	}116}117118enum ArgInfo {119	Normal {120		ty: Box<Type>,121		is_option: bool,122		name: Option<String>,123		cfg_attrs: Vec<Attribute>,124	},125	Lazy {126		is_option: bool,127		name: Option<String>,128	},129	Context,130	Location,131	This,132}133134impl ArgInfo {135	fn parse(name: &str, arg: &FnArg) -> Result<Self> {136		let FnArg::Typed(arg) = arg else {137			unreachable!()138		};139		let ident = match &arg.pat as &Pat {140			Pat::Ident(i) => Some(i.ident.clone()),141			_ => None,142		};143		let ty = &arg.ty;144		if type_is_path(ty, "Context").is_some() {145			return Ok(Self::Context);146		} else if type_is_path(ty, "CallLocation").is_some() {147			return Ok(Self::Location);148		} else if type_is_path(ty, "Thunk").is_some() {149			return Ok(Self::Lazy {150				is_option: false,151				name: ident.map(|v| v.to_string()),152			});153		}154155		match ty as &Type {156			Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),157			_ => {}158		}159160		let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {161			if type_is_path(ty, "Thunk").is_some() {162				return Ok(Self::Lazy {163					is_option: true,164					name: ident.map(|v| v.to_string()),165				});166			}167168			(true, Box::new(ty.clone()))169		} else {170			(false, ty.clone())171		};172173		let cfg_attrs = arg174			.attrs175			.iter()176			.filter(|a| a.path.is_ident("cfg"))177			.cloned()178			.collect();179180		Ok(Self::Normal {181			ty,182			is_option,183			name: ident.map(|v| v.to_string()),184			cfg_attrs,185		})186	}187}188189#[proc_macro_attribute]190pub fn builtin(191	attr: proc_macro::TokenStream,192	item: proc_macro::TokenStream,193) -> proc_macro::TokenStream {194	let attr = parse_macro_input!(attr as BuiltinAttrs);195	let item_fn = item.clone();196	let item_fn: ItemFn = parse_macro_input!(item_fn);197198	match builtin_inner(attr, item_fn, item.into()) {199		Ok(v) => v.into(),200		Err(e) => e.into_compile_error().into(),201	}202}203204fn builtin_inner(205	attr: BuiltinAttrs,206	fun: ItemFn,207	item: proc_macro2::TokenStream,208) -> syn::Result<TokenStream> {209	let ReturnType::Type(_, result) = &fun.sig.output else {210		return Err(Error::new(211			fun.sig.span(),212			"builtin should return something",213		));214	};215216	let name = fun.sig.ident.to_string();217	let args = fun218		.sig219		.inputs220		.iter()221		.map(|arg| ArgInfo::parse(&name, arg))222		.collect::<Result<Vec<_>>>()?;223224	let params_desc = args.iter().flat_map(|a| match a {225		ArgInfo::Normal {226			is_option,227			name,228			cfg_attrs,229			..230		} => {231			let name = name232				.as_ref()233				.map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})234				.unwrap_or_else(|| quote! {None});235			Some(quote! {236				#(#cfg_attrs)*237				BuiltinParam {238					name: #name,239					has_default: #is_option,240				},241			})242		}243		ArgInfo::Lazy { is_option, name } => {244			let name = name245				.as_ref()246				.map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})247				.unwrap_or_else(|| quote! {None});248			Some(quote! {249				BuiltinParam {250					name: #name,251					has_default: #is_option,252				},253			})254		}255		ArgInfo::Context => None,256		ArgInfo::Location => None,257		ArgInfo::This => None,258	});259260	let mut id = 0usize;261	let pass = args262		.iter()263		.map(|a| match a {264			ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {265				let cid = id;266				id += 1;267				(quote! {#cid}, a)268			}269			ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {270				(quote! {compile_error!("should not use id")}, a)271			}272		})273		.map(|(id, a)| match a {274			ArgInfo::Normal {275				ty,276				is_option,277				name,278				cfg_attrs,279			} => {280				let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");281				let eval = quote! {jrsonnet_evaluator::State::push_description(282					|| format!("argument <{}> evaluation", #name),283					|| <#ty>::from_untyped(value.evaluate()?),284				)?};285				let value = if *is_option {286					quote! {if let Some(value) = &parsed[#id] {287						Some(#eval)288					} else {289						None290					},}291				} else {292					quote! {{293						let value = parsed[#id].as_ref().expect("args shape is checked");294						#eval295					},}296				};297				quote! {298					#(#cfg_attrs)*299					#value300				}301			}302			ArgInfo::Lazy { is_option, .. } => {303				if *is_option {304					quote! {if let Some(value) = &parsed[#id] {305						Some(value.clone())306					} else {307						None308					}}309				} else {310					quote! {311						parsed[#id].as_ref().expect("args shape is correct").clone(),312					}313				}314			}315			ArgInfo::Context => quote! {ctx.clone(),},316			ArgInfo::Location => quote! {location,},317			ArgInfo::This => quote! {self,},318		});319320	let fields = attr.fields.iter().map(|field| {321		let name = &field.name;322		let ty = &field.ty;323		quote! {324			pub #name: #ty,325		}326	});327328	let name = &fun.sig.ident;329	let vis = &fun.vis;330	let static_ext = if attr.fields.is_empty() {331		quote! {332			impl #name {333				pub const INST: &'static dyn StaticBuiltin = &#name {};334			}335			impl StaticBuiltin for #name {}336		}337	} else {338		quote! {}339	};340	let static_derive_copy = if attr.fields.is_empty() {341		quote! {, Copy}342	} else {343		quote! {}344	};345346	Ok(quote! {347		#item348349		#[doc(hidden)]350		#[allow(non_camel_case_types)]351		#[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]352		#vis struct #name {353			#(#fields)*354		}355		const _: () = {356			use ::jrsonnet_evaluator::{357				State, Val,358				function::{builtin::{Builtin, StaticBuiltin, BuiltinParam}, CallLocation, ArgsLike, parse::parse_builtin_call},359				error::Result, Context, typed::Typed,360				parser::ExprLocation,361			};362			const PARAMS: &'static [BuiltinParam] = &[363				#(#params_desc)*364			];365366			#static_ext367			impl Builtin for #name368			where369				Self: 'static370			{371				fn name(&self) -> &str {372					stringify!(#name)373				}374				fn params(&self) -> &[BuiltinParam] {375					PARAMS376				}377				fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {378					let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;379380					let result: #result = #name(#(#pass)*);381					<_ as Typed>::into_result(result)382				}383				fn as_any(&self) -> &dyn ::std::any::Any {384					self385				}386			}387		};388	})389}390391#[derive(Default)]392struct TypedAttr {393	rename: Option<String>,394	flatten: bool,395	/// flatten(ok) strategy for flattened optionals396	/// field would be None in case of any parsing error (as in serde)397	flatten_ok: bool,398}399impl Parse for TypedAttr {400	fn parse(input: ParseStream) -> syn::Result<Self> {401		let mut out = Self::default();402		loop {403			let lookahead = input.lookahead1();404			if lookahead.peek(kw::rename) {405				input.parse::<kw::rename>()?;406				input.parse::<Token![=]>()?;407				let name = input.parse::<LitStr>()?;408				if out.rename.is_some() {409					return Err(Error::new(410						name.span(),411						"rename attribute may only be specified once",412					));413				}414				out.rename = Some(name.value());415			} else if lookahead.peek(kw::flatten) {416				input.parse::<kw::flatten>()?;417				out.flatten = true;418				if input.peek(token::Paren) {419					let content;420					parenthesized!(content in input);421					let lookahead = content.lookahead1();422					if lookahead.peek(kw::ok) {423						content.parse::<kw::ok>()?;424						out.flatten_ok = true;425					} else {426						return Err(lookahead.error());427					}428				}429			} else if input.is_empty() {430				break;431			} else {432				return Err(lookahead.error());433			}434			if input.peek(Token![,]) {435				input.parse::<Token![,]>()?;436			} else {437				break;438			}439		}440		Ok(out)441	}442}443444struct TypedField {445	attr: TypedAttr,446	ident: Ident,447	ty: Type,448	is_option: bool,449}450impl TypedField {451	fn parse(field: &syn::Field) -> Result<Self> {452		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();453		let Some(ident) = field.ident.clone() else {454			return Err(Error::new(455				field.span(),456				"this field should appear in output object, but it has no visible name",457			));458		};459		let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {460			(true, ty.clone())461		} else {462			(false, field.ty.clone())463		};464		if is_option && attr.flatten {465			if !attr.flatten_ok {466				return Err(Error::new(467					field.span(),468					"strategy should be set when flattening Option",469				));470			}471		} else if attr.flatten_ok {472			return Err(Error::new(473				field.span(),474				"flatten(ok) is only useable on optional fields",475			));476		}477478		Ok(Self {479			attr,480			ident,481			ty,482			is_option,483		})484	}485	/// None if this field is flattened in jsonnet output486	fn name(&self) -> Option<String> {487		if self.attr.flatten {488			return None;489		}490		Some(491			self.attr492				.rename493				.clone()494				.unwrap_or_else(|| self.ident.to_string()),495		)496	}497498	fn expand_field(&self) -> Option<TokenStream> {499		if self.is_option {500			return None;501		}502		let name = self.name()?;503		let ty = &self.ty;504		Some(quote! {505			(#name, <#ty as Typed>::TYPE)506		})507	}508	fn expand_parse(&self) -> TokenStream {509		let ident = &self.ident;510		let ty = &self.ty;511		if self.attr.flatten {512			// optional flatten is handled in same way as serde513			return if self.is_option {514				quote! {515					#ident: <#ty as TypedObj>::parse(&obj).ok(),516				}517			} else {518				quote! {519					#ident: <#ty as TypedObj>::parse(&obj)?,520				}521			};522		};523524		let name = self.name().unwrap();525		let value = if self.is_option {526			quote! {527				if let Some(value) = obj.get(#name.into())? {528					Some(<#ty as Typed>::from_untyped(value)?)529				} else {530					None531				}532			}533		} else {534			quote! {535				<#ty as Typed>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?536			}537		};538539		quote! {540			#ident: #value,541		}542	}543	fn expand_serialize(&self) -> Result<TokenStream> {544		let ident = &self.ident;545		let ty = &self.ty;546		Ok(if let Some(name) = self.name() {547			if self.is_option {548				quote! {549					if let Some(value) = self.#ident {550						out.member(#name.into()).value(<#ty as Typed>::into_untyped(value)?)?;551					}552				}553			} else {554				quote! {555					out.member(#name.into()).value(<#ty as Typed>::into_untyped(self.#ident)?)?;556				}557			}558		} else if self.is_option {559			quote! {560				if let Some(value) = self.#ident {561					<#ty as TypedObj>::serialize(value, out)?;562				}563			}564		} else {565			quote! {566				<#ty as TypedObj>::serialize(self.#ident, out)?;567			}568		})569	}570}571572#[proc_macro_derive(Typed, attributes(typed))]573pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {574	let input = parse_macro_input!(item as DeriveInput);575576	match derive_typed_inner(input) {577		Ok(v) => v.into(),578		Err(e) => e.to_compile_error().into(),579	}580}581582fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {583	let syn::Data::Struct(data) = &input.data else {584		return Err(Error::new(input.span(), "only structs supported"));585	};586587	let ident = &input.ident;588	let fields = data589		.fields590		.iter()591		.map(TypedField::parse)592		.collect::<Result<Vec<_>>>()?;593594	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();595596	let typed = {597		let fields = fields598			.iter()599			.flat_map(TypedField::expand_field)600			.collect::<Vec<_>>();601		quote! {602			impl #impl_generics Typed for #ident #ty_generics #where_clause {603				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[604					#(#fields,)*605				]);606607				fn from_untyped(value: Val) -> JrResult<Self> {608					let obj = value.as_obj().expect("shape is correct");609					Self::parse(&obj)610				}611612				fn into_untyped(value: Self) -> JrResult<Val> {613					let mut out = ObjValueBuilder::new();614					value.serialize(&mut out)?;615					Ok(Val::Obj(out.build()))616				}617618			}619		}620	};621622	let fields_parse = fields.iter().map(TypedField::expand_parse);623	let fields_serialize = fields624		.iter()625		.map(TypedField::expand_serialize)626		.collect::<Result<Vec<_>>>()?;627628	Ok(quote! {629		const _: () = {630			use ::jrsonnet_evaluator::{631				typed::{ComplexValType, Typed, TypedObj, CheckType},632				Val, State,633				error::{ErrorKind, Result as JrResult},634				ObjValueBuilder, ObjValue,635			};636637			#typed638639			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {640				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {641					#(#fields_serialize)*642643					Ok(())644				}645				fn parse(obj: &ObjValue) -> JrResult<Self> {646					Ok(Self {647						#(#fields_parse)*648					})649				}650			}651		};652	})653}
after · crates/jrsonnet-macros/src/lib.rs
1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4	parenthesized,5	parse::{Parse, ParseStream},6	parse_macro_input,7	punctuated::Punctuated,8	spanned::Spanned,9	token::{self, Comma},10	Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11	PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16	Ident: PartialEq<I>,17{18	let attrs = attrs19		.iter()20		.filter(|a| a.path.is_ident(&ident))21		.collect::<Vec<_>>();22	if attrs.len() > 1 {23		return Err(Error::new(24			attrs[1].span(),25			"this attribute may be specified only once",26		));27	} else if attrs.is_empty() {28		return Ok(None);29	}30	let attr = attrs[0];31	let attr = attr.parse_args::<A>()?;3233	Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37	path.leading_colon.is_none()38		&& !path.segments.is_empty()39		&& path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43	match ty {44		Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45			let args = &path.path.segments.iter().last().unwrap().arguments;46			Some(args)47		}48		_ => None,49	}50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53	let Some(args) = type_is_path(ty, "Option") else {54		return Ok(None);55	};56	// It should have only on angle-bracketed param ("<String>"):57	let PathArguments::AngleBracketed(params) = args else {58		return Err(Error::new(args.span(), "missing option generic"));59	};60	let generic_arg = params.args.iter().next().unwrap();61	// This argument must be a type:62	let GenericArgument::Type(ty) = generic_arg else {63		return Err(Error::new(64			generic_arg.span(),65			"option generic should be a type",66		));67	};68	Ok(Some(ty))69}7071struct Field {72	name: Ident,73	_colon: Token![:],74	ty: Type,75}76impl Parse for Field {77	fn parse(input: ParseStream) -> syn::Result<Self> {78		Ok(Self {79			name: input.parse()?,80			_colon: input.parse()?,81			ty: input.parse()?,82		})83	}84}8586mod kw {87	syn::custom_keyword!(fields);88	syn::custom_keyword!(rename);89	syn::custom_keyword!(flatten);90	syn::custom_keyword!(add);91	syn::custom_keyword!(hide);92	syn::custom_keyword!(ok);93}9495struct EmptyAttr;96impl Parse for EmptyAttr {97	fn parse(_input: ParseStream) -> Result<Self> {98		Ok(Self)99	}100}101102struct BuiltinAttrs {103	fields: Vec<Field>,104}105impl Parse for BuiltinAttrs {106	fn parse(input: ParseStream) -> syn::Result<Self> {107		if input.is_empty() {108			return Ok(Self { fields: Vec::new() });109		}110		input.parse::<kw::fields>()?;111		let fields;112		parenthesized!(fields in input);113		let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;114		Ok(Self {115			fields: p.into_iter().collect(),116		})117	}118}119120enum ArgInfo {121	Normal {122		ty: Box<Type>,123		is_option: bool,124		name: Option<String>,125		cfg_attrs: Vec<Attribute>,126	},127	Lazy {128		is_option: bool,129		name: Option<String>,130	},131	Context,132	Location,133	This,134}135136impl ArgInfo {137	fn parse(name: &str, arg: &FnArg) -> Result<Self> {138		let FnArg::Typed(arg) = arg else {139			unreachable!()140		};141		let ident = match &arg.pat as &Pat {142			Pat::Ident(i) => Some(i.ident.clone()),143			_ => None,144		};145		let ty = &arg.ty;146		if type_is_path(ty, "Context").is_some() {147			return Ok(Self::Context);148		} else if type_is_path(ty, "CallLocation").is_some() {149			return Ok(Self::Location);150		} else if type_is_path(ty, "Thunk").is_some() {151			return Ok(Self::Lazy {152				is_option: false,153				name: ident.map(|v| v.to_string()),154			});155		}156157		match ty as &Type {158			Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),159			_ => {}160		}161162		let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {163			if type_is_path(ty, "Thunk").is_some() {164				return Ok(Self::Lazy {165					is_option: true,166					name: ident.map(|v| v.to_string()),167				});168			}169170			(true, Box::new(ty.clone()))171		} else {172			(false, ty.clone())173		};174175		let cfg_attrs = arg176			.attrs177			.iter()178			.filter(|a| a.path.is_ident("cfg"))179			.cloned()180			.collect();181182		Ok(Self::Normal {183			ty,184			is_option,185			name: ident.map(|v| v.to_string()),186			cfg_attrs,187		})188	}189}190191#[proc_macro_attribute]192pub fn builtin(193	attr: proc_macro::TokenStream,194	item: proc_macro::TokenStream,195) -> proc_macro::TokenStream {196	let attr = parse_macro_input!(attr as BuiltinAttrs);197	let item_fn = item.clone();198	let item_fn: ItemFn = parse_macro_input!(item_fn);199200	match builtin_inner(attr, item_fn, item.into()) {201		Ok(v) => v.into(),202		Err(e) => e.into_compile_error().into(),203	}204}205206fn builtin_inner(207	attr: BuiltinAttrs,208	fun: ItemFn,209	item: proc_macro2::TokenStream,210) -> syn::Result<TokenStream> {211	let ReturnType::Type(_, result) = &fun.sig.output else {212		return Err(Error::new(213			fun.sig.span(),214			"builtin should return something",215		));216	};217218	let name = fun.sig.ident.to_string();219	let args = fun220		.sig221		.inputs222		.iter()223		.map(|arg| ArgInfo::parse(&name, arg))224		.collect::<Result<Vec<_>>>()?;225226	let params_desc = args.iter().flat_map(|a| match a {227		ArgInfo::Normal {228			is_option,229			name,230			cfg_attrs,231			..232		} => {233			let name = name234				.as_ref()235				.map(|n| quote! {ParamName::new_static(#n)})236				.unwrap_or_else(|| quote! {None});237			Some(quote! {238				#(#cfg_attrs)*239				BuiltinParam::new(#name, #is_option),240			})241		}242		ArgInfo::Lazy { is_option, name } => {243			let name = name244				.as_ref()245				.map(|n| quote! {ParamName::new_static(#n)})246				.unwrap_or_else(|| quote! {None});247			Some(quote! {248				BuiltinParam::new(#name, #is_option),249			})250		}251		ArgInfo::Context => None,252		ArgInfo::Location => None,253		ArgInfo::This => None,254	});255256	let mut id = 0usize;257	let pass = args258		.iter()259		.map(|a| match a {260			ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {261				let cid = id;262				id += 1;263				(quote! {#cid}, a)264			}265			ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {266				(quote! {compile_error!("should not use id")}, a)267			}268		})269		.map(|(id, a)| match a {270			ArgInfo::Normal {271				ty,272				is_option,273				name,274				cfg_attrs,275			} => {276				let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");277				let eval = quote! {jrsonnet_evaluator::State::push_description(278					|| format!("argument <{}> evaluation", #name),279					|| <#ty>::from_untyped(value.evaluate()?),280				)?};281				let value = if *is_option {282					quote! {if let Some(value) = &parsed[#id] {283						Some(#eval)284					} else {285						None286					},}287				} else {288					quote! {{289						let value = parsed[#id].as_ref().expect("args shape is checked");290						#eval291					},}292				};293				quote! {294					#(#cfg_attrs)*295					#value296				}297			}298			ArgInfo::Lazy { is_option, .. } => {299				if *is_option {300					quote! {if let Some(value) = &parsed[#id] {301						Some(value.clone())302					} else {303						None304					}}305				} else {306					quote! {307						parsed[#id].as_ref().expect("args shape is correct").clone(),308					}309				}310			}311			ArgInfo::Context => quote! {ctx.clone(),},312			ArgInfo::Location => quote! {location,},313			ArgInfo::This => quote! {self,},314		});315316	let fields = attr.fields.iter().map(|field| {317		let name = &field.name;318		let ty = &field.ty;319		quote! {320			pub #name: #ty,321		}322	});323324	let name = &fun.sig.ident;325	let vis = &fun.vis;326	let static_ext = if attr.fields.is_empty() {327		quote! {328			impl #name {329				pub const INST: &'static dyn StaticBuiltin = &#name {};330			}331			impl StaticBuiltin for #name {}332		}333	} else {334		quote! {}335	};336	let static_derive_copy = if attr.fields.is_empty() {337		quote! {, Copy}338	} else {339		quote! {}340	};341342	Ok(quote! {343		#item344345		#[doc(hidden)]346		#[allow(non_camel_case_types)]347		#[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]348		#vis struct #name {349			#(#fields)*350		}351		const _: () = {352			use ::jrsonnet_evaluator::{353				State, Val,354				function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName}, CallLocation, ArgsLike, parse::parse_builtin_call},355				error::Result, Context, typed::Typed,356				parser::ExprLocation,357			};358			const PARAMS: &'static [BuiltinParam] = &[359				#(#params_desc)*360			];361362			#static_ext363			impl Builtin for #name364			where365				Self: 'static366			{367				fn name(&self) -> &str {368					stringify!(#name)369				}370				fn params(&self) -> &[BuiltinParam] {371					PARAMS372				}373				fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {374					let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;375376					let result: #result = #name(#(#pass)*);377					<_ as Typed>::into_result(result)378				}379				fn as_any(&self) -> &dyn ::std::any::Any {380					self381				}382			}383		};384	})385}386387#[derive(Default)]388struct TypedAttr {389	rename: Option<String>,390	flatten: bool,391	/// flatten(ok) strategy for flattened optionals392	/// field would be None in case of any parsing error (as in serde)393	flatten_ok: bool,394	// Should it be `field+:` instead of `field:`395	add: bool,396	// Should it be `field::` instead of `field:`397	hide: bool,398}399impl Parse for TypedAttr {400	fn parse(input: ParseStream) -> syn::Result<Self> {401		let mut out = Self::default();402		loop {403			let lookahead = input.lookahead1();404			if lookahead.peek(kw::rename) {405				input.parse::<kw::rename>()?;406				input.parse::<Token![=]>()?;407				let name = input.parse::<LitStr>()?;408				if out.rename.is_some() {409					return Err(Error::new(410						name.span(),411						"rename attribute may only be specified once",412					));413				}414				out.rename = Some(name.value());415			} else if lookahead.peek(kw::flatten) {416				input.parse::<kw::flatten>()?;417				out.flatten = true;418				if input.peek(token::Paren) {419					let content;420					parenthesized!(content in input);421					let lookahead = content.lookahead1();422					if lookahead.peek(kw::ok) {423						content.parse::<kw::ok>()?;424						out.flatten_ok = true;425					} else {426						return Err(lookahead.error());427					}428				}429			} else if lookahead.peek(kw::add) {430				input.parse::<kw::add>()?;431				out.add = true;432			} else if lookahead.peek(kw::hide) {433				input.parse::<kw::hide>()?;434				out.hide = true;435			} else if input.is_empty() {436				break;437			} else {438				return Err(lookahead.error());439			}440			if input.peek(Token![,]) {441				input.parse::<Token![,]>()?;442			} else {443				break;444			}445		}446		Ok(out)447	}448}449450struct TypedField {451	attr: TypedAttr,452	ident: Ident,453	ty: Type,454	is_option: bool,455}456impl TypedField {457	fn parse(field: &syn::Field) -> Result<Self> {458		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();459		let Some(ident) = field.ident.clone() else {460			return Err(Error::new(461				field.span(),462				"this field should appear in output object, but it has no visible name",463			));464		};465		let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {466			(true, ty.clone())467		} else {468			(false, field.ty.clone())469		};470		if is_option && attr.flatten {471			if !attr.flatten_ok {472				return Err(Error::new(473					field.span(),474					"strategy should be set when flattening Option",475				));476			}477		} else if attr.flatten_ok {478			return Err(Error::new(479				field.span(),480				"flatten(ok) is only useable on optional fields",481			));482		}483484		Ok(Self {485			attr,486			ident,487			ty,488			is_option,489		})490	}491	/// None if this field is flattened in jsonnet output492	fn name(&self) -> Option<String> {493		if self.attr.flatten {494			return None;495		}496		Some(497			self.attr498				.rename499				.clone()500				.unwrap_or_else(|| self.ident.to_string()),501		)502	}503504	fn expand_field(&self) -> Option<TokenStream> {505		if self.is_option {506			return None;507		}508		let name = self.name()?;509		let ty = &self.ty;510		Some(quote! {511			(#name, <#ty as Typed>::TYPE)512		})513	}514	fn expand_parse(&self) -> TokenStream {515		let ident = &self.ident;516		let ty = &self.ty;517		if self.attr.flatten {518			// optional flatten is handled in same way as serde519			return if self.is_option {520				quote! {521					#ident: <#ty as TypedObj>::parse(&obj).ok(),522				}523			} else {524				quote! {525					#ident: <#ty as TypedObj>::parse(&obj)?,526				}527			};528		};529530		let name = self.name().unwrap();531		let value = if self.is_option {532			quote! {533				if let Some(value) = obj.get(#name.into())? {534					Some(<#ty as Typed>::from_untyped(value)?)535				} else {536					None537				}538			}539		} else {540			quote! {541				<#ty as Typed>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?542			}543		};544545		quote! {546			#ident: #value,547		}548	}549	fn expand_serialize(&self) -> Result<TokenStream> {550		let ident = &self.ident;551		let ty = &self.ty;552		Ok(if let Some(name) = self.name() {553			let hide = if self.attr.hide {554				quote! {.hide()}555			} else {556				quote! {}557			};558			let add = if self.attr.add {559				quote! {.add()}560			} else {561				quote! {}562			};563			if self.is_option {564				quote! {565					if let Some(value) = self.#ident {566						out.member(#name.into())567							#hide568							#add569							.value(<#ty as Typed>::into_untyped(value)?)?;570					}571				}572			} else {573				quote! {574					out.member(#name.into())575						#hide576						#add577						.value(<#ty as Typed>::into_untyped(self.#ident)?)?;578				}579			}580		} else if self.is_option {581			quote! {582				if let Some(value) = self.#ident {583					<#ty as TypedObj>::serialize(value, out)?;584				}585			}586		} else {587			quote! {588				<#ty as TypedObj>::serialize(self.#ident, out)?;589			}590		})591	}592}593594#[proc_macro_derive(Typed, attributes(typed))]595pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {596	let input = parse_macro_input!(item as DeriveInput);597598	match derive_typed_inner(input) {599		Ok(v) => v.into(),600		Err(e) => e.to_compile_error().into(),601	}602}603604fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {605	let syn::Data::Struct(data) = &input.data else {606		return Err(Error::new(input.span(), "only structs supported"));607	};608609	let ident = &input.ident;610	let fields = data611		.fields612		.iter()613		.map(TypedField::parse)614		.collect::<Result<Vec<_>>>()?;615616	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();617618	let typed = {619		let fields = fields620			.iter()621			.flat_map(TypedField::expand_field)622			.collect::<Vec<_>>();623		quote! {624			impl #impl_generics Typed for #ident #ty_generics #where_clause {625				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[626					#(#fields,)*627				]);628629				fn from_untyped(value: Val) -> JrResult<Self> {630					let obj = value.as_obj().expect("shape is correct");631					Self::parse(&obj)632				}633634				fn into_untyped(value: Self) -> JrResult<Val> {635					let mut out = ObjValueBuilder::new();636					value.serialize(&mut out)?;637					Ok(Val::Obj(out.build()))638				}639640			}641		}642	};643644	let fields_parse = fields.iter().map(TypedField::expand_parse);645	let fields_serialize = fields646		.iter()647		.map(TypedField::expand_serialize)648		.collect::<Result<Vec<_>>>()?;649650	Ok(quote! {651		const _: () = {652			use ::jrsonnet_evaluator::{653				typed::{ComplexValType, Typed, TypedObj, CheckType},654				Val, State,655				error::{ErrorKind, Result as JrResult},656				ObjValueBuilder, ObjValue,657			};658659			#typed660661			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {662				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {663					#(#fields_serialize)*664665					Ok(())666				}667				fn parse(obj: &ObjValue) -> JrResult<Self> {668					Ok(Self {669						#(#fields_parse)*670					})671				}672			}673		};674	})675}