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§

source§

impl<'a> Formatter<'a, ()>

source

pub fn builder() -> FormatterBuilder<'a>

Creates a new FormatterBuilder to start building a new Formatter.

source§

impl<'a, W> Formatter<'a, W>
where W: Write,

source

pub fn new(writer: W) -> Formatter<'a, W>

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

source

pub fn into_inner(self) -> W

Takes ownership of the Formatter and returns the underlying writer.

Trait Implementations§

source§

impl<'a> Default for Formatter<'a, Vec<u8>>

source§

fn default() -> Self

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§

§

impl<'a, W> Freeze for Formatter<'a, W>
where W: Freeze,

§

impl<'a, W> RefUnwindSafe for Formatter<'a, W>
where W: RefUnwindSafe,

§

impl<'a, W> Send for Formatter<'a, W>
where W: Send,

§

impl<'a, W> Sync for Formatter<'a, W>
where W: Sync,

§

impl<'a, W> Unpin for Formatter<'a, W>
where W: Unpin,

§

impl<'a, W> UnwindSafe for Formatter<'a, W>
where W: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.