@@ -153,7 +153,7 @@ pub(crate) fn expand(input: syn::DeriveInput) -> proc_macro2::TokenStream {
153
153
. map ( |f| f. ident . clone ( ) . unwrap ( ) )
154
154
. collect ( ) ;
155
155
156
- let default_type_variables = expand_default_type_variables ( & required_field_idents) ;
156
+ let default_type_variables = expand_default_type_variables ( & required_field_idents) ;
157
157
158
158
let field_type_map = create_field_type_map ( & fields) ;
159
159
let required_fns =
@@ -198,7 +198,6 @@ pub(crate) fn expand(input: syn::DeriveInput) -> proc_macro2::TokenStream {
198
198
} ) ;
199
199
let arg_type = field_type_map. get ( & ident. to_string ( ) ) . unwrap ( ) ;
200
200
let arg_type = inner_type ( "Option" , arg_type) . to_owned ( ) . unwrap ( ) ;
201
-
202
201
quote ! {
203
202
impl <#( #decls) * > #builder_name<#( #types) * > {
204
203
pub fn #ident( self , #ident: #arg_type) -> #builder_name<#( #return_types) * > {
@@ -211,6 +210,33 @@ pub(crate) fn expand(input: syn::DeriveInput) -> proc_macro2::TokenStream {
211
210
}
212
211
} ) ;
213
212
213
+ let with_option_fns = optional_field_idents. clone ( ) . into_iter ( ) . map ( |ident| {
214
+ let decls = expand_all_type_variables ( & required_field_idents) ;
215
+ let types = expand_all_type_variables ( & required_field_idents) ;
216
+ let return_types = expand_all_type_variables ( & required_field_idents) ;
217
+ let rest_fields = fields. named . iter ( ) . map ( |f| {
218
+ let name = & f. ident ;
219
+ if name. clone ( ) . unwrap ( ) . to_string ( ) == ident. to_string ( ) {
220
+ return quote ! { } ;
221
+ } ;
222
+ quote ! { #name: self . #name, }
223
+ } ) ;
224
+ let arg_type = field_type_map. get ( & ident. to_string ( ) ) . unwrap ( ) ;
225
+ let arg_type = inner_type ( "Option" , arg_type) . to_owned ( ) . unwrap ( ) ;
226
+ let with_opt_fn_name = format_ident ! ( "{}_with_option" , ident) ;
227
+
228
+ quote ! {
229
+ impl <#( #decls) * > #builder_name<#( #types) * > {
230
+ pub fn #with_opt_fn_name( self , #ident: Option <#arg_type>) -> #builder_name<#( #return_types) * > {
231
+ #builder_name{
232
+ #ident: #ident,
233
+ #( #rest_fields) *
234
+ }
235
+ }
236
+ }
237
+ }
238
+ } ) ;
239
+
214
240
let build_fields = fields. named . iter ( ) . map ( |f| {
215
241
let name = & f. ident ;
216
242
quote ! { #name: self . #name, }
@@ -263,6 +289,7 @@ pub(crate) fn expand(input: syn::DeriveInput) -> proc_macro2::TokenStream {
263
289
264
290
#( #required_fns) *
265
291
#( #optional_fns) *
292
+ #( #with_option_fns) *
266
293
}
267
294
}
268
295
0 commit comments