pub struct ParsedFmt<'a> { /* private fields */ }
Expand description
Preparsed formatting terms.
This is faster if you will be using the same format string again and again with different inputs.
use runtime_format::{FormatArgs, FormatKey, FormatKeyError, ParsedFmt};
use core::fmt;
impl FormatKey for DateTime {
fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
// ...
}
}
let now = DateTime::now();
let fmt = ParsedFmt::new("{month} {day} {year} {hours}:{minutes}:{seconds}").unwrap();
let args = FormatArgs::new(&fmt, &now);
let expected = "Jan 25 2023 16:27:53";
assert_eq!(args.to_string(), expected);
Implementations§
Source§impl<'a> ParsedFmt<'a>
impl<'a> ParsedFmt<'a>
Sourcepub fn new(s: &'a str) -> Result<Self, FormatError<'a>>
pub fn new(s: &'a str) -> Result<Self, FormatError<'a>>
Parse the given format string.
§Errors
If the string could not be parsed, or there is a key that is unacceptable.
Sourcepub fn keys(&self) -> impl Iterator<Item = &str>
pub fn keys(&self) -> impl Iterator<Item = &str>
Return the keys that will be used when formatting.
let fmt = "Hello, {recipient}. Hope you are having a nice {time_descriptor}.";
let parsed = ParsedFmt::new(fmt).unwrap();
let keys: Vec<_> = parsed.keys().collect();
assert_eq!(keys, ["recipient", "time_descriptor"]);
Sourcepub fn with_args<'fs, 'fk, F: FormatKey>(
&'fs self,
fmt: &'fk F,
) -> FormatArgs<'fs, 'fk, Self, F>
pub fn with_args<'fs, 'fk, F: FormatKey>( &'fs self, fmt: &'fk F, ) -> FormatArgs<'fs, 'fk, Self, F>
Combine this parsed format with the given values into a FormatArgs
Trait Implementations§
Source§impl<'a> FromIterator<ParseSegment<'a>> for ParsedFmt<'a>
impl<'a> FromIterator<ParseSegment<'a>> for ParsedFmt<'a>
Source§fn from_iter<T: IntoIterator<Item = ParseSegment<'a>>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = ParseSegment<'a>>>(iter: T) -> Self
Creates a value from an iterator. Read more
Source§impl<'a> ToFormatParser<'a> for ParsedFmt<'a>
impl<'a> ToFormatParser<'a> for ParsedFmt<'a>
Source§type Parser = Copied<Iter<'a, ParseSegment<'a>>>
type Parser = Copied<Iter<'a, ParseSegment<'a>>>
The Parser type that returns the
ParseSegment
sAuto Trait Implementations§
impl<'a> Freeze for ParsedFmt<'a>
impl<'a> RefUnwindSafe for ParsedFmt<'a>
impl<'a> Send for ParsedFmt<'a>
impl<'a> Sync for ParsedFmt<'a>
impl<'a> Unpin for ParsedFmt<'a>
impl<'a> UnwindSafe for ParsedFmt<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more