Skip to content

Commit

Permalink
Fix hard error in doc-comment parsing (#86)
Browse files Browse the repository at this point in the history
* Fix hard error when if Ident cannot be found from ast Attribute with only one path segment. -> Now returns just false to skip the Attribute.
  • Loading branch information
juhaku authored Apr 15, 2022
1 parent 9e5ae05 commit 90d16e4
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions utoipa-gen/src/doc_comment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::{Ident, Span};
use proc_macro_error::{abort_call_site, emit_warning, OptionExt, ResultExt};
use proc_macro_error::{abort_call_site, emit_warning, ResultExt};
use syn::{Attribute, Lit, Meta};

const DOC_ATTRIBUTE_TYPE: &str = "doc";
Expand All @@ -18,14 +18,14 @@ impl CommentAttributes {
}

fn is_doc_attribute(attribute: &&Attribute) -> bool {
&*Self::get_attribute_ident(attribute).to_string() == DOC_ATTRIBUTE_TYPE
match Self::get_attribute_ident(attribute) {
Some(attribute) => attribute == DOC_ATTRIBUTE_TYPE,
None => false,
}
}

fn get_attribute_ident(attribute: &Attribute) -> &Ident {
attribute
.path
.get_ident()
.expect_or_abort("Expected doc attribute with one path segment")
fn get_attribute_ident(attribute: &Attribute) -> Option<&Ident> {
attribute.path.get_ident()
}

fn as_string_vec<'a, I: Iterator<Item = &'a Attribute>>(attributes: I) -> Vec<String> {
Expand All @@ -38,7 +38,6 @@ impl CommentAttributes {
fn parse_doc_comment(attribute: &Attribute) -> Option<String> {
let meta = attribute.parse_meta().unwrap_or_abort();

// TODO find a correct span?
match meta {
Meta::NameValue(name_value) => {
if let Lit::Str(doc_comment) = name_value.lit {
Expand Down

0 comments on commit 90d16e4

Please sign in to comment.