desenv/lib.rs
1//! Desenv-rs is a library used to deserialize the environment variables into a given struct deriving
2//! `Desenv` macro.
3
4pub use desenv_macros::Desenv;
5pub use error::Error;
6
7mod error;
8
9/// Load all the environment variables into a given `Desenv` struct.
10///
11/// # Errors
12///
13/// Will return `Err` if:
14/// - One environment variable for non-optional field is missing (and no default is set).
15/// - Deserialization from string of the resulting type fails.
16/// - Deserialization of default value from string to resulting type fails.
17/// - Both environment variable for non-optional field and default environment variable
18/// is missing.
19pub fn load<T>() -> Result<T, Error>
20where
21 T: Desenv,
22{
23 T::_load(None)
24}
25
26pub trait Desenv {
27 /// Load the configuration with the given optional `parent_prefix`.
28 /// DO NOT USE THIS FUNCTION! Use [`desenv::load`] instead!
29 ///
30 /// # Errors
31 ///
32 /// Will return `Err` if:
33 /// - One environment variable for non-optional field is missing (and no default is set).
34 /// - Deserialization from string of the resulting type fails.
35 /// - Deserialization of default value from string to resulting type fails.
36 /// - Both environment variable for non-optional field and default environment variable
37 /// is missing.
38 fn _load(parent_prefix: Option<String>) -> Result<Self, Error>
39 where
40 Self: Sized;
41}