pub trait Config: Sized {
    type Partial: Partial;

    const META: Meta;

    fn from_partial(partial: Self::Partial) -> Result<Self, Error>;

    fn builder() -> Builder<Self> { ... }
    fn from_file(path: impl Into<PathBuf>) -> Result<Self, Error> { ... }
}
Expand description

A configuration object that can be deserialized in layers via serde.

You would usually derive this trait for your own type and then load the configuration with one of the provided methods, like from_file or builder.

Deriving

This trait is usually derived as implementing it manually usually entails writing some repetitive boilerplate code, that goes against the “don’t repeat yourself” principle. See the documentation of the derive macro for more information!

Associated Types

A version of Self that represents a potetially partial configuration.

This type is supposed to have the exact same fields as this one, but with every field being optional. Its main use is to have a layered configuration from multiple sources where each layer might not contain all required values. The only thing that matters is that combining all layers will result in a configuration object that has all required values defined.

Associated Constants

A description of this configuration.

This is a runtime representation from the struct definition of your configuration type.

Required methods

Tries to create Self from a potentially partial object.

If any required values are not defined in partial, an Error is returned.

Provided methods

Convenience builder to configure, load and merge multiple configuration sources. Sources specified earlier have a higher priority; later sources only fill in the gaps. After all sources have been loaded, the default values (usually specified with #[default = ...]) are merged (with the lowest priority).

Example

In the following example, configuration is first loaded from environment variables, then from app.toml, then from /etc/app/config.toml and finally from the configured default values. Values found earlier in this list have precedence.

use confique::Config;

#[derive(Config)]
struct Conf {
    #[config(env = "APP_PORT", default = 8080)]
    port: u16,
}

#[cfg(feature = "toml")]
let conf = Conf::builder()
    .env()
    .file("app.toml")
    .file("/etc/app/config.toml")
    .load();

Load the configuration from a single file.

If you rather want to load from multiple sources, use Config::builder. Infers the file format from the file extension. Returns an error in these cases:

  • The path does not have a known file extension.
  • Loading the file fails.
  • The file does not specify all required configuration values.
Example
use confique::Config;

#[derive(Config)]
struct Conf {
    port: u16,
}

let conf = Conf::from_file("config.toml");

Implementors