pub struct PreambleBuffer<'buf, Preamble, Context> { /* private fields */ }
Expand description

This struct wraps an existing buffer and emits a preamble text when the first text is written.

This can be useful if you, for example, want to write some content if what gets written next isn’t empty.

§Examples

use biome_formatter::{FormatState, Formatted, PreambleBuffer, SimpleFormatContext, VecBuffer, write};
use biome_formatter::prelude::*;

struct Preamble;

impl Format<SimpleFormatContext> for Preamble {
    fn fmt(&self, f: &mut Formatter<SimpleFormatContext>) -> FormatResult<()> {
        write!(f, [text("# heading"), hard_line_break()])
    }
}

let mut state = FormatState::new(SimpleFormatContext::default());
let mut buffer = VecBuffer::new(&mut state);

{
    let mut with_preamble = PreambleBuffer::new(&mut buffer, Preamble);

    write!(&mut with_preamble, [text("this text will be on a new line")])?;
}

let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default());
assert_eq!("# heading\nthis text will be on a new line", formatted.print()?.as_code());

The pre-amble does not get written if no content is written to the buffer.

use biome_formatter::{FormatState, Formatted, PreambleBuffer, SimpleFormatContext, VecBuffer, write};
use biome_formatter::prelude::*;

struct Preamble;

impl Format<SimpleFormatContext> for Preamble {
    fn fmt(&self, f: &mut Formatter<SimpleFormatContext>) -> FormatResult<()> {
        write!(f, [text("# heading"), hard_line_break()])
    }
}

let mut state = FormatState::new(SimpleFormatContext::default());
let mut buffer = VecBuffer::new(&mut state);
{
    let mut with_preamble = PreambleBuffer::new(&mut buffer, Preamble);
}

let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default());
assert_eq!("", formatted.print()?.as_code());

Implementations§

source§

impl<'buf, Preamble, Context> PreambleBuffer<'buf, Preamble, Context>

source

pub fn new( inner: &'buf mut dyn Buffer<Context = Context>, preamble: Preamble ) -> Self

source

pub fn did_write_preamble(&self) -> bool

Returns true if the preamble has been written, false otherwise.

Trait Implementations§

source§

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

§

type Context = Context

The context used during formatting
source§

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

Writes a crate::FormatElement into this buffer, returning whether the write succeeded. Read more
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 Read more
source§

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

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl<'buf, Preamble, Context> Freeze for PreambleBuffer<'buf, Preamble, Context>
where Preamble: Freeze,

§

impl<'buf, Preamble, Context> !RefUnwindSafe for PreambleBuffer<'buf, Preamble, Context>

§

impl<'buf, Preamble, Context> !Send for PreambleBuffer<'buf, Preamble, Context>

§

impl<'buf, Preamble, Context> !Sync for PreambleBuffer<'buf, Preamble, Context>

§

impl<'buf, Preamble, Context> Unpin for PreambleBuffer<'buf, Preamble, Context>
where Preamble: Unpin,

§

impl<'buf, Preamble, Context> !UnwindSafe for PreambleBuffer<'buf, Preamble, Context>

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> BufferExtensions for T
where T: Buffer,

source§

fn inspect<F>(&mut self, inspector: F) -> Inspect<'_, Self::Context, F>
where F: FnMut(&FormatElement),

Returns a new buffer that calls the passed inspector for every element that gets written to the output
source§

fn start_recording(&mut self) -> Recording<'_, Self>

Starts a recording that gives you access to all elements that have been written between the start and end of the recording Read more
source§

fn write_elements<I>(&mut self, elements: I) -> FormatResult<()>
where I: IntoIterator<Item = FormatElement>,

Writes a sequence of elements into this buffer.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more