Add documentation on ast::Attribute

This commit is contained in:
Guillaume Gomez 2024-11-05 16:15:45 +01:00
parent 096277e989
commit 33009601af

View File

@ -136,6 +136,13 @@ impl Attribute {
}
}
/// Returns a list of meta items if the attribute is delimited with parenthesis:
///
/// ```text
/// #[attr(a, b = "c")] // Returns `Some()`.
/// #[attr = ""] // Returns `None`.
/// #[attr] // Returns `None`.
/// ```
pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
match &self.kind {
AttrKind::Normal(normal) => normal.item.meta_item_list(),
@ -143,6 +150,21 @@ impl Attribute {
}
}
/// Returns the string value in:
///
/// ```text
/// #[attribute = "value"]
/// ^^^^^^^
/// ```
///
/// It returns `None` in any other cases, including doc comments if they
/// are not under the form `#[doc = "..."]`.
///
/// It also returns `None` for:
///
/// ```text
/// #[attr("value")]
/// ```
pub fn value_str(&self) -> Option<Symbol> {
match &self.kind {
AttrKind::Normal(normal) => normal.item.value_str(),
@ -232,6 +254,18 @@ impl AttrItem {
}
}
/// Returns the string value in:
///
/// ```text
/// #[attribute = "value"]
/// ^^^^^^^
/// ```
///
/// It returns `None` in any other cases like:
///
/// ```text
/// #[attr("value")]
/// ```
fn value_str(&self) -> Option<Symbol> {
match &self.args {
AttrArgs::Eq(_, args) => args.value_str(),
@ -315,6 +349,18 @@ impl MetaItem {
Some(self.name_value_literal()?.span)
}
/// Returns the string value in:
///
/// ```text
/// #[attribute = "value"]
/// ^^^^^^^
/// ```
///
/// It returns `None` in any other cases like:
///
/// ```text
/// #[attr("value")]
/// ```
pub fn value_str(&self) -> Option<Symbol> {
match &self.kind {
MetaItemKind::NameValue(v) => v.kind.str(),