[][src]Trait spirit::cfg_loader::ConfigBuilder

pub trait ConfigBuilder: Sized {
    fn config_default_paths<P, I>(self, paths: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>
;
fn config_defaults<D: Into<String>>(self, config: D) -> Self;
fn config_env<E: Into<String>>(self, env: E) -> Self;
fn config_filter<F: FnMut(&Path) -> bool + Send + 'static>(
        self,
        filter: F
    ) -> Self; fn config_ext<E: Into<OsString>>(self, ext: E) -> Self { ... }
fn config_exts<I, E>(self, exts: I) -> Self
    where
        I: IntoIterator<Item = E>,
        E: Into<OsString>
, { ... } }

Interface for configuring configuration loading options.

This is the common interface of cfg_loader::Builder and spirit Builder for configuring how and where from should configuration be loaded. The methods are available on both and do the same thing.

The interface is also implemented on Result<ConfigBuilder, Error>. This is both for convenience and for gathering errors to be handled within SpiritBuilder::run in uniform way.

Required methods

fn config_default_paths<P, I>(self, paths: I) -> Self where
    I: IntoIterator<Item = P>,
    P: Into<PathBuf>, 

Sets the configuration paths in case the user doesn't provide any.

This replaces any previously set default paths. If none are specified and the user doesn't specify any either, no config is loaded (but it is not an error in itself, simply the defaults will be used, if available).

This has no effect if the user does provide configuration paths on the command line.

fn config_defaults<D: Into<String>>(self, config: D) -> Self

Specifies the default configuration.

This „loads“ the lowest layer of the configuration from the passed string. The expected format is TOML.

Any user-provided configuration will be layered on top of it.

fn config_env<E: Into<String>>(self, env: E) -> Self

Enables loading configuration from environment variables.

If this is used, after loading the normal configuration files, the environment of the process is examined. Variables with the provided prefix are merged into the configuration.

Examples

use failure::Error;
use serde::Deserialize;
use spirit::cfg_loader::{Builder, ConfigBuilder};
use spirit::Empty;

#[derive(Default, Deserialize)]
struct Cfg {
    message: String,
}

const DEFAULT_CFG: &str = r#"
message = "Hello"
"#;

fn main() -> Result<(), Error> {
    let (_, mut loader) = Builder::new()
        .config_defaults(DEFAULT_CFG)
        .config_env("HELLO")
        .build::<Empty>();
    let cfg: Cfg = loader.load()?;
    println!("{}", cfg.message);
    Ok(())
}

If run like this, it'll print Hi. The environment takes precedence ‒ even if there was configuration file and it set the message, the Hi here would win.

HELLO_MESSAGE="Hi" ./hello

fn config_filter<F: FnMut(&Path) -> bool + Send + 'static>(
    self,
    filter: F
) -> Self

Sets a configuration dir filter.

If the user passes a directory path instead of a file path, the directory is traversed (every time the configuration is reloaded, so if files are added or removed, it is reflected) and files passing this filter are merged into the configuration, in the lexicographical order of their file names.

There's ever only one filter and the default one passes no files (therefore, directories are ignored by default).

The filter has no effect on files passed directly, only on loading directories. Only files directly in the directory are loaded ‒ subdirectories are not traversed.

For more convenient ways to set the filter, see config_ext and config_exts.

Loading content...

Provided methods

fn config_ext<E: Into<OsString>>(self, ext: E) -> Self

Configures a config dir filter for a single extension.

Sets the config directory filter (see config_filter) to one matching this single extension.

fn config_exts<I, E>(self, exts: I) -> Self where
    I: IntoIterator<Item = E>,
    E: Into<OsString>, 

Configures a config dir filter for multiple extensions.

Sets the config directory filter (see config_filter) to one matching files with any of the provided extensions.

Loading content...

Implementations on Foreign Types

impl<C: ConfigBuilder, Error> ConfigBuilder for Result<C, Error>[src]

fn config_ext<E: Into<OsString>>(self, ext: E) -> Self[src]

fn config_exts<I, E>(self, exts: I) -> Self where
    I: IntoIterator<Item = E>,
    E: Into<OsString>, 
[src]

Loading content...

Implementors

impl ConfigBuilder for spirit::cfg_loader::Builder[src]

fn config_ext<E: Into<OsString>>(self, ext: E) -> Self[src]

fn config_exts<I, E>(self, exts: I) -> Self where
    I: IntoIterator<Item = E>,
    E: Into<OsString>, 
[src]

impl<O, C> ConfigBuilder for spirit::Builder<O, C>[src]

fn config_ext<E: Into<OsString>>(self, ext: E) -> Self[src]

fn config_exts<I, E>(self, exts: I) -> Self where
    I: IntoIterator<Item = E>,
    E: Into<OsString>, 
[src]

Loading content...