Mufmt is a minimal and extensible runtime formatting library.
Mufmt allows arbitrary types to define a formatting syntax and compiled template format.
Mufmt also provides a number of built-in formats, backed by data stored in collection types like HashMap or Vec.
Key features:
- Separate parsing and interpolation stages, generic over the intermediate representation.
- Very lightweight: one
Vecfor compiled templates and no allocations for one-off rendering. - Fine-grained span-based error reporting.
- Exactly one non-std dependency (including transitive dependencies): the venerable memchr.
Please read the API docs for more detail.
Render a template using values in a HashMap.
use mufmt::Template;
use std::collections::HashMap;
// The `Ast` is &str
let template = Template::<&str, &str>::compile("Hello {name}!").unwrap();
// The `Manifest` is `HashMap<str, str>`
let mfst = HashMap::from([("name", "John")]);
assert_eq!(template.render(&mfst).unwrap(), "Hello John!");Render a template by indexing into a Vec.
use mufmt::Template;
let s = "Order: {1}".to_owned();
// The `Ast` is usize; also use a String to store the template text
// which unlinks the lifetime
let template = Template::<String, usize>::compile(&s).unwrap();
// we can drop the original template string
drop(s);
// The `Manifest` is `Vec<&str>`
let mut mfst = vec!["Grapes", "Apples"];
assert_eq!(template.render(&mfst).unwrap(), "Order: Apples");
// Render again, but with new data
mfst.clear();
mfst.push("Cheese");
mfst.push("Milk");
assert_eq!(template.render(&mfst).unwrap(), "Order: Milk");
// You can even change the type, as long as the `Ast` is the same
let new_mfst = vec![12, 5];
assert_eq!(template.render(&new_mfst).unwrap(), "Order: 5");