Struct Formatter

Source
pub struct Formatter<'s> { /* private fields */ }
Expand description

A formatter that applies format specifications to values.

The Formatter struct collects arguments and applies format specifications to them to build the final formatted string. It collects all arguments first using Formatter::push_arg and then performs the formatting in one operation with the Formatter::format method.

§Examples

Basic usage with a format string:

use dyf::{FormatString, Formatter};

let fmt = FormatString::from_string("Hello, {}!".to_string()).unwrap();
let mut formatter = Formatter::from(&fmt);
formatter.push_arg(&"world").format().unwrap();
assert_eq!(formatter.into_string(), "Hello, world!");

Formatting multiple values:

use dyf::{FormatString, Formatter};

let fmt = FormatString::from_string("{}, {}!".to_string()).unwrap();
let mut formatter = Formatter::from(&fmt);
formatter.push_arg(&"Hello").push_arg(&"world").format().unwrap();
assert_eq!(formatter.into_string(), "Hello, world!");

Using with custom types:

use dyf::{FormatString, Formatter, DynDisplay, Error, FormatSpec};

struct Point {
    x: i32,
    y: i32,
}

impl DynDisplay for Point {
    fn dyn_fmt(&self, f: &FormatSpec) -> Result<String, Error> {
        let s = format!("Point({}, {})", self.x, self.y);
        Ok(f.fill_and_align(s, dyf::Align::Left))
    }
}

let fmt = FormatString::from_string("Point: {}".to_string()).unwrap();
let point = Point { x: 10, y: 20 };
let mut formatter = Formatter::from(&fmt);
formatter.push_arg(&point).format().unwrap();
assert_eq!(formatter.into_string(), "Point: Point(10, 20)");

Implementations§

Source§

impl<'s> Formatter<'s>

Source

pub fn push_arg<A>(&mut self, arg: &'s A) -> &mut Self
where A: DynDisplay,

Adds an argument to be formatted.

This method collects arguments that will be formatted when Formatter::format is called. It supports method chaining for convenient use.

§Arguments
  • arg - The argument to format, which must implement DynDisplay
§Returns

A mutable reference to the formatter for method chaining.

§Examples
use dyf::{FormatString, Formatter};

let fmt = FormatString::from_string("{}, {}!".to_string()).unwrap();
let mut formatter = Formatter::from(&fmt);
formatter.push_arg(&"Hello").push_arg(&"world");
Source

pub fn format(&mut self) -> Result<&mut Self, Error>

Applies the format specifications to all collected arguments.

This method performs the actual formatting after all arguments have been collected with Formatter::push_arg. It verifies that the number of arguments matches the number of format specifications in the format string.

§Returns

A mutable reference to the formatter for method chaining.

§Errors

Returns an error if the number of arguments doesn’t match the number of format specifications, or if any individual formatting operation fails.

§Examples
use dyf::{FormatString, Formatter};

let fmt = FormatString::from_string("{:>5}, {:<5}".to_string()).unwrap();
let mut formatter = Formatter::from(&fmt);
formatter.push_arg(&42).push_arg(&"hello").format().unwrap();
assert_eq!(formatter.into_string(), "   42, hello");
Source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Returns a borrowed version of the formatted string.

This provides access to the current state of the formatted string without consuming the formatter.

§Examples
use dyf::{FormatString, Formatter};

let fmt = FormatString::from_string("Value: {}".to_string()).unwrap();
let mut formatter = Formatter::from(&fmt);
formatter.push_arg(&42).format().unwrap();
let borrowed = formatter.to_string_lossy();
assert_eq!(&*borrowed, "Value: 42");
Source

pub fn into_string(self) -> String

Consumes the formatter and returns the formatted string.

This finalizes the formatting process and returns the complete formatted string.

§Examples
use dyf::{FormatString, Formatter};

let fmt = FormatString::from_string("The answer is: {}".to_string()).unwrap();
let mut formatter = Formatter::from(&fmt);
formatter.push_arg(&42).format().unwrap();
let result = formatter.into_string();
assert_eq!(result, "The answer is: 42");

Trait Implementations§

Source§

impl<'s> From<&'s FormatString> for Formatter<'s>

Source§

fn from(value: &'s FormatString) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'s> Freeze for Formatter<'s>

§

impl<'s> !RefUnwindSafe for Formatter<'s>

§

impl<'s> !Send for Formatter<'s>

§

impl<'s> !Sync for Formatter<'s>

§

impl<'s> Unpin for Formatter<'s>

§

impl<'s> !UnwindSafe for Formatter<'s>

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.