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§

source

type Context

The context used during formatting

Required Methods§

source

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" }]);
source

fn state(&self) -> &FormatState<Self::Context>

Returns the formatting state relevant for this formatting session.

source

fn state_mut(&mut self) -> &mut FormatState<Self::Context>

Returns the mutable formatting state relevant for this formatting session.

source

fn snapshot(&self) -> BufferSnapshot

Takes a snapshot of the Buffers state, excluding the formatter state.

source

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§

source

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

Implements the [Buffer] trait for all mutable references of objects implementing Buffer.

§

type Context = Context

source§

fn write_element(&mut self, element: FormatElement) -> FormatResult<()>

source§

fn write_fmt(&mut self, args: Arguments<'_, Context>) -> FormatResult<()>

source§

fn state(&self) -> &FormatState<Self::Context>

source§

fn state_mut(&mut self) -> &mut FormatState<Self::Context>

source§

fn snapshot(&self) -> BufferSnapshot

source§

fn restore_snapshot(&mut self, snapshot: BufferSnapshot)

Implementors§

source§

impl<'inner, Context, Inspector> Buffer for Inspect<'inner, Context, Inspector>
where Inspector: FnMut(&FormatElement),

§

type Context = Context

source§

impl<Context> Buffer for Formatter<'_, Context>

§

type Context = Context

source§

impl<Context> Buffer for RemoveSoftLinesBuffer<'_, Context>

§

type Context = Context

source§

impl<Context> Buffer for VecBuffer<'_, Context>

§

type Context = Context

source§

impl<Preamble, Context> Buffer for PreambleBuffer<'_, Preamble, Context>
where Preamble: Format<Context>,

§

type Context = Context