git.delta.rocks / jrsonnet / refs/commits / a1d4291c87ac

difftreelog

feat support hidden/added fields in derive(Typed)

Yaroslav Bolyukin2023-08-06parent: #895de28.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
87 syn::custom_keyword!(fields);87 syn::custom_keyword!(fields);
88 syn::custom_keyword!(rename);88 syn::custom_keyword!(rename);
89 syn::custom_keyword!(flatten);89 syn::custom_keyword!(flatten);
90 syn::custom_keyword!(add);
91 syn::custom_keyword!(hide);
90 syn::custom_keyword!(ok);92 syn::custom_keyword!(ok);
91}93}
9294
230 } => {232 } => {
231 let name = name233 let name = name
232 .as_ref()234 .as_ref()
233 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})235 .map(|n| quote! {ParamName::new_static(#n)})
234 .unwrap_or_else(|| quote! {None});236 .unwrap_or_else(|| quote! {None});
235 Some(quote! {237 Some(quote! {
236 #(#cfg_attrs)*238 #(#cfg_attrs)*
237 BuiltinParam {239 BuiltinParam::new(#name, #is_option),
238 name: #name,
239 has_default: #is_option,
240 },
241 })240 })
242 }241 }
243 ArgInfo::Lazy { is_option, name } => {242 ArgInfo::Lazy { is_option, name } => {
244 let name = name243 let name = name
245 .as_ref()244 .as_ref()
246 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})245 .map(|n| quote! {ParamName::new_static(#n)})
247 .unwrap_or_else(|| quote! {None});246 .unwrap_or_else(|| quote! {None});
248 Some(quote! {247 Some(quote! {
249 BuiltinParam {248 BuiltinParam::new(#name, #is_option),
250 name: #name,
251 has_default: #is_option,
252 },
253 })249 })
254 }250 }
255 ArgInfo::Context => None,251 ArgInfo::Context => None,
355 const _: () = {351 const _: () = {
356 use ::jrsonnet_evaluator::{352 use ::jrsonnet_evaluator::{
357 State, Val,353 State, Val,
358 function::{builtin::{Builtin, StaticBuiltin, BuiltinParam}, CallLocation, ArgsLike, parse::parse_builtin_call},354 function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName}, CallLocation, ArgsLike, parse::parse_builtin_call},
359 error::Result, Context, typed::Typed,355 error::Result, Context, typed::Typed,
360 parser::ExprLocation,356 parser::ExprLocation,
361 };357 };
395 /// flatten(ok) strategy for flattened optionals391 /// flatten(ok) strategy for flattened optionals
396 /// field would be None in case of any parsing error (as in serde)392 /// field would be None in case of any parsing error (as in serde)
397 flatten_ok: bool,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}398}
399impl Parse for TypedAttr {399impl Parse for TypedAttr {
400 fn parse(input: ParseStream) -> syn::Result<Self> {400 fn parse(input: ParseStream) -> syn::Result<Self> {
426 return Err(lookahead.error());426 return Err(lookahead.error());
427 }427 }
428 }428 }
429 } else if input.is_empty() {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() {
430 break;436 break;
431 } else {437 } else {
432 return Err(lookahead.error());438 return Err(lookahead.error());
544 let ident = &self.ident;550 let ident = &self.ident;
545 let ty = &self.ty;551 let ty = &self.ty;
546 Ok(if let Some(name) = self.name() {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 };
547 if self.is_option {563 if self.is_option {
548 quote! {564 quote! {
549 if let Some(value) = self.#ident {565 if let Some(value) = self.#ident {
550 out.member(#name.into()).value(<#ty as Typed>::into_untyped(value)?)?;566 out.member(#name.into())
567 #hide
568 #add
569 .value(<#ty as Typed>::into_untyped(value)?)?;
551 }570 }
552 }571 }
553 } else {572 } else {
554 quote! {573 quote! {
555 out.member(#name.into()).value(<#ty as Typed>::into_untyped(self.#ident)?)?;574 out.member(#name.into())
575 #hide
576 #add
577 .value(<#ty as Typed>::into_untyped(self.#ident)?)?;
556 }578 }
557 }579 }