difftreelog
feat(macros) #[typed(method)]
in: master
2 files changed
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth127 syn::custom_keyword!(flatten);127 syn::custom_keyword!(flatten);128 syn::custom_keyword!(add);128 syn::custom_keyword!(add);129 syn::custom_keyword!(hide);129 syn::custom_keyword!(hide);130 syn::custom_keyword!(method);130 syn::custom_keyword!(ok);131 syn::custom_keyword!(ok);131}132}132133381382382 let name = &fun.sig.ident;383 let name = &fun.sig.ident;383 let vis = &fun.vis;384 let vis = &fun.vis;384 let static_ext = if attr.fields.is_empty() {385 quote! {386 impl #name {387 pub const INST: &'static dyn StaticBuiltin = &#name {};388 }389 impl StaticBuiltin for #name {}390 }391 } else {392 quote! {}393 };394 let static_derive_copy = if attr.fields.is_empty() {385 let static_derive_copy = if attr.fields.is_empty() {395 quote! {, Copy}386 quote! {, Copy, Default}396 } else {387 } else {397 quote! {}388 quote! {}398 };389 };409 const _: () = {400 const _: () = {410 use ::jrsonnet_evaluator::{401 use ::jrsonnet_evaluator::{411 State, Val,402 State, Val,412 function::{builtin::{Builtin, StaticBuiltin}, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation},403 function::{builtin::Builtin, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation},413 Result, Context, typed::{Typed, FromUntyped, IntoUntypedResult},404 Result, Context, typed::{Typed, FromUntyped, IntoUntypedResult},414 parser::Span, params, Thunk,405 parser::Span, params, Thunk,415 };406 };416 params!(407 params!(417 #(#params_desc)*408 #(#params_desc)*418 );409 );419410420 #static_ext421 impl Builtin for #name411 impl Builtin for #name422 where412 where423 Self: 'static413 Self: 'staticcrates/jrsonnet-macros/src/typed.rsdiffbeforeafterboth1use proc_macro2::TokenStream;1use proc_macro2::TokenStream;2use quote::quote;2use quote::quote;3use syn::{3use syn::{4 DeriveInput, Error, Ident, LitStr, Result, Token, Type, parenthesized,4 parenthesized,5 parse::{Parse, ParseStream},5 parse::{Parse, ParseStream},6 spanned::Spanned as _,6 spanned::Spanned as _,7 token,7 token, DeriveInput, Error, Ident, LitStr, Result, Token, Type,8};8};9910use crate::{extract_type_from_option, kw, names::Names, parse_attr, type_is_path};10use crate::{extract_type_from_option, kw, names::Names, parse_attr, type_is_path};22 add: bool,22 add: bool,23 // Should it be `field::` instead of `field:`23 // Should it be `field::` instead of `field:`24 hide: bool,24 hide: bool,25 // Builtin value26 method: bool,25}27}26impl Parse for TypedAttr {28impl Parse for TypedAttr {27 fn parse(input: ParseStream) -> syn::Result<Self> {29 fn parse(input: ParseStream) -> syn::Result<Self> {64 } else if lookahead.peek(kw::hide) {66 } else if lookahead.peek(kw::hide) {65 input.parse::<kw::hide>()?;67 input.parse::<kw::hide>()?;66 out.hide = true;68 out.hide = true;67 } else if input.is_empty() {69 } else if lookahead.peek(kw::method) {70 input.parse::<kw::method>()?;71 out.method = true;72 } else if input.is_empty() {68 break;73 break;69 } else {74 } else {70 return Err(lookahead.error());75 return Err(lookahead.error());134 }139 }135140136 fn expand_field(&self) -> Option<TokenStream> {141 fn expand_field(&self) -> Option<TokenStream> {137 if self.is_option {142 if self.is_option || self.attr.method {138 return None;143 return None;139 }144 }140 let name = self.name()?;145 let name = self.name()?;256 } else {261 } else {257 quote! {}262 quote! {}258 };263 };259 let value = if self.is_lazy {264 let value = if self.attr.method {265 quote! {266 out.method(__names[#name].clone(), value);267 }268 } else if self.is_lazy {260 quote! {269 quote! {261 out.field(__names[#name].clone())270 out.field(__names[#name].clone())262 #hide271 #hide