macro_rules! body {
($($body:tt)*) => { ... };
}
Expand description
Construct an hcl::Body
from HCL blocks and attributes.
The macro supports a subset of the HCL syntax. If you need more flexibility, use the
BlockBuilder
instead.
§Supported Syntax
- Attribute keys and block identifiers can be raw identifiers (
identifier
) or parenthesized expressions ((expr)
). - Block labels can be string literals (
"label"
), identifiers (label
) or parenthesized expressions ((label_expr)
). - Object keys can be string literals (
"key"
), identifiers (key
) or parenthesized expressions ((key_expr)
). - Attribute expression values can be any valid primitive, collection or expression.
§Unsupported syntax
Heredocs are not supported by the hcl::body
macro.
§Related macros
The body!
macro is composed out of different other macros that can be used on their own to
construct HCL data structures:
attribute!
: constructs anAttribute
block!
: constructs aBlock
expression!
: constructs anExpression
structure!
: constructs aStructure
Additionally the value!
macro is provided for constructing a
Value
.
§Example
use hcl::{Block, Body};
let body = hcl::body!({
resource "aws_sns_topic" "topic" {
name = "my-topic"
}
});
let expected = Body::builder()
.add_block(
Block::builder("resource")
.add_label("aws_sns_topic")
.add_label("topic")
.add_attribute(("name", "my-topic"))
.build()
)
.build();
assert_eq!(body, expected);
Attribute keys, block identifiers and object keys can be expressions by wrapping them in parenthesis:
use hcl::{Block, Body};
let block_identifier = "resource";
let attribute_key = "name";
let body = hcl::body!({
(block_identifier) "aws_sns_topic" "topic" {
(attribute_key) = "my-topic"
}
});
let expected = Body::builder()
.add_block(
Block::builder(block_identifier)
.add_label("aws_sns_topic")
.add_label("topic")
.add_attribute((attribute_key, "my-topic"))
.build()
)
.build();
assert_eq!(body, expected);