Skip to content

Commit

Permalink
docs: add serialization example using the block! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
martinohmann committed Jun 10, 2022
1 parent 336187f commit 0661eba
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,66 @@
//! # Ok(())
//! # }
//! ```
//!
//! The same result could be acheived using the [`block!`] macro:
//!
//! ```
//! # use std::error::Error;
//! #
//! # fn main() -> Result<(), Box<dyn Error>> {
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct User {
//! age: u8,
//! username: &'static str,
//! email: &'static str,
//! }
//!
//! let users = vec![
//! User {
//! age: 34,
//! username: "johndoe",
//! email: "johndoe@example.com",
//! },
//! User {
//! age: 27,
//! username: "janedoe",
//! email: "janedoe@example.com",
//! },
//! ];
//!
//! let body: hcl::Body = users
//! .into_iter()
//! .map(|user| {
//! hcl::block! {
//! user (user.username) {
//! age = (user.age)
//! email = (user.email)
//! }
//! }
//! })
//! .collect();
//!
//! let expected = r#"
//! user "johndoe" {
//! age = 34
//! email = "johndoe@example.com"
//! }
//!
//! user "janedoe" {
//! age = 27
//! email = "janedoe@example.com"
//! }
//! "#
//! .trim_start();
//!
//! let serialized = hcl::to_string(&body).unwrap();
//!
//! assert_eq!(serialized, expected);
//! # Ok(())
//! # }
//! ```
mod escape;
mod format;
Expand Down

0 comments on commit 0661eba

Please sign in to comment.