Skip to content

Commit

Permalink
fix(sol-macro): flatten doc strings correctly (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Oct 9, 2023
1 parent 5585abd commit b45a0d3
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions crates/sol-macro/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,31 @@ pub fn docs(attrs: &[Attribute]) -> impl Iterator<Item = &Attribute> {
attrs.iter().filter(|a| is_doc(a))
}

/// Flattens all the `#[doc = "..."]` attributes into a single string.
pub fn docs_str(attrs: &[Attribute]) -> String {
docs(attrs)
.filter_map(|attr| attr.parse_args::<LitStr>().ok())
.map(|doc| doc.value())
.collect::<Vec<_>>()
.join("\n")
let mut doc = String::new();
for attr in docs(attrs) {
let syn::Meta::NameValue(syn::MetaNameValue {
value:
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(s),
..
}),
..
}) = &attr.meta
else {
continue
};

let value = s.value();
if !value.is_empty() {
if !doc.is_empty() {
doc.push('\n');
}
doc.push_str(&value);
}
}
doc
}

pub fn derives(attrs: &[Attribute]) -> impl Iterator<Item = &Attribute> {
Expand Down

0 comments on commit b45a0d3

Please sign in to comment.