Struct hcl::structure::Body

source ·
pub struct Body(pub Vec<Structure>);
Expand description

Represents an HCL config file body.

A Body consists of zero or more Attribute and Block HCL structures.

Tuple Fields§

§0: Vec<Structure>

Implementations§

Consumes self and returns the wrapped Vec<Structure>.

Creates a new BodyBuilder to start building a new Body.

An iterator visiting all structures within the Body. The iterator element type is &'a Structure.

Examples
use hcl::{Attribute, Body};

let body = Body::from([
    Attribute::new("a", 1),
    Attribute::new("b", 2),
    Attribute::new("c", 3),
]);

for structure in body.iter() {
    println!("{structure:?}");
}

An iterator visiting all structures within the Body. The iterator element type is &'a mut Structure.

Examples
use hcl::{Attribute, Block, Body, Identifier, Structure};

let mut body = Body::from([
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

// Update all attribute keys and block identifiers
for structure in body.iter_mut() {
    match structure {
        Structure::Attribute(attr) => {
            attr.key = Identifier::new(format!("attr_{}", attr.key)).unwrap();
        }
        Structure::Block(block) => {
            block.identifier = Identifier::new(format!("block_{}", block.identifier)).unwrap();
        }
    }
}

assert_eq!(body.into_inner(), [
    Structure::Attribute(Attribute::new("attr_a", 1)),
    Structure::Block(Block::new("block_b")),
    Structure::Attribute(Attribute::new("attr_c", 3)),
]);

An iterator visiting all attributes within the Body. The iterator element type is &'a Attribute.

Examples
use hcl::{Attribute, Block, Body, Structure};

let body = Body::from([
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

let vec: Vec<&Attribute> = body.attributes().collect();
assert_eq!(vec, [&Attribute::new("a", 1), &Attribute::new("c", 3)]);

An iterator visiting all attributes within the Body. The iterator element type is &'a mut Attribute.

Examples
use hcl::{Attribute, Block, Body, Identifier, Structure};

let mut body = Body::from([
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

// Update all attribute keys
for attr in body.attributes_mut() {
    attr.key = Identifier::new(format!("attr_{}", attr.key)).unwrap();
}

assert_eq!(body.into_inner(), [
    Structure::Attribute(Attribute::new("attr_a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("attr_c", 3)),
]);

Creates a consuming iterator visiting all attributes within the Body. The object cannot be used after calling this. The iterator element type is Attribute.

Examples
use hcl::{Attribute, Block, Body, Structure};

let body = Body::from([
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

let vec: Vec<Attribute> = body.into_attributes().collect();
assert_eq!(vec, [Attribute::new("a", 1), Attribute::new("c", 3)]);

An iterator visiting all blocks within the Body. The iterator element type is &'a Block.

Examples
use hcl::{Attribute, Block, Body, Structure};

let body = Body::from([
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

let vec: Vec<&Block> = body.blocks().collect();
assert_eq!(vec, [&Block::new("b")]);

An iterator visiting all blocks within the Body. The iterator element type is &'a mut Block.

Examples
use hcl::{Attribute, Block, Body, Identifier, Structure};

let mut body = Body::from([
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

// Update all block identifiers
for block in body.blocks_mut() {
    block.identifier = Identifier::new(format!("block_{}", block.identifier)).unwrap();
}

assert_eq!(body.into_inner(), [
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("block_b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

Creates a consuming iterator visiting all blocks within the Body. The object cannot be used after calling this. The iterator element type is Block.

Examples
use hcl::{Attribute, Block, Body, Structure};

let body = Body::from([
    Structure::Attribute(Attribute::new("a", 1)),
    Structure::Block(Block::new("b")),
    Structure::Attribute(Attribute::new("c", 3)),
]);

let vec: Vec<Block> = body.into_blocks().collect();
assert_eq!(vec, [Block::new("b")]);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
The type that is returned by evaluate on success.
Recursively evaluates all HCL templates and expressions in the implementing type using the variables and functions declared in the Context. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Formats a HCL structure using a formatter and writes the result to the provided writer. Read more
Formats a HCL structure using a formatter and returns the result as a Vec<u8>. Read more
Formats a HCL structure using a formatter and returns the result as a String. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Creates a value from an iterator. Read more
The type of the deserializer being converted into.
Convert this value into a deserializer.
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.