Trait biome_formatter::Buffer
source · pub trait Buffer {
type Context;
// Required methods
fn write_element(&mut self, element: FormatElement) -> FormatResult<()>;
fn state(&self) -> &FormatState<Self::Context>;
fn state_mut(&mut self) -> &mut FormatState<Self::Context>;
fn snapshot(&self) -> BufferSnapshot;
fn restore_snapshot(&mut self, snapshot: BufferSnapshot);
// Provided method
fn write_fmt(
&mut self,
arguments: Arguments<'_, Self::Context>
) -> FormatResult<()> { ... }
}
Expand description
A trait for writing or formatting into FormatElement-accepting buffers or streams.
Required Associated Types§
Required Methods§
sourcefn write_element(&mut self, element: FormatElement) -> FormatResult<()>
fn write_element(&mut self, element: FormatElement) -> FormatResult<()>
Writes a crate::FormatElement into this buffer, returning whether the write succeeded.
§Errors
This function will return an instance of crate::FormatError on error.
§Examples
use biome_formatter::{Buffer, FormatElement, FormatState, SimpleFormatContext, VecBuffer};
let mut state = FormatState::new(SimpleFormatContext::default());
let mut buffer = VecBuffer::new(&mut state);
buffer.write_element(FormatElement::StaticText { text: "test"}).unwrap();
assert_eq!(buffer.into_vec(), vec![FormatElement::StaticText { text: "test" }]);
sourcefn state(&self) -> &FormatState<Self::Context>
fn state(&self) -> &FormatState<Self::Context>
Returns the formatting state relevant for this formatting session.
sourcefn state_mut(&mut self) -> &mut FormatState<Self::Context>
fn state_mut(&mut self) -> &mut FormatState<Self::Context>
Returns the mutable formatting state relevant for this formatting session.
sourcefn snapshot(&self) -> BufferSnapshot
fn snapshot(&self) -> BufferSnapshot
Takes a snapshot of the Buffers state, excluding the formatter state.
sourcefn restore_snapshot(&mut self, snapshot: BufferSnapshot)
fn restore_snapshot(&mut self, snapshot: BufferSnapshot)
Restores the snapshot buffer
§Panics
If the passed snapshot id is a snapshot of another buffer OR if the snapshot is restored out of order
Provided Methods§
sourcefn write_fmt(
&mut self,
arguments: Arguments<'_, Self::Context>
) -> FormatResult<()>
fn write_fmt( &mut self, arguments: Arguments<'_, Self::Context> ) -> FormatResult<()>
Glue for usage of the write!
macro with implementors of this trait.
This method should generally not be invoked manually, but rather through the write!
macro itself.
§Examples
use biome_formatter::prelude::*;
use biome_formatter::{Buffer, FormatState, SimpleFormatContext, VecBuffer, format_args};
let mut state = FormatState::new(SimpleFormatContext::default());
let mut buffer = VecBuffer::new(&mut state);
buffer.write_fmt(format_args!(text("Hello World"))).unwrap();
assert_eq!(buffer.into_vec(), vec![FormatElement::StaticText{ text: "Hello World" }]);
Implementations on Foreign Types§
source§impl<W: Buffer<Context = Context> + ?Sized, Context> Buffer for &mut W
impl<W: Buffer<Context = Context> + ?Sized, Context> Buffer for &mut W
Implements the [Buffer]
trait for all mutable references of objects implementing Buffer.