From 0661ebaa834edb58cfff83b604ddc3ddc66dac4a Mon Sep 17 00:00:00 2001 From: martinohmann Date: Fri, 10 Jun 2022 13:38:48 +0200 Subject: [PATCH] docs: add serialization example using the `block!` macro --- src/ser/mod.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index be30e34a..2f6a787d 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -125,6 +125,66 @@ //! # Ok(()) //! # } //! ``` +//! +//! The same result could be acheived using the [`block!`] macro: +//! +//! ``` +//! # use std::error::Error; +//! # +//! # fn main() -> Result<(), Box> { +//! 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;