From 8af4b193ff11aa80f2b065ab5a1cea5f3946bf1a Mon Sep 17 00:00:00 2001 From: martinohmann Date: Fri, 12 May 2023 11:51:41 +0200 Subject: [PATCH] feat(structure): add `Body::has_{attribute,blocks}` --- crates/hcl-edit/src/structure/body.rs | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/hcl-edit/src/structure/body.rs b/crates/hcl-edit/src/structure/body.rs index d9a36dea..2437da2e 100644 --- a/crates/hcl-edit/src/structure/body.rs +++ b/crates/hcl-edit/src/structure/body.rs @@ -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