Trait biome_formatter::Format
source · pub trait Format<Context> {
// Required method
fn fmt(&self, f: &mut Formatter<'_, Context>) -> FormatResult<()>;
}
Expand description
Formatting trait for types that can create a formatted representation. The biome_formatter
equivalent
to std::fmt::Display.
§Example
Implementing Format
for a custom struct
use biome_formatter::{format, write, IndentStyle, LineWidth};
use biome_formatter::prelude::*;
use biome_rowan::TextSize;
struct Paragraph(String);
impl Format<SimpleFormatContext> for Paragraph {
fn fmt(&self, f: &mut Formatter<SimpleFormatContext>) -> FormatResult<()> {
write!(f, [
hard_line_break(),
dynamic_text(&self.0, TextSize::from(0)),
hard_line_break(),
])
}
}
let paragraph = Paragraph(String::from("test"));
let formatted = format!(SimpleFormatContext::default(), [paragraph])?;
assert_eq!("test\n", formatted.print()?.as_code());
Required Methods§
sourcefn fmt(&self, f: &mut Formatter<'_, Context>) -> FormatResult<()>
fn fmt(&self, f: &mut Formatter<'_, Context>) -> FormatResult<()>
Formats the object using the given formatter.