git.delta.rocks / jrsonnet / refs/commits / 153383ce07c1

difftreelog

feat(derive) explicit traits

Yaroslav Bolyukin2023-01-22parent: #e0a8037.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
499 let name = self.name()?;499 let name = self.name()?;
500 let ty = &self.ty;500 let ty = &self.ty;
501 Some(quote! {501 Some(quote! {
502 (#name, <#ty>::TYPE)502 (#name, <#ty as Typed>::TYPE)
503 })503 })
504 }504 }
505 fn expand_parse(&self) -> TokenStream {505 fn expand_parse(&self) -> TokenStream {
509 // optional flatten is handled in same way as serde509 // optional flatten is handled in same way as serde
510 return if self.is_option {510 return if self.is_option {
511 quote! {511 quote! {
512 #ident: <#ty>::parse(&obj).ok(),512 #ident: <#ty as TypedObj>::parse(&obj).ok(),
513 }513 }
514 } else {514 } else {
515 quote! {515 quote! {
516 #ident: <#ty>::parse(&obj)?,516 #ident: <#ty as TypedObj>::parse(&obj)?,
517 }517 }
518 };518 };
519 };519 };
522 let value = if self.is_option {522 let value = if self.is_option {
523 quote! {523 quote! {
524 if let Some(value) = obj.get(#name.into())? {524 if let Some(value) = obj.get(#name.into())? {
525 Some(<#ty>::from_untyped(value)?)525 Some(<#ty as Typed>::from_untyped(value)?)
526 } else {526 } else {
527 None527 None
528 }528 }
529 }529 }
530 } else {530 } else {
531 quote! {531 quote! {
532 <#ty>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?532 <#ty as Typed>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?
533 }533 }
534 };534 };
535535
544 if self.is_option {544 if self.is_option {
545 quote! {545 quote! {
546 if let Some(value) = self.#ident {546 if let Some(value) = self.#ident {
547 out.member(#name.into()).value(<#ty>::into_untyped(value)?)?;547 out.member(#name.into()).value(<#ty as Typed>::into_untyped(value)?)?;
548 }548 }
549 }549 }
550 } else {550 } else {
551 quote! {551 quote! {
552 out.member(#name.into()).value(<#ty>::into_untyped(self.#ident)?)?;552 out.member(#name.into()).value(<#ty as Typed>::into_untyped(self.#ident)?)?;
553 }553 }
554 }554 }
555 } else if self.is_option {555 } else if self.is_option {
556 quote! {556 quote! {
557 if let Some(value) = self.#ident {557 if let Some(value) = self.#ident {
558 value.serialize(out)?;558 <#ty as TypedObj>::serialize(value, out)?;
559 }559 }
560 }560 }
561 } else {561 } else {
562 quote! {562 quote! {
563 self.#ident.serialize(out)?;563 <#ty as TypedObj>::serialize(self.#ident, out)?;
564 }564 }
565 })565 })
566 }566 }