pub trait Config: Sized {
type Layer: Layer;
const META: Meta;
// Required method
fn from_layer(layer: Self::Layer) -> Result<Self, Error>;
// Provided methods
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!
Required Associated Constants§
Required Associated Types§
Sourcetype Layer: Layer
type Layer: Layer
A layer of Self (a potentially 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.
Required Methods§
Provided Methods§
Sourcefn builder() -> Builder<Self>
fn builder() -> Builder<Self>
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();Sourcefn from_file(path: impl Into<PathBuf>) -> Result<Self, Error>
fn from_file(path: impl Into<PathBuf>) -> Result<Self, Error>
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");Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".