interpolator

runtime format strings, fully compatible with std's macros

7 releases (4 breaking)

0.5.0 Apr 17, 2023
0.4.0 Mar 1, 2023
0.3.0 Mar 1, 2023
0.2.0 Feb 28, 2023
0.1.2 Feb 27, 2023

#106 in Template engine

Download history 69389/week @ 2024-06-19 90836/week @ 2024-06-26 79602/week @ 2024-07-03 83738/week @ 2024-07-10 87198/week @ 2024-07-17 88594/week @ 2024-07-24 82963/week @ 2024-07-31 95368/week @ 2024-08-07 112800/week @ 2024-08-14 109431/week @ 2024-08-21 97882/week @ 2024-08-28 124297/week @ 2024-09-04 125745/week @ 2024-09-11 120313/week @ 2024-09-18 132078/week @ 2024-09-25 108206/week @ 2024-10-02

512,390 downloads per month
Used in 253 crates (via attribute-derive-macro)

MIT/Apache

255KB
3.5K SLoC

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 std::collections::HashMap;
use template::{format, Formattable};

let formatted = format(
    "{value:+05}", // could be dynamic
    &[("value", Formattable::display(&12))].into_iter().collect::<HashMap<_,_>>(),
)
.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 std::collections::HashMap;
use template::{write, Formattable};
                                                                            
let mut buf = String::new();
write(
    &mut buf,
    "{value:+05}", // could be dynamic
    &[("value", Formattable::display(&12))].into_iter().collect::<HashMap<_,_>>(),
)
.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, '{}' is the array element)(the join)}, an empty join can also be omitted {list:i({})}. If join is omitted the format string {} can be omitted as well {list:i}.

Should you need to use ) inside your format string or join, you can add # similar to rust's raw string (i.e. #(({}))#).

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

It is also possible to index a single value by only specifying an isize {list:i1}.

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

// HashMap macro
use collection_literals::hash;
use interpolator::{format, Formattable};
// Needs to be a slice of references because `Formattable::display` expects 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`, `hi` and `hey`"
);
# return Ok::<(), interpolator::Error>(())

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
  • iter enables i trait specifier

No runtime deps