Skip to content

autobib/mufmt

Repository files navigation

Current crates.io release Documentation

Mufmt

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:

Please read the API docs for more detail.

Examples

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");

About

a minimal and extensible runtime formatting library for Rust

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages