Skip to main content

Loader

Struct Loader 

Source
pub struct Loader<T> { /* private fields */ }
Expand description

Merges configuration sources and converts the result into T.

Sources are merged in ascending priority order; a higher priority value overrides a lower one, and equal priorities keep insertion order, with the later source winning.

SourcePriority
declared or added files (ConfigFile)10
source default100
environment variables, then dotenv200
command line250

A null value never overrides a lower source, arrays replace instead of appending, and tables merge key by key.

Implementations§

Source§

impl<T: FromNode> Loader<T>

Source

pub fn new() -> Self

A loader with no sources.

Source

pub fn file(self, file: ConfigFile) -> Self

Add a configuration file.

Source

pub fn source(self, source: impl ConfigSource + 'static) -> Self

Add any ConfigSource; its priority decides the merge order.

Source

pub fn env_vars<I, K, V>(self, vars: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

Use vars instead of the process environment. Meant for tests, which then never touch global state.

Source

pub fn on_warning( self, handler: impl Fn(&Warning) + Send + Sync + 'static, ) -> Self

Replace the default warning handler (which prints warning: ... to stderr). Called once per deprecated key present in the merged tree.

Source

pub fn load(&self) -> Result<T, ConfigError>

Load every source, merge them, report deprecations and convert.

§Errors

The first source error, or the conversion errors of T (all of them at once when several fields fail).

Source§

impl<T: FromNode> Loader<T>

Source

pub fn cli(self) -> Self

Read flags from the process arguments (std::env::args_os).

Flags are derived from T: --port, --server.max-conn, -p with short, --verbose / --no-verbose for booleans. Repeated list flags append, -h / --help yields a Help error from load. See load_or_exit.

Source

pub fn cli_args<I>(self, args: I) -> Self
where I: IntoIterator, I::Item: Into<OsString>,

Like cli with explicit arguments. args is the full argv, so the first item is the program name. Meant for tests.

Source

pub fn load_or_exit(&self) -> T

load, then exit the process on failure: --help prints the help text to stdout and exits with status 0, any other error prints error: ... to stderr and exits with status 2.

Trait Implementations§

Source§

impl<T: FromNode> Default for Loader<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Loader<T>

§

impl<T> !UnwindSafe for Loader<T>

§

impl<T> Freeze for Loader<T>
where PhantomData<fn() -> T>: Freeze,

§

impl<T> Send for Loader<T>
where PhantomData<fn() -> T>: Send,

§

impl<T> Sync for Loader<T>
where PhantomData<fn() -> T>: Sync,

§

impl<T> Unpin for Loader<T>
where PhantomData<fn() -> T>: Unpin,

§

impl<T> UnsafeUnpin for Loader<T>
where PhantomData<fn() -> T>: UnsafeUnpin,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.