5 unstable releases

0.2.1 Nov 29, 2022
0.2.0 Apr 2, 2020
0.1.1 Jun 4, 2019
0.1.0 May 28, 2019
0.0.1 May 14, 2019

#666 in Rust patterns

Download history 497/week @ 2024-07-19 835/week @ 2024-07-26 691/week @ 2024-08-02 985/week @ 2024-08-09 687/week @ 2024-08-16 801/week @ 2024-08-23 773/week @ 2024-08-30 1264/week @ 2024-09-06 803/week @ 2024-09-13 656/week @ 2024-09-20 791/week @ 2024-09-27 876/week @ 2024-10-04 744/week @ 2024-10-11 810/week @ 2024-10-18 904/week @ 2024-10-25 1220/week @ 2024-11-01

3,774 downloads per month
Used in 7 crates

MIT/Apache

8KB
106 lines

envsubst

crates.io Documentation

A simple Rust library for variables substitution.

This library provide helper functions for string manipulation, taking values from a context environment map and substituting all matching placeholders.

Its name and logic is similar to the envsubst GNU utility, but this only supports braces-delimited variables (i.e. ${foo}) and takes replacement values from an explicit map of variables.

License

Licensed under either of

at your option.


lib.rs:

Variables substitution in string templates.

This library provide helper functions for string manipulation, taking values from a context environment map and substituting all matching placeholders.

Its name and logic is similar to the envsubst GNU utility, but this only supports braces-delimited variables (i.e. ${foo}) and takes replacement values from an explicit map of variables.

Example

let base_url = "${protocol}://${hostname}/${endpoint}";
assert!(envsubst::is_templated(base_url));

let mut context = std::collections::HashMap::new();
context.insert("protocol".to_string(), "https".to_string());
context.insert("hostname".to_string(), "example.com".to_string());
context.insert("endpoint".to_string(), "login".to_string());
assert!(envsubst::validate_vars(&context).is_ok());

let final_url = envsubst::substitute(base_url, &context).unwrap();
assert!(!envsubst::is_templated(&final_url));
assert_eq!(final_url, "https://example.com/login");

Dependencies

~240–700KB
~16K SLoC