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
sourceimpl<'a, W> Formatter<'a, W>
impl<'a, W> Formatter<'a, W>
sourcepub fn new(writer: W) -> Formatter<'a, W>ⓘNotable traits for Formatter<'a, W>impl<'a, W> Write for Formatter<'a, W>where
W: Write,
pub fn new(writer: W) -> Formatter<'a, W>ⓘNotable traits for Formatter<'a, W>impl<'a, W> Write for Formatter<'a, W>where
W: Write,
W: Write,
Creates a new Formatter
which writes HCL to the provided writer.
sourcepub fn builder() -> FormatterBuilder<'a, W>
pub fn builder() -> FormatterBuilder<'a, W>
Creates a new FormatterBuilder
to start building a new Formatter
.
sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consumes self
and returns the wrapped writer.
Trait Implementations
sourceimpl<'a, W> Write for Formatter<'a, W>where
W: Write,
impl<'a, W> Write for Formatter<'a, W>where
W: Write,
sourcefn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this writer, returning how many bytes were written. Read more
sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more