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

#211 in Rust patterns

Download history 312/week @ 2022-11-28 489/week @ 2022-12-05 399/week @ 2022-12-12 292/week @ 2022-12-19 105/week @ 2022-12-26 227/week @ 2023-01-02 389/week @ 2023-01-09 238/week @ 2023-01-16 369/week @ 2023-01-23 373/week @ 2023-01-30 404/week @ 2023-02-06 409/week @ 2023-02-13 200/week @ 2023-02-20 223/week @ 2023-02-27 492/week @ 2023-03-06 193/week @ 2023-03-13

1,158 downloads per month
Used in 5 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

~0.7–1MB
~26K SLoC