interpolator

CI Status Documentation for <code>main</code> 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.

```rs 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.

```rs 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)); ```

i iter format

The feature iter enables an additional format trait i, it allows to format a list of values with a format string and an optional join expression.

The syntax is {list:i(the format string, '{it}' is the array element)(the optional join)}, an empty join can also be omitted {list:i({it})}. Should you need to use ) inside your format string or join, you can add # similar to rust's raw string.

It is also possible to only iterate a sub-slice specified through a range before the format string, i.e. {list:i1..4({it})}. For open ranges range bounds can also be omitted. To index from the end, you can use negative range bounds.

A Formattable implementing iter is created using Formattable::iter:

``rs // HashMap macro use collection_literals::hash; use interpolator::{format, Formattable}; // Needs to be a slice of references so becauseFormattable::displayexpects a // reference let items = [&"hello", &"hi", &"hey"].map(Formattable::display); let items = Formattable::iter(&items); let format_str = "Greetings: {items:i..-1({it})(, )} and {items:i-1..({it})}"; assert_eq!( format(format_str, &hash!("items" => items))?, "Greetings:hello,hiandhey`" );

return Ok::<(), interpolator::Error>(())

```

Features

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