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.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/typed.rs
+++ b/crates/jrsonnet-macros/src/typed.rs
@@ -1,10 +1,10 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
- DeriveInput, Error, Ident, LitStr, Result, Token, Type, parenthesized,
+ parenthesized,
parse::{Parse, ParseStream},
spanned::Spanned as _,
- token,
+ token, DeriveInput, Error, Ident, LitStr, Result, Token, Type,
};
use crate::{extract_type_from_option, kw, names::Names, parse_attr, type_is_path};
@@ -22,6 +22,8 @@
add: bool,
// Should it be `field::` instead of `field:`
hide: bool,
+ // Builtin value
+ method: bool,
}
impl Parse for TypedAttr {
fn parse(input: ParseStream) -> syn::Result<Self> {
@@ -64,6 +66,9 @@
} else if lookahead.peek(kw::hide) {
input.parse::<kw::hide>()?;
out.hide = true;
+ } else if lookahead.peek(kw::method) {
+ input.parse::<kw::method>()?;
+ out.method = true;
} else if input.is_empty() {
break;
} else {
@@ -134,7 +139,7 @@
}
fn expand_field(&self) -> Option<TokenStream> {
- if self.is_option {
+ if self.is_option || self.attr.method {
return None;
}
let name = self.name()?;
@@ -256,7 +261,11 @@
} else {
quote! {}
};
- let value = if self.is_lazy {
+ let value = if self.attr.method {
+ quote! {
+ out.method(__names[#name].clone(), value);
+ }
+ } else if self.is_lazy {
quote! {
out.field(__names[#name].clone())
#hide