From a10f2cf96ed804a0faa5d36ffa52d6fac03cc2e9 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sat, 04 Apr 2026 21:27:50 +0000 Subject: [PATCH] feat(macros): #[typed(method)] --- --- a/crates/jrsonnet-macros/src/lib.rs +++ b/crates/jrsonnet-macros/src/lib.rs @@ -127,6 +127,7 @@ syn::custom_keyword!(flatten); syn::custom_keyword!(add); syn::custom_keyword!(hide); + syn::custom_keyword!(method); syn::custom_keyword!(ok); } @@ -381,18 +382,8 @@ let name = &fun.sig.ident; let vis = &fun.vis; - let static_ext = if attr.fields.is_empty() { - quote! { - impl #name { - pub const INST: &'static dyn StaticBuiltin = &#name {}; - } - impl StaticBuiltin for #name {} - } - } else { - quote! {} - }; let static_derive_copy = if attr.fields.is_empty() { - quote! {, Copy} + quote! {, Copy, Default} } else { quote! {} }; @@ -409,7 +400,7 @@ const _: () = { use ::jrsonnet_evaluator::{ State, Val, - function::{builtin::{Builtin, StaticBuiltin}, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation}, + function::{builtin::Builtin, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation}, Result, Context, typed::{Typed, FromUntyped, IntoUntypedResult}, parser::Span, params, Thunk, }; @@ -417,7 +408,6 @@ #(#params_desc)* ); - #static_ext impl Builtin for #name where Self: 'static --- 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 { @@ -64,6 +66,9 @@ } else if lookahead.peek(kw::hide) { input.parse::()?; out.hide = true; + } else if lookahead.peek(kw::method) { + input.parse::()?; + out.method = true; } else if input.is_empty() { break; } else { @@ -134,7 +139,7 @@ } fn expand_field(&self) -> Option { - 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 -- gitstuff