Skip to main content

Substituter

Struct Substituter 

Source
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>

Source

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}
Source

pub fn substitute(&self, value: &str) -> Result<String, SubstitutionError>

Substitute all {{VAR_NAME}} placeholders in the given value.

§Returns
  • Ok(substituted_value) if substitution succeeds
  • Err(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>

Source§

type SubstitutionError = SubstitutionError

Error produced before conversion to the public config error.
Source§

type CompositePolicy = CompositePolicy

Composite environment variable substitution policy.
Source§

type PatternWhitelistPolicy = PatternWhitelistPolicy

Regex-backed environment variable substitution policy.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.