interpolator 0.1.2

runtime format strings, fully compatible with std's macros
Documentation

interpolator

CI Status Documentation for main Crates.io Docs.rs

Runtime implementation of format!.

format

Runtime version of format!.

Takes a string and a context, containing Formattable values, returns a string.

use template::{format, Formattable};

let formatted = format(
    "{value:+05}", // could be dynamic
    &[("value", Formattable::display(&12))].into_iter().collect(),
)
.unwrap();

assert_eq!(formatted, format!("{:+05}", 12));

write

Runtime version of write!.

Takes a mutable Write e.g. &mut String, a format string and a context, containing Formattable values.

use template::{write, Formattable};
                                                                            
let mut buf = String::new();
write(
    &mut buf,
    "{value:+05}", // could be dynamic
    &[("value", Formattable::display(&12))].into_iter().collect(),
)
.unwrap();
                                                                            
assert_eq!(buf, format!("{:+05}", 12));

Features

By default only Display is supported, the rest of the formatting traits can be enabled through the following features.

  • debug enables ?, x? and X? trait specifiers
  • number enables x, X, b, o, e and E trait specifiers
  • pointer enables p trait specifiers