Skip to main content

FormatterBuilder

Struct FormatterBuilder 

Source
pub struct FormatterBuilder { /* private fields */ }
Expand description

Formatter safe builder.

The safe and stable builder for fmt::Formatter

Due to unstable Formatter::new, uses this crate to dynamically build a Formatter.

§Examples

use formatter_builder::{FormatterBuilder, Fill, Sign, Alignment};

let mut output = String::new();
let n = 6.23;
formatter_builder::FormatterBuilder::new()
    .width(8)
    .align(Alignment::Right)
    .precision(3)
    .fill(Fill::Zero)
    .with(&mut output, |f| {
        std::fmt::Display::fmt(&n, f)
    })
    .unwrap();
assert_eq!(output, "0006.230")

Implementations§

Source§

impl FormatterBuilder

Source

pub fn with<W, F>(&self, writer: W, f: F) -> Result
where W: Write, F: FnOnce(&mut Formatter<'_>) -> Result,

Source§

impl FormatterBuilder

Source

pub fn new() -> Self

Source

pub fn sign(&mut self, sign: impl Into<Option<Sign>>) -> &mut Self

Format like {:+} and {:-}

§Examples
use std::fmt::Display;
let mut writter = String::new();
FormatterBuilder::new().sign(Sign::Plus).with(&mut writter, |f| {
    2i32.fmt(f);
    Ok(())
}).unwrap();
assert_eq!(writter, "+2");
Source

pub fn sign_aware_zero_pad(&mut self, sign_aware_zero_pad: bool) -> &mut Self

Format like {:0}

§Examples
FormatterBuilder::new().sign_aware_zero_pad(true).with(writter, |f| {
    assert!(f.sign_aware_zero_pad());
    Ok(())
}).unwrap();
Source

pub fn alternate(&mut self, alternate: bool) -> &mut Self

Format like {:#}

§Examples
FormatterBuilder::new().alternate(true).with(writter, |f| {
    assert!(f.alternate());
    Ok(())
}).unwrap();
Source

pub fn fill(&mut self, fill: impl Into<Option<Fill>>) -> &mut Self

Format like {:0>} {: ^} etc

§Panics
  • panic when align is unset
§Examples
FormatterBuilder::new().align(Left).fill(Fill::Space).with(writter, |f| {
    assert_eq!(f.fill(), ' ');
    Ok(())
}).unwrap();
Source

pub fn align(&mut self, align: impl Into<Option<Alignment>>) -> &mut Self

Format like {:<} and {:^} and {:>}

§Examples
FormatterBuilder::new().align(Center).with(writter, |f| {
    assert_eq!(f.align(), Some(Center));
    Ok(())
}).unwrap();
Source

pub fn width(&mut self, width: impl Into<Option<u16>>) -> &mut Self

Format like {:3}

§Examples
use std::fmt::Display;
let mut writter = String::new();
FormatterBuilder::new().width(3).with(&mut writter, |f| {
    2i32.fmt(f);
    Ok(())
}).unwrap();
assert_eq!(writter, "  2");
Source

pub fn precision(&mut self, precision: impl Into<Option<u16>>) -> &mut Self

Format like {:.3}

§Examples
use std::fmt::Display;
let mut writter = String::new();
FormatterBuilder::new().precision(3).with(&mut writter, |f| {
    2f32.fmt(f);
    Ok(())
}).unwrap();
assert_eq!(writter, "2.000");

Trait Implementations§

Source§

impl Debug for FormatterBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FormatterBuilder

Source§

fn default() -> FormatterBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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>,

Source§

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>,

Source§

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.