Skip to content

Commit

Permalink
Add unspecified field option
Browse files Browse the repository at this point in the history
  • Loading branch information
tinrab committed Jun 7, 2024
1 parent 58730f6 commit f6d4250
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
7 changes: 7 additions & 0 deletions bomboni_request/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ mod tests {
required: String,
possibly_empty: String,
regex_validated: String,
wrapped: StringValue,
}

#[derive(Parse, Debug, PartialEq)]
Expand All @@ -671,6 +672,8 @@ mod tests {
possibly_empty: Option<String>,
#[parse(regex = "^[a-z]+$")]
regex_validated: String,
#[parse(wrapper, unspecified)]
wrapped: String,
}

impl Default for Item {
Expand All @@ -679,6 +682,9 @@ mod tests {
required: "required".into(),
possibly_empty: "possibly_empty".into(),
regex_validated: "regex".into(),
wrapped: StringValue {
value: "wrapped".into(),
},
}
}
}
Expand All @@ -689,6 +695,7 @@ mod tests {
required: "required".into(),
possibly_empty: Some("possibly_empty".into()),
regex_validated: "regex".into(),
wrapped: "wrapped".into(),
}
);

Expand Down
1 change: 1 addition & 0 deletions bomboni_request_derive/src/parse/field_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ mod tests {
skip: false,
keep: false,
keep_primitive: false,
unspecified: false,
extract: None,
wrapper: false,
oneof: false,
Expand Down
4 changes: 4 additions & 0 deletions bomboni_request_derive/src/parse/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ pub struct ParseFieldOptions {
/// Only surrounding container will be extracted and parsed.
#[darling(default)]
pub keep_primitive: bool,
/// Allow unspecified enum values and empty strings.
/// The field will not be treated as required.
#[darling(default)]
pub unspecified: bool,
/// Extraction plan for the field.
#[darling(default)]
pub extract: Option<FieldExtract>,
Expand Down
36 changes: 20 additions & 16 deletions bomboni_request_derive/src/parse/parse_utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,16 @@ pub fn expand_parse_field_type(
.map_err(|err: RequestError| err.wrap_path(#field_error_path) #inner_wrap_err)?;
});
} else if field_options.enumeration {
parse_impl = quote! {
if target == 0 {
return Err(RequestError::path(
#field_error_path,
CommonError::RequiredFieldMissing,
) #inner_wrap_err);
}
};
if !field_options.unspecified {
parse_impl = quote! {
if target == 0 {
return Err(RequestError::path(
#field_error_path,
CommonError::RequiredFieldMissing,
) #inner_wrap_err);
}
};
}
if !field_options.keep_primitive {
parse_impl.extend(quote! {
let target = target.try_into()
Expand All @@ -260,14 +262,16 @@ pub fn expand_parse_field_type(
}

if primitive_ident == "String" {
parse_impl.extend(quote! {
if target.is_empty() {
return Err(RequestError::path(
#field_error_path,
CommonError::RequiredFieldMissing,
) #inner_wrap_err);
}
});
if !field_options.unspecified {
parse_impl.extend(quote! {
if target.is_empty() {
return Err(RequestError::path(
#field_error_path,
CommonError::RequiredFieldMissing,
) #inner_wrap_err);
}
});
}
if let Some(regex) = field_options.regex.as_ref() {
parse_impl.extend(quote! {
if !re.is_match(&target) {
Expand Down

0 comments on commit f6d4250

Please sign in to comment.