From 5ddbd50b37e2f1f63915dec3867be0b747d6ebf5 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 2 Nov 2020 01:35:02 +0300 Subject: [PATCH 1/2] Implement IntoIterator-based convert for impl_payload! macro --- src/local_macros.rs | 68 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/src/local_macros.rs b/src/local_macros.rs index 756cbdf1..f5e6adee 100644 --- a/src/local_macros.rs +++ b/src/local_macros.rs @@ -140,7 +140,7 @@ macro_rules! impl_payload { $( #[ $($field_meta:tt)* ] )* - $v:vis $fields:ident : $FTy:ty $([$into:ident])? + $v:vis $fields:ident : $FTy:ty $([$conv:ident])? , )* } @@ -152,7 +152,7 @@ macro_rules! impl_payload { $( #[ $($opt_field_meta:tt)* ] )* - $opt_v:vis $opt_fields:ident : $OptFTy:ty $([$opt_into:ident])? + $opt_v:vis $opt_fields:ident : $OptFTy:ty $([$opt_conv:ident])? ),* $(,)? } @@ -182,11 +182,11 @@ macro_rules! impl_payload { } impl $Method { - $vi fn new($($($fields : impl_payload!(@into? $FTy $([$into])?)),*)?) -> Self { + $vi fn new($($($fields : impl_payload!(@convert? $FTy $([$conv])?)),*)?) -> Self { Self { $( $( - $fields: $fields $(.$into())?, + $fields: impl_payload!(@convert_map ($fields) $([$conv])?), )* )? $( @@ -213,12 +213,12 @@ macro_rules! impl_payload { $vi trait $Setters: $crate::requests::HasPayload + ::core::marker::Sized { $( $( - impl_payload! { @setter $Method $fields : $FTy $([$into])? } + impl_payload! { @setter $Method $fields : $FTy $([$conv])? } )* )? $( $( - impl_payload! { @setter_opt $Method $opt_fields : $OptFTy $([$opt_into])? } + impl_payload! { @setter_opt $Method $opt_fields : $OptFTy $([$opt_conv])? } )* )? } @@ -246,6 +246,26 @@ macro_rules! impl_payload { } } }; + (@setter_opt $Method:ident $field:ident : $FTy:ty [collect]) => { + calculated_doc! { + #[doc = concat!( + "Setter for [`", + stringify!($field), + "`](", + stringify!($Method), + "::", + stringify!($field), + ") field." + )] + fn $field(mut self, value: T) -> Self + where + T: ::core::iter::IntoIterator::Item>, + { + self.payload_mut().$field = Some(value.into_iter().collect()); + self + } + } + }; (@setter_opt $Method:ident $field:ident : $FTy:ty) => { calculated_doc! { #[doc = concat!( @@ -283,6 +303,26 @@ macro_rules! impl_payload { } } }; + (@setter $Method:ident $field:ident : $FTy:ty [collect]) => { + calculated_doc! { + #[doc = concat!( + "Setter for [`", + stringify!($field), + "`](", + stringify!($Method), + "::", + stringify!($field), + ") field." + )] + fn $field(mut self, value: T) -> Self + where + T: ::core::iter::IntoIterator::Item>, + { + self.payload_mut().$field = value.into_iter().collect(); + self + } + } + }; (@setter $Method:ident $field:ident : $FTy:ty) => { calculated_doc! { #[doc = concat!( @@ -300,10 +340,22 @@ macro_rules! impl_payload { } } }; - (@into? $T:ty [into]) => { + (@convert? $T:ty [into]) => { impl ::core::convert::Into<$T> }; - (@into? $T:ty) => { + (@convert? $T:ty [collect]) => { + impl ::core::iter::IntoIterator::Item> + }; + (@convert? $T:ty) => { $T }; + (@convert_map ($e:expr) [into]) => { + $e.into() + }; + (@convert_map ($e:expr) [collect]) => { + $e.into_iter().collect() + }; + (@convert_map ($e:expr)) => { + $e + }; } From 106c49fa464b06d21d1fe2fd707fac43e6fd1be7 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 2 Nov 2020 02:21:02 +0300 Subject: [PATCH 2/2] add #[serde_with_macros::skip_serializing_none] annotation to the impl_payload output --- src/local_macros.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/local_macros.rs b/src/local_macros.rs index f5e6adee..98afd698 100644 --- a/src/local_macros.rs +++ b/src/local_macros.rs @@ -159,6 +159,7 @@ macro_rules! impl_payload { )? } ) => { + #[serde_with_macros::skip_serializing_none] $( #[ $($method_meta)* ] )*