dynfmt

Trait Format

Source
pub trait Format<'f> {
    type Iter: Iterator<Item = ArgumentResult<'f>>;

    // Required method
    fn iter_args(&self, format: &'f str) -> Result<Self::Iter, Error<'f>>;

    // Provided method
    fn format<A>(
        &self,
        format: &'f str,
        arguments: A,
    ) -> Result<Cow<'f, str>, Error<'f>>
       where A: FormatArgs { ... }
}
Expand description

A format for string formatting.

This trait exposes formatting helper functions and lower-level utilities to interface with format strings.

In its core, a format can parse a format string and return an iterator over ArgumentSpecs. Each specification refers to arguments in and [argument list] and contains information on how to format it.

Required Associated Types§

Source

type Iter: Iterator<Item = ArgumentResult<'f>>

The iterator returned by iter_args.

Required Methods§

Source

fn iter_args(&self, format: &'f str) -> Result<Self::Iter, Error<'f>>

Returns an iterator over format arguments in the format string.

This method is not meant to be used directly, instead use some of the provided methods to format a string.

The iterator and this method are responsible for parsing the format string correctly and returning ArgumentSpecs for each argument in the format string. See the module level documentation for an example of how to implement this method.

Provided Methods§

Source

fn format<A>( &self, format: &'f str, arguments: A, ) -> Result<Cow<'f, str>, Error<'f>>
where A: FormatArgs,

Formats the given string with the specified arguments.

Individual arguments must implement Debug and serde::Serialize. The arguments container must implement the FormatArgs trait.

use dynfmt::{Format, NoopFormat};

let formatted = NoopFormat.format("hello, world", &["unused"]);
assert_eq!("hello, world", formatted.expect("formatting failed"));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§