Struct runtime_format::ParsedFmt
source · 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<'b, F: FormatKey>(
&'b self,
fmt: &'b F
) -> FormatArgs<'b, Self, F>
pub fn with_args<'b, F: FormatKey>(
&'b self,
fmt: &'b F
) -> FormatArgs<'b, Self, F>
Combine this parsed format with the given values into a FormatArgs
Trait Implementations§
source§impl<'a> ToFormatParser<'a> for ParsedFmt<'a>
impl<'a> ToFormatParser<'a> for ParsedFmt<'a>
§type Parser = Copied<Iter<'a, ParseSegment<'a>>>
type Parser = Copied<Iter<'a, ParseSegment<'a>>>
The Parser type that returns the
ParseSegments