difftreelog
feat support hidden/added fields in derive(Typed)
in: master
1 file changed
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth1use 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}