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>
impl<'s> Formatter<'s>
Sourcepub fn push_arg<A>(&mut self, arg: &'s A) -> &mut Selfwhere
A: DynDisplay,
pub fn push_arg<A>(&mut self, arg: &'s A) -> &mut Selfwhere
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 implementDynDisplay
§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");
Sourcepub fn format(&mut self) -> Result<&mut Self, Error>
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");
Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
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");
Sourcepub fn into_string(self) -> String
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");