Skip to content

Commit

Permalink
feat(structure): add Body::has_{attribute,blocks}
Browse files Browse the repository at this point in the history
  • Loading branch information
martinohmann committed May 12, 2023
1 parent 930a511 commit 8af4b19
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/hcl-edit/src/structure/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,40 @@ impl Body {
self.structures.get_mut(index)
}

/// Returns `true` if the body contains an attribute with given key.
///
/// # Example
///
/// ```
/// use hcl_edit::structure::{Attribute, Body};
/// use hcl_edit::Ident;
///
/// let body = Body::from_iter([Attribute::new(Ident::new("foo"), "bar")]);
/// assert!(body.has_attribute("foo"));
/// assert!(!body.has_attribute("bar"));
/// ```
#[inline]
pub fn has_attribute(&self, key: &str) -> bool {
self.get_attribute(key).is_some()
}

/// Returns `true` if the body contains blocks with given identifier.
///
/// # Example
///
/// ```
/// use hcl_edit::structure::{Block, Body};
/// use hcl_edit::Ident;
///
/// let body = Body::from_iter([Block::new(Ident::new("foo"))]);
/// assert!(body.has_blocks("foo"));
/// assert!(!body.has_blocks("bar"));
/// ```
#[inline]
pub fn has_blocks(&self, ident: &str) -> bool {
self.get_blocks(ident).next().is_some()
}

/// Returns a reference to the `Attribute` with given key if it exists, otherwise `None`.
///
/// # Example
Expand Down

0 comments on commit 8af4b19

Please sign in to comment.