Config

Struct Config 

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

The main configuration struct that holds parsed configuration data.

Implementations§

Source§

impl Config

Source

pub fn new(data: ConfigValue) -> Self

Create a new Config from a ConfigValue.

Source

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(())
}
Source

pub fn with_source(data: ConfigValue, path: PathBuf) -> Self

Create a new Config with a source path.

Source

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.

Source

pub async fn load_from_path(path: &PathBuf) -> Result<Self>

Load a configuration from a specific file path.

Source

pub fn source_path(&self) -> Option<&PathBuf>

Get the source path of this configuration, if available.

Source

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")?;
Source

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.

Source

pub fn data(&self) -> &ConfigValue

Get the entire configuration data as a reference.

Source

pub fn data_mut(&mut self) -> &mut ConfigValue

Get the entire configuration data as a mutable reference.

Source

pub fn has_key(&self, key: &str) -> bool

Check if a key exists in the configuration.

Source

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.

Source

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)?;
Source

pub fn visit<V: ValueVisitor>(&self, visitor: &mut V) -> Result<V::Output>

Visit the entire configuration data with a custom visitor.

This is useful for transforming the entire configuration into a custom type.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.