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 @@ pub fn is_word(&self) -> bool {
} }
} }
/// 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>> { pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
match &self.kind { match &self.kind {
AttrKind::Normal(normal) => normal.item.meta_item_list(), AttrKind::Normal(normal) => normal.item.meta_item_list(),
@ -143,6 +150,21 @@ pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
} }
} }
/// 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> { pub fn value_str(&self) -> Option<Symbol> {
match &self.kind { match &self.kind {
AttrKind::Normal(normal) => normal.item.value_str(), AttrKind::Normal(normal) => normal.item.value_str(),
@ -232,6 +254,18 @@ pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
} }
} }
/// 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> { fn value_str(&self) -> Option<Symbol> {
match &self.args { match &self.args {
AttrArgs::Eq(_, args) => args.value_str(), AttrArgs::Eq(_, args) => args.value_str(),
@ -315,6 +349,18 @@ pub fn name_value_literal_span(&self) -> Option<Span> {
Some(self.name_value_literal()?.span) 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> { pub fn value_str(&self) -> Option<Symbol> {
match &self.kind { match &self.kind {
MetaItemKind::NameValue(v) => v.kind.str(), MetaItemKind::NameValue(v) => v.kind.str(),