dyn_formatting

Crates.iodyn_formatting
lib.rsdyn_formatting
version3.0.0
sourcesrc
created_at2023-01-23 02:10:20.064586
updated_at2023-01-25 05:52:12.77264
descriptionA library provides limited Python-style safe dynamic (runtime) formatting support for Rust
homepage
repositoryhttps://github.com/cup113/dyn_formatting
max_upload_size
id765470
size15,062
Jason Li (cup113)

documentation

https://docs.rs/dyn_formatting

README

Dynamic Formatting README

Lightweight, dynamic, Python-styled string formatting (Only support String, {key} patterns). It only needs std to work.

Escape like {{ or }}.

Install

cargo add dyn_formatting@3.0.0

Examples

use dyn_formatting::dynamic_format;
assert_eq!(
    dynamic_format(
        "I'm {name}. I'm {age} years old now.",
        &[("name", "ABC"), ("age", "20")].into()
    ).unwrap(),
    "I'm ABC. I'm 20 years old now.".to_string()
);
use dyn_formatting::dynamic_format;
use std::collections::HashMap;
let value_age = (15).to_string(); // Make lifetime long enough
let dictionary = HashMap::from([
    ("age", value_age.as_str()),
]);
assert_eq!(
    dynamic_format("{{{age} }}{age}", &dictionary).unwrap(),
    "{15 }15"
)
use dyn_formatting::dynamic_format;
assert!(
    dynamic_format(
        "I'm {name}. I'm {age} years old now.",
        &[("name", "ABC")].into()
    ).is_err() // Key error
);
use dyn_formatting::dynamic_format;
assert!(
    dynamic_format(
        "I'm {name{name}}.",
        &[("name", "ABC")].into()
    ).is_err() // Token error: '{' unmatched.
);
Commit count: 10

cargo fmt