Skip to content

Commit c8f4474

Browse files
committed
Implement handling of optional attribute in proc macro
1 parent 657d025 commit c8f4474

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

derive/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,19 @@ fn generate_field(field: &Field) -> TokenStream2 {
146146
quote! {
147147
.unwrap_or_else(|| #default)
148148
}
149+
} else if attr.optional {
150+
quote! {
151+
.map(crate::function::OptionalArg::Present)
152+
.unwrap_or(crate::function::OptionalArg::Missing)
153+
}
149154
} else {
150155
let err = match attr.kind {
151-
ArgKind::Positional | ArgKind::PositionalKeyword => {
152-
quote!(crate::function::ArgumentError::TooFewArgs)
153-
}
154-
ArgKind::Keyword => quote!(crate::function::ArgumentError::RequiredKeywordArgument(
155-
stringify!(#name)
156-
)),
156+
ArgKind::Positional | ArgKind::PositionalKeyword => quote! {
157+
crate::function::ArgumentError::TooFewArgs
158+
},
159+
ArgKind::Keyword => quote! {
160+
crate::function::ArgumentError::RequiredKeywordArgument(tringify!(#name))
161+
},
157162
};
158163
quote! {
159164
.ok_or_else(|| #err)?

0 commit comments

Comments
 (0)