Skip to content

Commit

Permalink
Improve openapi info generation
Browse files Browse the repository at this point in the history
Remove unnecessary allocation and make code simplier to reason. Do not
render empty email to the generated OpenAPI doc.
  • Loading branch information
juhaku committed Sep 26, 2022
1 parent 513f66d commit 19e029e
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions utoipa-gen/src/openapi/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,31 @@ pub(crate) fn impl_info() -> TokenStream2 {
}
}

fn get_parsed_author(author: Option<&str>) -> Option<(&str, String)> {
fn get_parsed_author(author: Option<&str>) -> Option<(&str, &str)> {
author.map(|author| {
if author.contains('<') && author.contains('>') {
let mut author_iter = author.split('<');
let mut author_iter = author.split('<');

let name = author_iter.next().unwrap_or_default();
let mut email = author_iter.next().unwrap_or_default().to_string();
email = email.replace('<', "").replace('>', "");

(name.trim_end(), email)
} else {
(author, "".to_string())
let name = author_iter.next().unwrap_or_default();
let mut email = author_iter.next().unwrap_or_default();
if !email.is_empty() {
email = &email[..email.len() - 1];
}

(name.trim_end(), email)
})
}

fn get_contact(authors: &str) -> TokenStream2 {
if let Some((name, email)) = get_parsed_author(authors.split(':').into_iter().next()) {
let email_tokens = if email.is_empty() {
None
} else {
Some(quote! { .email(Some(#email)) })
};
quote! {
utoipa::openapi::ContactBuilder::new()
.name(Some(#name))
.email(Some(#email))
#email_tokens
.build()
}
} else {
Expand Down

0 comments on commit 19e029e

Please sign in to comment.