#parameters #substitution #string #late #hash-map #name #substituting

yanked late_substitution

Late substitution of string parameters

2 stable releases

1.0.1 Oct 23, 2023

#6 in #late

MIT/Apache

6KB

Late substitution

This crate provides a simple way of substituting parameters in an arbitrary string, where parameter names are arbitrary.

This is an alternative to using complex template engines.

Example

use late_substitution::LateSubstitution;
use maplit::hashmap;

let user_string: String = "some user string: {id}".into();
assert_eq!(
    "some user string: x",
    user_string.late_substitution(hashmap!{"id".into() => "x".into()})
);

let user_string: String = r#"some user string: {"id"}"#.into();
assert_eq!(
    "some user string: id",
    user_string.late_substitution(hashmap!{"id".into() => "x".into()})
);

let user_string: String = r#"some user string: {  "id"  }"#.into();
assert_eq!(
    "some user string: id",
    user_string.late_substitution(hashmap!{"id".into() => "x".into()})
);

let user_string: String = "some user string: {id}".into();
assert_eq!(
    "some user string: None",
    user_string.late_substitution(hashmap!{})
);

Dependencies

~2.5–4MB
~71K SLoC