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

difftreelog

source

crates/jrsonnet-macros/src/lib.rs14.5 KiBsourcehistory
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: ItemFn = parse_macro_input!(item);196197	match builtin_inner(attr, item) {198		Ok(v) => v.into(),199		Err(e) => e.into_compile_error().into(),200	}201}202203fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {204	let ReturnType::Type(_, result) = &fun.sig.output else {205		return Err(Error::new(206			fun.sig.span(),207			"builtin should return something",208		))209	};210211	let Some(args) = type_is_path(result, "Result") else {212		return Err(Error::new(result.span(), "return value should be result"));213214	};215	let PathArguments::AngleBracketed(params) = args else {216		return Err(Error::new(args.span(), "missing result generic"));217	};218	let generic_arg = params.args.iter().next().unwrap();219	// This argument must be a type:220	let GenericArgument::Type(result_inner) = generic_arg else {221		return Err(Error::new(222			generic_arg.span(),223			"option generic should be a type",224		))225	};226227	let name = fun.sig.ident.to_string();228	let args = fun229		.sig230		.inputs231		.iter()232		.map(|arg| ArgInfo::parse(&name, arg))233		.collect::<Result<Vec<_>>>()?;234235	let params_desc = args.iter().flat_map(|a| match a {236		ArgInfo::Normal {237			is_option,238			name,239			cfg_attrs,240			..241		} => {242			let name = name243				.as_ref()244				.map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})245				.unwrap_or_else(|| quote! {None});246			Some(quote! {247				#(#cfg_attrs)*248				BuiltinParam {249					name: #name,250					has_default: #is_option,251				},252			})253		}254		ArgInfo::Lazy { is_option, name } => {255			let name = name256				.as_ref()257				.map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})258				.unwrap_or_else(|| quote! {None});259			Some(quote! {260				BuiltinParam {261					name: #name,262					has_default: #is_option,263				},264			})265		}266		ArgInfo::Context => None,267		ArgInfo::Location => None,268		ArgInfo::This => None,269	});270271	let mut id = 0usize;272	let pass = args273		.iter()274		.map(|a| match a {275			ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {276				let cid = id;277				id += 1;278				(quote! {#cid}, a)279			}280			ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {281				(quote! {compile_error!("should not use id")}, a)282			}283		})284		.map(|(id, a)| match a {285			ArgInfo::Normal {286				ty,287				is_option,288				name,289				cfg_attrs,290			} => {291				let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");292				let eval = quote! {jrsonnet_evaluator::State::push_description(293					|| format!("argument <{}> evaluation", #name),294					|| <#ty>::from_untyped(value.evaluate()?),295				)?};296				let value = if *is_option {297					quote! {if let Some(value) = &parsed[#id] {298						Some(#eval)299					} else {300						None301					},}302				} else {303					quote! {{304						let value = parsed[#id].as_ref().expect("args shape is checked");305						#eval306					},}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 name = &field.name;333		let ty = &field.ty;334		quote! {335			pub #name: #ty,336		}337	});338339	let name = &fun.sig.ident;340	let vis = &fun.vis;341	let static_ext = if attr.fields.is_empty() {342		quote! {343			impl #name {344				pub const INST: &'static dyn StaticBuiltin = &#name {};345			}346			impl StaticBuiltin for #name {}347		}348	} else {349		quote! {}350	};351	let static_derive_copy = if attr.fields.is_empty() {352		quote! {, Copy}353	} else {354		quote! {}355	};356357	Ok(quote! {358		#fun359		#[doc(hidden)]360		#[allow(non_camel_case_types)]361		#[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]362		#vis struct #name {363			#(#fields)*364		}365		const _: () = {366			use ::jrsonnet_evaluator::{367				State, Val,368				function::{builtin::{Builtin, StaticBuiltin, BuiltinParam}, CallLocation, ArgsLike, parse::parse_builtin_call},369				error::Result, Context, typed::Typed,370				parser::ExprLocation,371			};372			const PARAMS: &'static [BuiltinParam] = &[373				#(#params_desc)*374			];375376			#static_ext377			impl Builtin for #name378			where379				Self: 'static380			{381				fn name(&self) -> &str {382					stringify!(#name)383				}384				fn params(&self) -> &[BuiltinParam] {385					PARAMS386				}387				fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {388					let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;389390					let result: #result = #name(#(#pass)*);391					let result = result?;392					<#result_inner>::into_untyped(result)393				}394			}395		};396	})397}398399#[derive(Default)]400struct TypedAttr {401	rename: Option<String>,402	flatten: bool,403	/// flatten(ok) strategy for flattened optionals404	/// field would be None in case of any parsing error (as in serde)405	flatten_ok: bool,406}407impl Parse for TypedAttr {408	fn parse(input: ParseStream) -> syn::Result<Self> {409		let mut out = Self::default();410		loop {411			let lookahead = input.lookahead1();412			if lookahead.peek(kw::rename) {413				input.parse::<kw::rename>()?;414				input.parse::<Token![=]>()?;415				let name = input.parse::<LitStr>()?;416				if out.rename.is_some() {417					return Err(Error::new(418						name.span(),419						"rename attribute may only be specified once",420					));421				}422				out.rename = Some(name.value());423			} else if lookahead.peek(kw::flatten) {424				input.parse::<kw::flatten>()?;425				out.flatten = true;426				if input.peek(token::Paren) {427					let content;428					parenthesized!(content in input);429					let lookahead = content.lookahead1();430					if lookahead.peek(kw::ok) {431						content.parse::<kw::ok>()?;432						out.flatten_ok = true;433					} else {434						return Err(lookahead.error());435					}436				}437			} else if input.is_empty() {438				break;439			} else {440				return Err(lookahead.error());441			}442			if input.peek(Token![,]) {443				input.parse::<Token![,]>()?;444			} else {445				break;446			}447		}448		Ok(out)449	}450}451452struct TypedField {453	attr: TypedAttr,454	ident: Ident,455	ty: Type,456	is_option: bool,457}458impl TypedField {459	fn parse(field: &syn::Field) -> Result<Self> {460		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();461		let Some(ident) = field.ident.clone() else {462			return Err(Error::new(463				field.span(),464				"this field should appear in output object, but it has no visible name",465			));466		};467		let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {468			(true, ty.clone())469		} else {470			(false, field.ty.clone())471		};472		if is_option && attr.flatten {473			if !attr.flatten_ok {474				return Err(Error::new(475					field.span(),476					"strategy should be set when flattening Option",477				));478			}479		} else if attr.flatten_ok {480			return Err(Error::new(481				field.span(),482				"flatten(ok) is only useable on optional fields",483			));484		}485486		Ok(Self {487			attr,488			ident,489			ty,490			is_option,491		})492	}493	/// None if this field is flattened in jsonnet output494	fn name(&self) -> Option<String> {495		if self.attr.flatten {496			return None;497		}498		Some(499			self.attr500				.rename501				.clone()502				.unwrap_or_else(|| self.ident.to_string()),503		)504	}505506	fn expand_field(&self) -> Option<TokenStream> {507		if self.is_option {508			return None;509		}510		let name = self.name()?;511		let ty = &self.ty;512		Some(quote! {513			(#name, <#ty>::TYPE)514		})515	}516	fn expand_parse(&self) -> TokenStream {517		let ident = &self.ident;518		let ty = &self.ty;519		if self.attr.flatten {520			// optional flatten is handled in same way as serde521			return if self.is_option {522				quote! {523					#ident: <#ty>::parse(&obj).ok(),524				}525			} else {526				quote! {527					#ident: <#ty>::parse(&obj)?,528				}529			};530		};531532		let name = self.name().unwrap();533		let value = if self.is_option {534			quote! {535				if let Some(value) = obj.get(#name.into())? {536					Some(<#ty>::from_untyped(value)?)537				} else {538					None539				}540			}541		} else {542			quote! {543				<#ty>::from_untyped(obj.get(#name.into())?.ok_or_else(|| Error::NoSuchField(#name.into(), vec![]))?)?544			}545		};546547		quote! {548			#ident: #value,549		}550	}551	fn expand_serialize(&self) -> Result<TokenStream> {552		let ident = &self.ident;553		let ty = &self.ty;554		Ok(if let Some(name) = self.name() {555			if self.is_option {556				quote! {557					if let Some(value) = self.#ident {558						out.member(#name.into()).value(<#ty>::into_untyped(value)?)?;559					}560				}561			} else {562				quote! {563					out.member(#name.into()).value(<#ty>::into_untyped(self.#ident)?)?;564				}565			}566		} else if self.is_option {567			quote! {568				if let Some(value) = self.#ident {569					value.serialize(out)?;570				}571			}572		} else {573			quote! {574				self.#ident.serialize(out)?;575			}576		})577	}578}579580#[proc_macro_derive(Typed, attributes(typed))]581pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {582	let input = parse_macro_input!(item as DeriveInput);583584	match derive_typed_inner(input) {585		Ok(v) => v.into(),586		Err(e) => e.to_compile_error().into(),587	}588}589590fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {591	let syn::Data::Struct(data) = &input.data else {592		return Err(Error::new(input.span(), "only structs supported"));593	};594595	let ident = &input.ident;596	let fields = data597		.fields598		.iter()599		.map(TypedField::parse)600		.collect::<Result<Vec<_>>>()?;601602	let typed = {603		let fields = fields604			.iter()605			.flat_map(TypedField::expand_field)606			.collect::<Vec<_>>();607		let len = fields.len();608		quote! {609			const ITEMS: [(&'static str, &'static ComplexValType); #len] = [610				#(#fields,)*611			];612			impl Typed for #ident {613				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);614615				fn from_untyped(value: Val) -> Result<Self> {616					let obj = value.as_obj().expect("shape is correct");617					Self::parse(&obj)618				}619620				fn into_untyped(value: Self) -> Result<Val> {621					let mut out = ObjValueBuilder::new();622					value.serialize(&mut out)?;623					Ok(Val::Obj(out.build()))624				}625626			}627		}628	};629630	let fields_parse = fields.iter().map(TypedField::expand_parse);631	let fields_serialize = fields632		.iter()633		.map(TypedField::expand_serialize)634		.collect::<Result<Vec<_>>>()?;635636	Ok(quote! {637		const _: () = {638			use ::jrsonnet_evaluator::{639				typed::{ComplexValType, Typed, TypedObj, CheckType},640				Val, State,641				error::{LocError, Error, Result},642				ObjValueBuilder, ObjValue,643			};644645			#typed646647			impl TypedObj for #ident {648				fn serialize(self, out: &mut ObjValueBuilder) -> Result<(), LocError> {649					#(#fields_serialize)*650651					Ok(())652				}653				fn parse(obj: &ObjValue) -> Result<Self, LocError> {654					Ok(Self {655						#(#fields_parse)*656					})657				}658			}659		};660	})661}