Skip to content

Commit

Permalink
Add get_attr_strings() for doc-like attributes (#14)
Browse files Browse the repository at this point in the history
This allows dependents to create their own attributes that function
similarly to `#[doc = "..."]`. And in fact, `get_doc_comment()` is now
implemented with it.
  • Loading branch information
parasyte committed Feb 11, 2024
1 parent d8fd9c4 commit f3e0e1a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ pub fn spanned_error<S: AsRef<str>>(msg: S, span: Span) -> TokenStream {
])
}

/// Get a list of lines representing the doc comment.
/// Get a list of lines representing the doc comments.
#[must_use]
pub fn get_doc_comment(attrs: &[Attribute]) -> Vec<String> {
get_attr_strings(attrs, "doc")
}

/// Get a list of strings matching the given attribute name.
///
/// This only supports attributes of the form `#[name = "..."]`.
#[must_use]
pub fn get_attr_strings(attrs: &[Attribute], name: &str) -> Vec<String> {
attrs
.iter()
.filter_map(|attr| {
if attr.name.to_string() == "doc" {
if attr.name.to_string() == name {
let mut tree = attr.tree.clone();

match tree.next() {
Expand Down

0 comments on commit f3e0e1a

Please sign in to comment.