Struct PredefinedConfigurationBuilder

Source
pub struct PredefinedConfigurationBuilder { /* private fields */ }
Expand description

Predefined Configuration Builder. See init for details.

Implementations§

Source§

impl PredefinedConfigurationBuilder

Source

pub fn set_prefix_env<K: ToString>(self, prefix: K) -> Self

Set environment prefix, default value if CFG. By default, following environment variables will be loaded in to configuration.

§Environment Variable Regex Pattern: CFG_[0-9a-zA-Z]+

These are some predefined environment variables:

  • CFG_ENV_PREFIX=CFG
  • CFG_APP_NAME=app
  • CFG_APP_DIR=
  • CFG_APP_PROFILE=

You can change CFG to other prefix by this method.

Source

pub fn set_dir<V: Into<PathBuf>>(self, path: V) -> Self

Set config file directory.

Source

pub fn set_name<V: Into<String>>(self, name: V) -> Self

Set config file name.

Source

pub fn set_profile<V: Into<String>>(self, profile: V) -> Self

Set config file profile.

Source

pub fn set<K: Borrow<str>, V: Into<ConfigValue<'static>>>( self, key: K, value: V, ) -> Self

Set config into configuration by programming, or from command line arguments.

Source

pub fn set_cargo_env(self, cargo: Cargo) -> Self

Set source from cargo build env. Macro init_cargo_env will collect all CARGO_PKG_* env variables, and CARGO_BIN_NAME into configuration.

§Usage
use cfg_rs::*;
// Generate fn init_cargo_env().
init_cargo_env!();
let c = Configuration::with_predefined_builder()
  .set_cargo_env(init_cargo_env())
  .init()
  .unwrap();
Source

pub fn set_init<F: FnOnce(&Configuration) -> Result<(), ConfigError> + 'static>( self, f: F, ) -> Self

Set init func, which will be run after env source loaded.

Source

pub fn init(self) -> Result<Configuration, ConfigError>

Initialize configuration by multiple predefined sources.

§Predefined Sources.
  1. Cargo Package Env Variables (Must be explicitly set by set_cargo_env).
  2. Customized by Programming or Commandline Args.1
  3. Random Value (Auto enabled with feature rand).
  4. Environment Variable with Prefix CFG, referto set_prefix_env for details.1
  5. Profiled File Source with Path, ${app.dir}/${app.name}-${app.profile}.EXT. EXT: toml, json, yaml.2
  6. File Source with Path, ${app.dir}/${app.name}.EXT. EXT: toml, json, yaml.2
  7. Customized Source Can be Registered by register_source.
§Crate Feature
  • Feature rand to enable random value source.
  • Feature toml to enable toml supports.
  • Feature yaml to enable yaml supports.
  • Feature json to enable json supports.

  1. Always be enabled. 

  2. See Supported File Formats for details. 

Auto Trait Implementations§

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V