confroid 0.0.4

The n+1-st config reader for your environment-based configs.
Documentation
//! # confroid
//!
//! Your config on 'roids — a small, type-driven reader for environment-based
//! configuration.
//!
//! Derive [`Config`] on a struct and read it with [`from_env`]:
//!
//! ```
//! # use confroid as confroid;
//! #[derive(confroid::Config)]
//! struct Config {
//!     #[confroid(default = 8080)]
//!     port: u16,
//! }
//!
//! // Reads the `PORT` environment variable, falling back to 8080.
//! let config: Config = confroid::from_env().unwrap();
//! # let _ = config.port;
//! ```

// Allows the derive macro's `::confroid::…` paths to resolve within this
// crate's own doctests and tests.
extern crate self as confroid;

#[cfg(feature = "docs")]
mod docs;
mod env;
mod error;
#[macro_use]
mod macros;
mod value;

#[cfg(feature = "docs")]
pub use docs::{Documented, FieldDoc, env_example, markdown_table};
pub use env::{Ctx, Env};
pub use error::{ConfroidError, ParserError, Result};
pub use value::FromEnv;
#[cfg(feature = "humantime")]
#[doc(hidden)]
pub use value::parse_humantime_duration as __parse_humantime_duration;

#[doc(inline)]
pub use confroid_derive::{Config, ConfigValue};

/// Read `T` from the current process environment.
pub fn from_env<T: FromEnv>() -> Result<T> {
    T::from_env(&Ctx::root(), &Env::from_os())
}

/// Read `T` from an explicit set of variables instead of the process
/// environment. Useful for tests and for reading a curated variable set.
pub fn from_pairs<T, I, K, V>(pairs: I) -> Result<T>
where
    T: FromEnv,
    I: IntoIterator<Item = (K, V)>,
    K: Into<String>,
    V: Into<String>,
{
    T::from_env(&Ctx::root(), &Env::from_pairs(pairs))
}

/// Read `T` from an existing [`Env`] snapshot.
pub fn from_snapshot<T: FromEnv>(env: &Env) -> Result<T> {
    T::from_env(&Ctx::root(), env)
}