Struct hcl::format::Formatter

source ·
pub struct Formatter<'a, W> { /* private fields */ }
Expand description

A pretty printing HCL formatter.

Examples

Format an HCL block as string:

use hcl::format::{Format, Formatter};

let mut buf = Vec::new();
let mut formatter = Formatter::new(&mut buf);

let block = hcl::Block::builder("user")
    .add_label("johndoe")
    .add_attribute(("age", 34))
    .add_attribute(("email", "johndoe@example.com"))
    .build();

block.format(&mut formatter)?;

let expected = r#"
user "johndoe" {
  age = 34
  email = "johndoe@example.com"
}
"#.trim_start();

let formatted = String::from_utf8(buf)?;

assert_eq!(formatted, expected);

The builder() method can be used to construct a custom Formatter for use with a Serializer:

use hcl::{format::Formatter, ser::Serializer};

let formatter = Formatter::builder()
    .indent(b"  ")
    .dense(false)
    .build(&mut writer);

let ser = Serializer::with_formatter(formatter);

Implementations§

Creates a new FormatterBuilder to start building a new Formatter.

Creates a new Formatter which writes HCL to the provided writer.

Takes ownership of the Formatter and returns the underlying writer.

Trait Implementations§

Creates the default Formatter which is specialized to use a pre-allocated Vec<u8> as internal buffer.

The formatter can be passed to the format_string or format_vec method of types implementing Format.

Alternatively, the internal buffer can be obtained by calling into_inner after passing it to the format method of a type implementing Format.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.