hcl/
lib.rs

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