This crate provides functionality to deserialize and manipulate HCL data.
The main types are Deserializer
for deserializing data and Value
which can
be used to deserialize arbitrary HCL data.
Note: Serializing to HCL is not supported yet.
Deserialize arbitrary HCL according to the HCL JSON Specification:
```rust use serde_json::{json, Value};
let input = r#" some_attr = { foo = [1, 2] bar = true }
some_block "some_block_label" {
attr = "value"
}
"#;
let expected = json!({ "someattr": { "foo": [1, 2], "bar": true }, "someblock": { "someblocklabel": { "attr": "value" } } });
let value: Value = hcl::from_str(input).unwrap();
assert_eq!(value, expected); ```
If you need to preserve context about the HCL structure, deserialize into
hcl::Body
instead:
```rust use hcl::{Block, Body, Expression};
let input = r#" some_attr = { "foo" = [1, 2] "bar" = true }
some_block "some_block_label" {
attr = "value"
}
"#;
let expected = Body::builder() .addattribute(( "someattr", Expression::fromiter([ ("foo", Expression::from(vec![1, 2])), ("bar", Expression::Bool(true)), ]), )) .addblock( Block::builder("someblock") .addlabel("someblocklabel") .add_attribute(("attr", "value")) .build(), ) .build();
let body: Body = hcl::from_str(input).unwrap();
assert_eq!(body, expected); ```
The source code of hcl-rs is released under the MIT License. See the bundled LICENSE file for details.