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

Source

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.

Source

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"]);
Source

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 Debug for ParsedFmt<'_>

Source§

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

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

impl<'a> FromIterator<ParseSegment<'a>> for ParsedFmt<'a>

Source§

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>

Source§

type Parser = Copied<Iter<'a, ParseSegment<'a>>>

The Parser type that returns the ParseSegments
Source§

fn to_parser(&'a self) -> Self::Parser

Turn this value into the parser
Source§

fn unparsed(_: Self::Parser) -> &'a str

Get the unparsed str from this parser. Used to determine if there was an error while parsing.
Source§

impl<'a> TryFrom<&'a str> for ParsedFmt<'a>

Source§

type Error = FormatError<'a>

The type returned in the event of a conversion error.
Source§

fn try_from(value: &'a str) -> Result<Self, Self::Error>

Performs the conversion.

Auto 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> 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.