hcl_edit::structure

Struct Block

Source
pub struct Block {
    pub ident: Decorated<Ident>,
    pub labels: Vec<BlockLabel>,
    pub body: Body,
    /* private fields */
}
Expand description

Represents an HCL block which consists of a block identifier, zero or more block labels and a block body.

In HCL syntax this is represented as:

block_identifier "block_label1" "block_label2" {
  body
}

Fields§

§ident: Decorated<Ident>

The block identifier.

§labels: Vec<BlockLabel>

Zero or more block labels.

§body: Body

Represents the Block’s body.

Implementations§

Source§

impl Block

Source

pub fn new(ident: impl Into<Decorated<Ident>>) -> Block

Creates a new Block from an identifier.

Source

pub fn builder(ident: impl Into<Decorated<Ident>>) -> BlockBuilder

Creates a new BlockBuilder to start building a new Block with the provided identifier.

Source

pub fn is_labeled(&self) -> bool

Returns true if the block has labels.

§Example
use hcl_edit::{structure::Block, Ident};

let block = Block::new(Ident::new("foo"));
assert!(!block.is_labeled());

let labeled_block = Block::builder(Ident::new("foo"))
    .label("bar")
    .build();
assert!(labeled_block.is_labeled());
Source

pub fn has_ident(&self, ident: &str) -> bool

Returns true if the block has the given identifier.

§Example
use hcl_edit::{structure::Block, Ident};

let block = Block::new(Ident::new("foo"));
assert!(block.has_ident("foo"));
assert!(!block.has_ident("bar"));
Source

pub fn has_labels<T>(&self, labels: &[T]) -> bool
where T: AsRef<str>,

Returns true if the Block’s labels and the provided ones share a common prefix.

For example, &["foo"] will match blocks that fulfil either of these criteria:

  • Single "foo" label.
  • Multiple labels, with "foo" being in first position.

For an alternative which matches labels exactly see Block::has_exact_labels.

§Examples
use hcl_edit::{structure::Block, Ident};

let block = Block::builder(Ident::new("resource"))
    .labels(["aws_s3_bucket", "mybucket"])
    .build();

assert!(block.has_labels(&["aws_s3_bucket"]));
assert!(block.has_labels(&["aws_s3_bucket", "mybucket"]));
assert!(!block.has_labels(&["mybucket"]));

One use case for this method is to find blocks in a Body that have a common label prefix:

use hcl_edit::structure::{Attribute, Block, Body};
use hcl_edit::Ident;

let body = Body::builder()
    .attribute(Attribute::new(Ident::new("foo"), "bar"))
    .block(
        Block::builder(Ident::new("resource"))
            .labels(["aws_s3_bucket", "bucket1"])
    )
    .block(
        Block::builder(Ident::new("resource"))
            .labels(["aws_db_instance", "db_instance"])
    )
    .block(
        Block::builder(Ident::new("resource"))
            .labels(["aws_s3_bucket", "bucket2"])
    )
    .build();

let buckets: Vec<&Block> = body.get_blocks("resource")
    .filter(|block| block.has_labels(&["aws_s3_bucket"]))
    .collect();

assert_eq!(
    buckets,
    [
        &Block::builder(Ident::new("resource"))
            .labels(["aws_s3_bucket", "bucket1"])
            .build(),
        &Block::builder(Ident::new("resource"))
            .labels(["aws_s3_bucket", "bucket2"])
            .build()
    ]
);
Source

pub fn has_exact_labels<T>(&self, labels: &[T]) -> bool
where T: AsRef<str>,

Returns true if the Block’s labels match the provided ones exactly.

For an alternative which matches a common label prefix see Block::has_labels.

§Examples
use hcl_edit::{structure::Block, Ident};

let block = Block::builder(Ident::new("resource"))
    .labels(["aws_s3_bucket", "mybucket"])
    .build();

assert!(!block.has_exact_labels(&["aws_s3_bucket"]));
assert!(block.has_exact_labels(&["aws_s3_bucket", "mybucket"]));

One use case for this method is to find blocks in a Body that have an exact set of labels:

use hcl_edit::structure::{Attribute, Block, Body};
use hcl_edit::Ident;

let body = Body::builder()
    .block(
        Block::builder(Ident::new("resource"))
            .labels(["aws_s3_bucket", "bucket1"])
    )
    .block(
        Block::builder(Ident::new("resource"))
            .labels(["aws_s3_bucket", "bucket2"])
    )
    .build();

let buckets: Vec<&Block> = body.get_blocks("resource")
    .filter(|block| block.has_exact_labels(&["aws_s3_bucket", "bucket1"]))
    .collect();

assert_eq!(
    buckets,
    [
        &Block::builder(Ident::new("resource"))
            .labels(["aws_s3_bucket", "bucket1"])
            .build(),
    ]
);

Trait Implementations§

Source§

impl Clone for Block

Source§

fn clone(&self) -> Block

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Block

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decorate for Block

Source§

fn decor(&self) -> &Decor

Returns a reference to the object’s Decor.
Source§

fn decor_mut(&mut self) -> &mut Decor

Returns a mutable reference to the object’s Decor.
Source§

fn decorate(&mut self, decor: impl Into<Decor>)

Decorate the object with decor in-place.
Source§

fn decorated(self, decor: impl Into<Decor>) -> Self
where Self: Sized,

Decorate the object with decor and return the modified value.
Source§

impl From<Block> for Structure

Source§

fn from(value: Block) -> Self

Converts to this type from the input type.
Source§

impl From<BlockBuilder> for Block

Source§

fn from(builder: BlockBuilder) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Block

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Span for Block

Source§

fn span(&self) -> Option<Range<usize>>

Obtains the span information. This only returns Some if the value was emitted by the parser. Read more
Source§

impl Eq for Block

Auto Trait Implementations§

§

impl Freeze for Block

§

impl RefUnwindSafe for Block

§

impl Send for Block

§

impl Sync for Block

§

impl Unpin for Block

§

impl UnwindSafe for Block

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.