difftreelog
feat builtin field attributes
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}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 attrs: Vec<Attribute>,73 name: Ident,74 _colon: Token![:],75 ty: Type,76}77impl Parse for Field {78 fn parse(input: ParseStream) -> syn::Result<Self> {79 Ok(Self {80 attrs: input.call(Attribute::parse_outer)?,81 name: input.parse()?,82 _colon: input.parse()?,83 ty: input.parse()?,84 })85 }86}8788mod kw {89 syn::custom_keyword!(fields);90 syn::custom_keyword!(rename);91 syn::custom_keyword!(flatten);92 syn::custom_keyword!(add);93 syn::custom_keyword!(hide);94 syn::custom_keyword!(ok);95}9697struct EmptyAttr;98impl Parse for EmptyAttr {99 fn parse(_input: ParseStream) -> Result<Self> {100 Ok(Self)101 }102}103104struct BuiltinAttrs {105 fields: Vec<Field>,106}107impl Parse for BuiltinAttrs {108 fn parse(input: ParseStream) -> syn::Result<Self> {109 if input.is_empty() {110 return Ok(Self { fields: Vec::new() });111 }112 input.parse::<kw::fields>()?;113 let fields;114 parenthesized!(fields in input);115 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;116 Ok(Self {117 fields: p.into_iter().collect(),118 })119 }120}121122enum ArgInfo {123 Normal {124 ty: Box<Type>,125 is_option: bool,126 name: Option<String>,127 cfg_attrs: Vec<Attribute>,128 },129 Lazy {130 is_option: bool,131 name: Option<String>,132 },133 Context,134 Location,135 This,136}137138impl ArgInfo {139 fn parse(name: &str, arg: &FnArg) -> Result<Self> {140 let FnArg::Typed(arg) = arg else {141 unreachable!()142 };143 let ident = match &arg.pat as &Pat {144 Pat::Ident(i) => Some(i.ident.clone()),145 _ => None,146 };147 let ty = &arg.ty;148 if type_is_path(ty, "Context").is_some() {149 return Ok(Self::Context);150 } else if type_is_path(ty, "CallLocation").is_some() {151 return Ok(Self::Location);152 } else if type_is_path(ty, "Thunk").is_some() {153 return Ok(Self::Lazy {154 is_option: false,155 name: ident.map(|v| v.to_string()),156 });157 }158159 match ty as &Type {160 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),161 _ => {}162 }163164 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {165 if type_is_path(ty, "Thunk").is_some() {166 return Ok(Self::Lazy {167 is_option: true,168 name: ident.map(|v| v.to_string()),169 });170 }171172 (true, Box::new(ty.clone()))173 } else {174 (false, ty.clone())175 };176177 let cfg_attrs = arg178 .attrs179 .iter()180 .filter(|a| a.path.is_ident("cfg"))181 .cloned()182 .collect();183184 Ok(Self::Normal {185 ty,186 is_option,187 name: ident.map(|v| v.to_string()),188 cfg_attrs,189 })190 }191}192193#[proc_macro_attribute]194pub fn builtin(195 attr: proc_macro::TokenStream,196 item: proc_macro::TokenStream,197) -> proc_macro::TokenStream {198 let attr = parse_macro_input!(attr as BuiltinAttrs);199 let item_fn = item.clone();200 let item_fn: ItemFn = parse_macro_input!(item_fn);201202 match builtin_inner(attr, item_fn, item.into()) {203 Ok(v) => v.into(),204 Err(e) => e.into_compile_error().into(),205 }206}207208fn builtin_inner(209 attr: BuiltinAttrs,210 fun: ItemFn,211 item: proc_macro2::TokenStream,212) -> syn::Result<TokenStream> {213 let ReturnType::Type(_, result) = &fun.sig.output else {214 return Err(Error::new(215 fun.sig.span(),216 "builtin should return something",217 ));218 };219220 let name = fun.sig.ident.to_string();221 let args = fun222 .sig223 .inputs224 .iter()225 .map(|arg| ArgInfo::parse(&name, arg))226 .collect::<Result<Vec<_>>>()?;227228 let params_desc = args.iter().flat_map(|a| match a {229 ArgInfo::Normal {230 is_option,231 name,232 cfg_attrs,233 ..234 } => {235 let name = name236 .as_ref()237 .map(|n| quote! {ParamName::new_static(#n)})238 .unwrap_or_else(|| quote! {None});239 Some(quote! {240 #(#cfg_attrs)*241 BuiltinParam::new(#name, #is_option),242 })243 }244 ArgInfo::Lazy { is_option, name } => {245 let name = name246 .as_ref()247 .map(|n| quote! {ParamName::new_static(#n)})248 .unwrap_or_else(|| quote! {None});249 Some(quote! {250 BuiltinParam::new(#name, #is_option),251 })252 }253 ArgInfo::Context => None,254 ArgInfo::Location => None,255 ArgInfo::This => None,256 });257258 let mut id = 0usize;259 let pass = args260 .iter()261 .map(|a| match a {262 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {263 let cid = id;264 id += 1;265 (quote! {#cid}, a)266 }267 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {268 (quote! {compile_error!("should not use id")}, a)269 }270 })271 .map(|(id, a)| match a {272 ArgInfo::Normal {273 ty,274 is_option,275 name,276 cfg_attrs,277 } => {278 let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");279 let eval = quote! {jrsonnet_evaluator::State::push_description(280 || format!("argument <{}> evaluation", #name),281 || <#ty>::from_untyped(value.evaluate()?),282 )?};283 let value = if *is_option {284 quote! {if let Some(value) = &parsed[#id] {285 Some(#eval)286 } else {287 None288 },}289 } else {290 quote! {{291 let value = parsed[#id].as_ref().expect("args shape is checked");292 #eval293 },}294 };295 quote! {296 #(#cfg_attrs)*297 #value298 }299 }300 ArgInfo::Lazy { is_option, .. } => {301 if *is_option {302 quote! {if let Some(value) = &parsed[#id] {303 Some(value.clone())304 } else {305 None306 }}307 } else {308 quote! {309 parsed[#id].as_ref().expect("args shape is correct").clone(),310 }311 }312 }313 ArgInfo::Context => quote! {ctx.clone(),},314 ArgInfo::Location => quote! {location,},315 ArgInfo::This => quote! {self,},316 });317318 let fields = attr.fields.iter().map(|field| {319 let attrs = &field.attrs;320 let name = &field.name;321 let ty = &field.ty;322 quote! {323 #(#attrs)*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, ParamName}, 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 // Should it be `field+:` instead of `field:`399 add: bool,400 // Should it be `field::` instead of `field:`401 hide: bool,402}403impl Parse for TypedAttr {404 fn parse(input: ParseStream) -> syn::Result<Self> {405 let mut out = Self::default();406 loop {407 let lookahead = input.lookahead1();408 if lookahead.peek(kw::rename) {409 input.parse::<kw::rename>()?;410 input.parse::<Token![=]>()?;411 let name = input.parse::<LitStr>()?;412 if out.rename.is_some() {413 return Err(Error::new(414 name.span(),415 "rename attribute may only be specified once",416 ));417 }418 out.rename = Some(name.value());419 } else if lookahead.peek(kw::flatten) {420 input.parse::<kw::flatten>()?;421 out.flatten = true;422 if input.peek(token::Paren) {423 let content;424 parenthesized!(content in input);425 let lookahead = content.lookahead1();426 if lookahead.peek(kw::ok) {427 content.parse::<kw::ok>()?;428 out.flatten_ok = true;429 } else {430 return Err(lookahead.error());431 }432 }433 } else if lookahead.peek(kw::add) {434 input.parse::<kw::add>()?;435 out.add = true;436 } else if lookahead.peek(kw::hide) {437 input.parse::<kw::hide>()?;438 out.hide = true;439 } else if input.is_empty() {440 break;441 } else {442 return Err(lookahead.error());443 }444 if input.peek(Token![,]) {445 input.parse::<Token![,]>()?;446 } else {447 break;448 }449 }450 Ok(out)451 }452}453454struct TypedField {455 attr: TypedAttr,456 ident: Ident,457 ty: Type,458 is_option: bool,459}460impl TypedField {461 fn parse(field: &syn::Field) -> Result<Self> {462 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();463 let Some(ident) = field.ident.clone() else {464 return Err(Error::new(465 field.span(),466 "this field should appear in output object, but it has no visible name",467 ));468 };469 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {470 (true, ty.clone())471 } else {472 (false, field.ty.clone())473 };474 if is_option && attr.flatten {475 if !attr.flatten_ok {476 return Err(Error::new(477 field.span(),478 "strategy should be set when flattening Option",479 ));480 }481 } else if attr.flatten_ok {482 return Err(Error::new(483 field.span(),484 "flatten(ok) is only useable on optional fields",485 ));486 }487488 Ok(Self {489 attr,490 ident,491 ty,492 is_option,493 })494 }495 /// None if this field is flattened in jsonnet output496 fn name(&self) -> Option<String> {497 if self.attr.flatten {498 return None;499 }500 Some(501 self.attr502 .rename503 .clone()504 .unwrap_or_else(|| self.ident.to_string()),505 )506 }507508 fn expand_field(&self) -> Option<TokenStream> {509 if self.is_option {510 return None;511 }512 let name = self.name()?;513 let ty = &self.ty;514 Some(quote! {515 (#name, <#ty as Typed>::TYPE)516 })517 }518 fn expand_parse(&self) -> TokenStream {519 let ident = &self.ident;520 let ty = &self.ty;521 if self.attr.flatten {522 // optional flatten is handled in same way as serde523 return if self.is_option {524 quote! {525 #ident: <#ty as TypedObj>::parse(&obj).ok(),526 }527 } else {528 quote! {529 #ident: <#ty as TypedObj>::parse(&obj)?,530 }531 };532 };533534 let name = self.name().unwrap();535 let value = if self.is_option {536 quote! {537 if let Some(value) = obj.get(#name.into())? {538 Some(<#ty as Typed>::from_untyped(value)?)539 } else {540 None541 }542 }543 } else {544 quote! {545 <#ty as Typed>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?546 }547 };548549 quote! {550 #ident: #value,551 }552 }553 fn expand_serialize(&self) -> Result<TokenStream> {554 let ident = &self.ident;555 let ty = &self.ty;556 Ok(if let Some(name) = self.name() {557 let hide = if self.attr.hide {558 quote! {.hide()}559 } else {560 quote! {}561 };562 let add = if self.attr.add {563 quote! {.add()}564 } else {565 quote! {}566 };567 if self.is_option {568 quote! {569 if let Some(value) = self.#ident {570 out.member(#name.into())571 #hide572 #add573 .value(<#ty as Typed>::into_untyped(value)?)?;574 }575 }576 } else {577 quote! {578 out.member(#name.into())579 #hide580 #add581 .value(<#ty as Typed>::into_untyped(self.#ident)?)?;582 }583 }584 } else if self.is_option {585 quote! {586 if let Some(value) = self.#ident {587 <#ty as TypedObj>::serialize(value, out)?;588 }589 }590 } else {591 quote! {592 <#ty as TypedObj>::serialize(self.#ident, out)?;593 }594 })595 }596}597598#[proc_macro_derive(Typed, attributes(typed))]599pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {600 let input = parse_macro_input!(item as DeriveInput);601602 match derive_typed_inner(input) {603 Ok(v) => v.into(),604 Err(e) => e.to_compile_error().into(),605 }606}607608fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {609 let syn::Data::Struct(data) = &input.data else {610 return Err(Error::new(input.span(), "only structs supported"));611 };612613 let ident = &input.ident;614 let fields = data615 .fields616 .iter()617 .map(TypedField::parse)618 .collect::<Result<Vec<_>>>()?;619620 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();621622 let typed = {623 let fields = fields624 .iter()625 .flat_map(TypedField::expand_field)626 .collect::<Vec<_>>();627 quote! {628 impl #impl_generics Typed for #ident #ty_generics #where_clause {629 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[630 #(#fields,)*631 ]);632633 fn from_untyped(value: Val) -> JrResult<Self> {634 let obj = value.as_obj().expect("shape is correct");635 Self::parse(&obj)636 }637638 fn into_untyped(value: Self) -> JrResult<Val> {639 let mut out = ObjValueBuilder::new();640 value.serialize(&mut out)?;641 Ok(Val::Obj(out.build()))642 }643644 }645 }646 };647648 let fields_parse = fields.iter().map(TypedField::expand_parse);649 let fields_serialize = fields650 .iter()651 .map(TypedField::expand_serialize)652 .collect::<Result<Vec<_>>>()?;653654 Ok(quote! {655 const _: () = {656 use ::jrsonnet_evaluator::{657 typed::{ComplexValType, Typed, TypedObj, CheckType},658 Val, State,659 error::{ErrorKind, Result as JrResult},660 ObjValueBuilder, ObjValue,661 };662663 #typed664665 impl #impl_generics TypedObj for #ident #ty_generics #where_clause {666 fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {667 #(#fields_serialize)*668669 Ok(())670 }671 fn parse(obj: &ObjValue) -> JrResult<Self> {672 Ok(Self {673 #(#fields_parse)*674 })675 }676 }677 };678 })679}