pub struct Config { /* private fields */ }Expand description
The main configuration struct that holds parsed configuration data.
Implementations§
Source§impl Config
impl Config
Sourcepub fn new(data: ConfigValue) -> Self
pub fn new(data: ConfigValue) -> Self
Create a new Config from a ConfigValue.
Sourcepub fn builder() -> ConfigBuilder
pub fn builder() -> ConfigBuilder
Create a new ConfigBuilder for constructing a Config from multiple sources.
§Examples
use prefer::Config;
#[tokio::main]
async fn main() -> prefer::Result<()> {
let config = Config::builder()
.add_file("config/default.toml")
.add_env("MYAPP")
.build()
.await?;
Ok(())
}Sourcepub fn with_source(data: ConfigValue, path: PathBuf) -> Self
pub fn with_source(data: ConfigValue, path: PathBuf) -> Self
Create a new Config with a source path.
Sourcepub async fn load(name: &str) -> Result<Self>
pub async fn load(name: &str) -> Result<Self>
Load a configuration file by name.
Searches standard system paths for a configuration file matching the given name with any supported extension.
Sourcepub async fn load_from_path(path: &PathBuf) -> Result<Self>
pub async fn load_from_path(path: &PathBuf) -> Result<Self>
Load a configuration from a specific file path.
Sourcepub fn source_path(&self) -> Option<&PathBuf>
pub fn source_path(&self) -> Option<&PathBuf>
Get the source path of this configuration, if available.
Sourcepub fn get<T: FromValue>(&self, key: &str) -> Result<T>
pub fn get<T: FromValue>(&self, key: &str) -> Result<T>
Get a configuration value by key using dot notation.
§Examples
let username: String = config.get("auth.username")?;
let port: u16 = config.get("server.port")?;
let enabled: bool = config.get("features.logging.enabled")?;Sourcepub fn get_value(&self, key: &str) -> Result<&ConfigValue>
pub fn get_value(&self, key: &str) -> Result<&ConfigValue>
Get a raw configuration value by key using dot notation.
Returns a reference to the ConfigValue at the specified key path.
Sourcepub fn data(&self) -> &ConfigValue
pub fn data(&self) -> &ConfigValue
Get the entire configuration data as a reference.
Sourcepub fn data_mut(&mut self) -> &mut ConfigValue
pub fn data_mut(&mut self) -> &mut ConfigValue
Get the entire configuration data as a mutable reference.
Sourcepub fn extract<T: FromValue>(&self, key: &str) -> Result<T>
pub fn extract<T: FromValue>(&self, key: &str) -> Result<T>
Extract a value by key using the FromValue trait.
This is an alias for get() for backwards compatibility.
Sourcepub fn visit_key<V: ValueVisitor>(
&self,
key: &str,
visitor: &mut V,
) -> Result<V::Output>
pub fn visit_key<V: ValueVisitor>( &self, key: &str, visitor: &mut V, ) -> Result<V::Output>
Visit a value at the given key with a custom visitor.
This method allows for complex custom deserialization logic using the visitor pattern.
§Examples
struct PortVisitor;
impl ValueVisitor for PortVisitor {
type Output = u16;
fn visit_i64(&mut self, v: i64) -> Result<Self::Output> {
u16::try_from(v).map_err(|_| prefer::Error::ConversionError {
key: String::new(),
type_name: "u16".into(),
source: "port out of range".into(),
})
}
fn expecting(&self) -> &'static str {
"a port number"
}
}
let port = config.visit_key("server.port", &mut PortVisitor)?;