Skip to content

Commit

Permalink
Refactor IntoParams attribute parsing functionality (#339)
Browse files Browse the repository at this point in the history
Refactor IntoParams field attribute parsing functionality to use the new
Feature based parsing. Add new features for `Rename`, `RenameAll`,
`Style`, `AllowReserved` and `Explode`.

Add support for serde `default`, `rename` and `rename_all` attributes
for container and field level similar to `ToSchema` implementation.

Update docs and add tests for renaming and supported serde attributes.

Resolves #309, fixes #313
  • Loading branch information
juhaku committed Nov 8, 2022
1 parent 3364428 commit 8d3e443
Show file tree
Hide file tree
Showing 9 changed files with 918 additions and 308 deletions.
37 changes: 23 additions & 14 deletions utoipa-gen/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ pub mod into_params;

mod features;
pub mod schema;
mod serde;
pub mod serde;

/// Check whether either serde `container_rule` or `field_rule` has _`default`_ attribute set.
#[inline]
fn is_default(container_rules: &Option<&SerdeContainer>, field_rule: &Option<&SerdeValue>) -> bool {
container_rules
.as_ref()
.map(|rule| rule.default)
.unwrap_or(false)
|| field_rule
.as_ref()
.map(|rule| rule.default)
.unwrap_or(false)
}

/// Find `#[deprecated]` attribute from given attributes. Typically derive type attributes
/// or field attributes of struct.
Expand Down Expand Up @@ -293,29 +306,25 @@ trait Rename {
///
/// Method accepts 3 arguments.
/// * `value` to rename.
/// * `field_rule` which is used for renaming fields with _`rename`_ property.
/// * `to` Optional rename to value for fields with _`rename`_ property.
/// * `container_rule` which is used to rename containers with _`rename_all`_ property.
fn rename<'r, R: Rename>(
value: &'r str,
field_rule: &'r Option<SerdeValue>,
container_rule: &'r Option<SerdeContainer>,
to: Option<&'r str>,
container_rule: Option<&'r RenameRule>,
) -> Option<Cow<'r, str>> {
let rename = field_rule.as_ref().and_then(|field_rule| {
if !field_rule.rename.is_empty() {
Some(Cow::Borrowed(&*field_rule.rename))
let rename = to.as_ref().and_then(|to| {
if !to.is_empty() {
Some(Cow::Borrowed(*to))
} else {
None
}
});

rename.or_else(|| {
container_rule.as_ref().and_then(|container_rule| {
container_rule
.rename_all
.as_ref()
.map(|rule| R::rename(rule, value))
.map(Cow::Owned)
})
container_rule
.as_ref()
.map(|container_rule| Cow::Owned(R::rename(container_rule, value)))
})
}

Expand Down
Loading

0 comments on commit 8d3e443

Please sign in to comment.