Struct Configuration

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

Configuration Instance, See Examples, How to Initialize Configuration for details.

Implementations§

Source§

impl Configuration

Source

pub fn new() -> Self

Create an empty Configuration.

If you want to use predefined sources, please try Configuration::with_predefined or Configuration::with_predefined_builder.

Source

pub fn register_kv<N: Into<String>>(self, name: N) -> ManualSource

Register key value manually.

Source

pub fn register_prefix_env(self, prefix: &str) -> Result<Self, ConfigError>

Register all env variables with prefix, default prefix is CFG.

  • prefix - Env variable prefix.

If prefix is CFG, then all env variables with pattern CFG_* will be added into configuration.

Examples:

  1. CFG_APP_NAME => app.name
  2. CFG_APP_0_NAME => app[0].name
Source

pub fn register_file<P: Into<PathBuf>>( self, path: P, required: bool, ) -> Result<Self, ConfigError>

Register file source, this method uses file extension1 to choose how to parsing configuration.

  • path - Config file path.
  • required - Whether config file must exist.

See Supported File Formats for details.


  1. cfg-rs does not enable any file format by default, please enable specific features when use this method. 

Source

pub fn register_random(self) -> Result<Self, ConfigError>

Available on crate feature rand only.

Register random value source, must enable feature rand.

Supported integer types:

  • random.u8
  • random.u16
  • random.u32
  • random.u64
  • random.u128
  • random.usize
  • random.i8
  • random.i16
  • random.i32
  • random.i64
  • random.i128
  • random.isize
Source

pub fn register_source<L: ConfigSource + 'static>( self, loader: L, ) -> Result<Self, ConfigError>

Register customized source, see How to Initialize Configuration, ConfigSource for details.

Source

pub fn refresh_ref(&self) -> Result<bool, ConfigError>

Refresh all RefValues without change Configuration itself.

Source

pub fn refresh(&mut self) -> Result<bool, ConfigError>

Refresh all RefValues and Configuration itself.

Source

pub fn get<T: FromConfig>(&self, key: &str) -> Result<T, ConfigError>

Get config from configuration by key, see ConfigKey for the key’s pattern details.

  • key - Config Key. Key examples:
  1. cfg.v1
  2. cfg.v2[0]
  3. cfg.v3[0][1]
  4. cfg.v4.key
  5. cfg.v5.arr[0]
Source

pub fn get_or<T: FromConfig>(&self, key: &str, def: T) -> Result<T, ConfigError>

Get config from configuration by key, otherwise return default. See ConfigKey for the key’s pattern details.

  • key - Config Key.
  • def - If config value is not found, then return def.
Source

pub fn get_predefined<T: FromConfigWithPrefix>(&self) -> Result<T, ConfigError>

Get config with predefined key, which is automatically derived by FromConfig.

Source

pub fn source_names(&self) -> Vec<&str>

Get source names, just for test.

Source

pub fn with_predefined_builder() -> PredefinedConfigurationBuilder

Create predefined sources builder, see init for details.

Source

pub fn with_predefined() -> Result<Self, ConfigError>

Create predefined configuration, see init for details.

Trait Implementations§

Source§

impl Default for Configuration

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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