Function biome_formatter::write

source ·
pub fn write<Context>(
    output: &mut dyn Buffer<Context = Context>,
    args: Arguments<'_, Context>
) -> FormatResult<()>
Expand description

The write function takes a target buffer and an Arguments struct that can be precompiled with the format_args! macro.

The arguments will be formatted in-order into the output buffer provided.

§Examples

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

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

write!(&mut buffer, [format_args!(text("Hello World"))])?;

let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default());

assert_eq!("Hello World", formatted.print()?.as_code());

Please note that using write! might be preferable. Example:

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

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

write!(&mut buffer, [text("Hello World")])?;

let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default());

assert_eq!("Hello World", formatted.print()?.as_code());