pub struct Configuration { /* private fields */ }
Expand description
Configuration Instance, See Examples, How to Initialize Configuration for details.
Implementations§
Source§impl Configuration
impl Configuration
Sourcepub fn new() -> Self
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
.
Sourcepub fn register_kv<N: Into<String>>(self, name: N) -> ManualSource
pub fn register_kv<N: Into<String>>(self, name: N) -> ManualSource
Register key value manually.
Sourcepub fn register_prefix_env(self, prefix: &str) -> Result<Self, ConfigError>
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:
CFG_APP_NAME
=>app.name
CFG_APP_0_NAME
=>app[0].name
Sourcepub fn register_file<P: Into<PathBuf>>(
self,
path: P,
required: bool,
) -> Result<Self, ConfigError>
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.
cfg-rs
does not enable any file format by default, please enable specific features when use this method. ↩
Sourcepub fn register_random(self) -> Result<Self, ConfigError>
Available on crate feature rand
only.
pub fn register_random(self) -> Result<Self, ConfigError>
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
Sourcepub fn register_source<L: ConfigSource + 'static>(
self,
loader: L,
) -> Result<Self, ConfigError>
pub fn register_source<L: ConfigSource + 'static>( self, loader: L, ) -> Result<Self, ConfigError>
Register customized source, see How to Initialize Configuration, ConfigSource for details.
Sourcepub fn refresh_ref(&self) -> Result<bool, ConfigError>
pub fn refresh_ref(&self) -> Result<bool, ConfigError>
Refresh all RefValues without change Configuration
itself.
Sourcepub fn refresh(&mut self) -> Result<bool, ConfigError>
pub fn refresh(&mut self) -> Result<bool, ConfigError>
Refresh all RefValues and Configuration
itself.
Sourcepub fn get<T: FromConfig>(&self, key: &str) -> Result<T, ConfigError>
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:
cfg.v1
cfg.v2[0]
cfg.v3[0][1]
cfg.v4.key
cfg.v5.arr[0]
Sourcepub fn get_or<T: FromConfig>(&self, key: &str, def: T) -> Result<T, ConfigError>
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.
Sourcepub fn get_predefined<T: FromConfigWithPrefix>(&self) -> Result<T, ConfigError>
pub fn get_predefined<T: FromConfigWithPrefix>(&self) -> Result<T, ConfigError>
Get config with predefined key, which is automatically derived by FromConfig.
Sourcepub fn source_names(&self) -> Vec<&str>
pub fn source_names(&self) -> Vec<&str>
Get source names, just for test.
Sourcepub fn with_predefined_builder() -> PredefinedConfigurationBuilder
pub fn with_predefined_builder() -> PredefinedConfigurationBuilder
Create predefined sources builder, see init for details.
Sourcepub fn with_predefined() -> Result<Self, ConfigError>
pub fn with_predefined() -> Result<Self, ConfigError>
Create predefined configuration, see init for details.