pub struct Substituter<'a> { /* private fields */ }Expand description
Performs value substitution on a string value.
Supports {{VAR_NAME}} syntax. Escaping is supported via \{\{ and \}\}.
Values are looked up via a ValueResolver — EnvValueResolver
by default, backed by std::env::var.
Used by real, technology-specific LoaderOps implementors (see
configbuilder-svc-loader-spi) to apply substitution after parsing a config file.
Implementations§
Source§impl<'a> Substituter<'a>
impl<'a> Substituter<'a>
Sourcepub fn new(
policy: &'a dyn SubstitutionPolicy,
resolver: &'a dyn ValueResolver,
location: String,
) -> Self
pub fn new( policy: &'a dyn SubstitutionPolicy, resolver: &'a dyn ValueResolver, location: String, ) -> Self
Create a new substituter with a policy and value resolver.
Examples found in repository?
examples/prefix_whitelist.rs (line 12)
6fn main() {
7 // SAFETY: single-threaded example, no concurrent env access.
8 unsafe { std::env::set_var("APP_HOST", "example.internal") };
9
10 let policy = PrefixWhitelistPolicy::new(vec!["APP_".to_string()]);
11 let resolver = EnvValueResolver;
12 let substituter = Substituter::new(&policy, &resolver, "example.toml".to_string());
13
14 match substituter.substitute("host = {{APP_HOST}}") {
15 Ok(value) => println!("{value}"),
16 Err(e) => eprintln!("substitution failed: {e}"),
17 }
18}Sourcepub fn substitute(&self, value: &str) -> Result<String, SubstitutionError>
pub fn substitute(&self, value: &str) -> Result<String, SubstitutionError>
Substitute all {{VAR_NAME}} placeholders in the given value.
§Returns
Ok(substituted_value)if substitution succeedsErr(SubstitutionError)if:- A referenced variable doesn’t exist
- A variable is rejected by the policy
- The syntax is invalid (e.g., nested placeholders)
Examples found in repository?
examples/prefix_whitelist.rs (line 14)
6fn main() {
7 // SAFETY: single-threaded example, no concurrent env access.
8 unsafe { std::env::set_var("APP_HOST", "example.internal") };
9
10 let policy = PrefixWhitelistPolicy::new(vec!["APP_".to_string()]);
11 let resolver = EnvValueResolver;
12 let substituter = Substituter::new(&policy, &resolver, "example.toml".to_string());
13
14 match substituter.substitute("host = {{APP_HOST}}") {
15 Ok(value) => println!("{value}"),
16 Err(e) => eprintln!("substitution failed: {e}"),
17 }
18}Trait Implementations§
Source§impl<'a> PolicyCatalog for Substituter<'a>
impl<'a> PolicyCatalog for Substituter<'a>
Source§type SubstitutionError = SubstitutionError
type SubstitutionError = SubstitutionError
Error produced before conversion to the public config error.
Source§type CompositePolicy = CompositePolicy
type CompositePolicy = CompositePolicy
Composite environment variable substitution policy.
Source§type PatternWhitelistPolicy = PatternWhitelistPolicy
type PatternWhitelistPolicy = PatternWhitelistPolicy
Regex-backed environment variable substitution policy.
Source§type PrefixWhitelistPolicy = PrefixWhitelistPolicy
type PrefixWhitelistPolicy = PrefixWhitelistPolicy
Prefix-backed environment variable substitution policy.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Substituter<'a>
impl<'a> !UnwindSafe for Substituter<'a>
impl<'a> Forget for Substituter<'a>
impl<'a> Freeze for Substituter<'a>
impl<'a> Send for Substituter<'a>
impl<'a> Sync for Substituter<'a>
impl<'a> Unpin for Substituter<'a>
impl<'a> UnsafeUnpin for Substituter<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more