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), parenthesized expressions ((key_expr)) or raw HCL expressions (#{raw_expr}). - Attribute expression values can be any valid primitive, collection, expression or raw HCL
expression (
#{raw_expr}).
Please note that HCL actually uses ${} for interpolating expression, but since rust macros do
not support matching on $ it was chosen to use #{} instead to support raw expressions.
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 anAttributeblock!: constructs aBlockexpression!: constructs anExpressionstructure!: 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);