[][src]Trait justconfig::processors::Subst

pub trait Subst where
    Self: Sized
{ pub fn env(self) -> Result<StringItem, ConfigError>;
pub fn expand(
        self,
        start: char,
        end: char,
        resolver: &dyn Fn(&str) -> Result<String, Box<dyn Error>>
    ) -> Result<StringItem, ConfigError>; }

Substitute placeholders within config values with values (for example environment variables).

Required methods

pub fn env(self) -> Result<StringItem, ConfigError>[src]

pub fn expand(
    self,
    start: char,
    end: char,
    resolver: &dyn Fn(&str) -> Result<String, Box<dyn Error>>
) -> Result<StringItem, ConfigError>
[src]

Loading content...

Implementations on Foreign Types

impl Subst for Result<StringItem, ConfigError>[src]

pub fn env(self) -> Result<StringItem, ConfigError>[src]

Call this method to substitute placeholders with environment variables.

An environment variable can be referenced by ${name}. Every occurrence of of this placeholder is expanded by replacing it with the named environment variable. If the environment variable is not set or can not be converted into a UTF-8 string an empty string is substituted.

To escape the start sequence ${ a second $ character must be used. For example $${LITERAL} will be replaced by ${LITERAL} without expanding the environment variable LITERAL. Any $ character not followed by { must not be escaped. The string cash: $$$ will be returned as cash: $$$.

Example

defaults.set(conf.root().push_all(&["env"]), "${PATH}", "substitute PATH");
conf.add_source(defaults);

let value: String = conf.get(ConfPath::from(&["env"])).env().value().unwrap();

assert_eq!(value, std::env::var("PATH").unwrap_or_default());

pub fn expand(
    self,
    start: char,
    end: char,
    resolver: &dyn Fn(&str) -> Result<String, Box<dyn Error>>
) -> Result<StringItem, ConfigError>
[src]

Call this method to substitute placeholders with an application defined value.

For a general description see the env method. This method is more general than env as it allows the characters enclosing the variable to be set and uses a callback to supply the value that should be substituted.

Because this function allows the enclosing characters to be set different substitutions can be used for different sources of the substituted value. For example ${} can be sued for environment variable substitution and $() could be used for substitution for a secondary configuration file.

The $ character as the start marker can not be changed.

Example

This example emulates the env method but returns an error if the environment variable is not found. In addition it replaces the curly brackets used by the env method with round ones.

defaults.set(conf.root().push_all(&["env"]), "$(I_DONT_KONW)", "substitute PATH");
conf.add_source(defaults);

let result: Result<String, ConfigError> = conf.get(ConfPath::from(&["env"])).expand('(', ')', &|key| { env::var(key).map_err(Box::from) } ).value();

assert!(result.is_err());
Loading content...

Implementors

Loading content...