@@ -153,7 +153,7 @@ pub(crate) fn expand(input: syn::DeriveInput) -> proc_macro2::TokenStream {
153153 . map ( |f| f. ident . clone ( ) . unwrap ( ) )
154154 . collect ( ) ;
155155
156- let default_type_variables = expand_default_type_variables ( & required_field_idents) ;
156+ let default_type_variables = expand_default_type_variables ( & required_field_idents) ;
157157
158158 let field_type_map = create_field_type_map ( & fields) ;
159159 let required_fns =
@@ -198,7 +198,6 @@ pub(crate) fn expand(input: syn::DeriveInput) -> proc_macro2::TokenStream {
198198 } ) ;
199199 let arg_type = field_type_map. get ( & ident. to_string ( ) ) . unwrap ( ) ;
200200 let arg_type = inner_type ( "Option" , arg_type) . to_owned ( ) . unwrap ( ) ;
201-
202201 quote ! {
203202 impl <#( #decls) * > #builder_name<#( #types) * > {
204203 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 {
211210 }
212211 } ) ;
213212
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+
214240 let build_fields = fields. named . iter ( ) . map ( |f| {
215241 let name = & f. ident ;
216242 quote ! { #name: self . #name, }
@@ -263,6 +289,7 @@ pub(crate) fn expand(input: syn::DeriveInput) -> proc_macro2::TokenStream {
263289
264290 #( #required_fns) *
265291 #( #optional_fns) *
292+ #( #with_option_fns) *
266293 }
267294}
268295
0 commit comments