pub struct Config { /* private fields */ }Expand description
A resolved configuration: a flat map of dotted keys to string values, plus the precedence machinery to overlay env and flags.
Implementations§
Source§impl Config
impl Config
pub fn new() -> Self
Sourcepub fn env_prefix<S: Into<String>>(self, prefix: S) -> Self
pub fn env_prefix<S: Into<String>>(self, prefix: S) -> Self
Set an environment-variable prefix used by Config::get to overlay
env values. With prefix MYTOOL, key port reads MYTOOL_PORT.
Sourcepub fn set<K: Into<String>, V: Into<String>>(
&mut self,
key: K,
value: V,
) -> &mut Self
pub fn set<K: Into<String>, V: Into<String>>( &mut self, key: K, value: V, ) -> &mut Self
Directly set a value (lowest-effort source, e.g. a default).
Sourcepub fn load_file(&mut self, path: &str) -> Result<&mut Self>
pub fn load_file(&mut self, path: &str) -> Result<&mut Self>
Load and merge a config file, autodetecting the format by extension
(.toml, .ini/.conf, .json). Values overwrite existing keys.
Sourcepub fn load_file_opt(&mut self, path: &str) -> Result<&mut Self>
pub fn load_file_opt(&mut self, path: &str) -> Result<&mut Self>
Try to load a file if it exists; missing files are not an error. Handy for “load ~/.config/tool.toml if present”.
Sourcepub fn get(&self, key: &str) -> Option<String>
pub fn get(&self, key: &str) -> Option<String>
Resolve a key using precedence: environment (if a prefix is set) wins over the loaded file/default values.
Sourcepub fn resolve(&self, key: &str, flag: Option<&str>, default: &str) -> String
pub fn resolve(&self, key: &str, flag: Option<&str>, default: &str) -> String
Resolve with an explicit flag value taking top precedence. Pass the flag
value the user supplied (or None); this returns the first of
flag → env → file → default.