hcl/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(missing_docs, clippy::pedantic)]
3#![allow(
4    clippy::cast_lossless,
5    clippy::cast_possible_truncation,
6    clippy::cast_possible_wrap,
7    clippy::cast_precision_loss,
8    clippy::cast_sign_loss,
9    clippy::enum_glob_use,
10    clippy::let_underscore_untyped,
11    clippy::match_wildcard_for_single_variants,
12    clippy::missing_panics_doc,
13    clippy::module_name_repetitions,
14    clippy::must_use_candidate,
15    clippy::needless_lifetimes,
16    clippy::needless_pass_by_value,
17    clippy::option_option,
18    clippy::return_self_not_must_use,
19    clippy::should_implement_trait,
20    clippy::struct_excessive_bools,
21    clippy::unnecessary_wraps,
22    clippy::wildcard_imports
23)]
24
25#[macro_use]
26mod macros;
27
28pub mod de;
29pub mod error;
30pub mod eval;
31pub mod expr;
32pub mod format;
33mod ident;
34pub mod ser;
35pub mod structure;
36pub mod template;
37#[cfg(test)]
38mod tests;
39mod util;
40pub mod value;
41
42pub use hcl_edit as edit;
43
44// Re-exported for convenience.
45#[doc(inline)]
46pub use hcl_primitives::{InternalString, Number};
47
48#[doc(inline)]
49pub use de::{from_body, from_reader, from_slice, from_str};
50
51#[doc(inline)]
52pub use error::{Error, Result};
53
54#[doc(inline)]
55pub use expr::{to_expression, Expression, Object, ObjectKey};
56
57// Deprecated, these re-exports will be removed in a future release.
58#[doc(hidden)]
59pub use expr::{
60    BinaryOp, BinaryOperator, Conditional, ForExpr, FuncCall, FuncCallBuilder, Heredoc,
61    HeredocStripMode, Operation, TemplateExpr, Traversal, TraversalOperator, UnaryOp,
62    UnaryOperator, Variable,
63};
64
65pub use ident::Identifier;
66
67#[doc(inline)]
68pub use ser::{to_string, to_vec, to_writer};
69
70#[doc(inline)]
71pub use structure::{Attribute, Block, BlockLabel, Body, Structure};
72
73// Deprecated, these re-exports will be removed in a future release.
74#[doc(hidden)]
75pub use structure::{BlockBuilder, BodyBuilder};
76
77#[doc(inline)]
78pub use template::Template;
79
80#[doc(inline)]
81pub use value::{from_value, to_value, Map, Value};
82
83/// Parse a `hcl::Body` from a `&str`.
84///
85/// If deserialization into a different type is preferred consider using [`hcl::from_str`][from_str].
86///
87/// [from_str]: ./de/fn.from_str.html
88///
89/// # Example
90///
91/// ```
92/// use hcl::{Attribute, Block, Body};
93///
94/// let input = r#"
95///     some_attr = "foo"
96///
97///     some_block "some_block_label" {
98///       attr = "value"
99///     }
100/// "#;
101///
102/// let expected = Body::builder()
103///     .add_attribute(("some_attr", "foo"))
104///     .add_block(
105///         Block::builder("some_block")
106///             .add_label("some_block_label")
107///             .add_attribute(("attr", "value"))
108///             .build()
109///     )
110///     .build();
111///
112/// let body = hcl::parse(input)?;
113///
114/// assert_eq!(body, expected);
115/// # Ok::<(), Box<dyn core::error::Error>>(())
116/// ```
117///
118/// # Errors
119///
120/// This function fails with an error if the `input` cannot be parsed as HCL.
121#[inline]
122pub fn parse(input: &str) -> Result<Body> {
123    input.parse()
124}