@@ -8,17 +8,31 @@ use std::{convert::Infallible, env::VarError, num::ParseIntError, str::FromStr};
8
8
/// returns a list of all environment variables that are required to load the
9
9
/// struct.
10
10
///
11
- /// The macro also generates a `__EnvError ` type that captures errors that can
11
+ /// The macro also generates a `____EnvError ` type that captures errors that can
12
12
/// occur when trying to create an instance of the struct from environment
13
13
/// variables. This error type is used in the `FromEnv` trait implementation.
14
14
///
15
+ /// ## [`FromEnv`] vs [`FromEnvVar`]
16
+ ///
17
+ /// While [`FromEnvVar`] deals with loading simple types from the environment,
18
+ /// [`FromEnv`] is for loading complex types. It builds a struct from the
19
+ /// environment, usually be delegating each field to a [`FromEnvVar`] or
20
+ /// [`FromEnv`] implementation.
21
+ ///
22
+ /// When using the derive macro, the props of the struct must implement
23
+ /// [`FromEnv`] or [`FromEnvVar`]. Props that implement [`FromEnv`] contain all
24
+ /// the information needed to load the struct from the environment. Props
25
+ /// that implement [`FromEnvVar`] need additional information via attributes.
26
+ ///
15
27
/// ## Attributes
16
28
///
17
29
/// The macro supports the following attributes:
18
- /// - `var = ""`: The name of the environment variable. This is required if the
19
- /// prop implements [`FromEnvVar`].
20
- /// - `desc = ""`: A description of the environment variable. This is required
21
- /// if the prop implements [`FromEnvVar`].
30
+ /// - `var = ""`: The name of the environment variable. **This is required if
31
+ /// the prop implements [`FromEnvVar`] and forbidden if the prop implements
32
+ /// [`FromEnv`].**
33
+ /// - `desc = ""`: A description of the environment variable. **This is required
34
+ /// if the prop implements [`FromEnvVar`] and forbidden if the prop
35
+ /// implements [`FromEnv`].**
22
36
/// - `optional`: Marks the prop as optional. This is currently only used in the
23
37
/// generated `fn inventory`, and is informational.
24
38
/// - `infallible`: Marks the prop as infallible. This means that the prop
@@ -78,10 +92,20 @@ use std::{convert::Infallible, env::VarError, num::ParseIntError, str::FromStr};
78
92
/// maybe_not_needed: Option<String>,
79
93
/// }
80
94
///
81
- /// // The `FromEnv` trait is implemented for the struct, and the struct can
95
+ /// #[derive(Debug, FromEnv)]
96
+ /// pub struct MyBiggerCfg {
97
+ /// #[from_env(var = "BIGGGG_CONFIGGGG", desc = "A big config", infallible)]
98
+ /// pub big_config: String,
99
+ ///
100
+ /// // Note that becuase `MyCfg` implements `FromEnv`, we do not need to
101
+ /// // specify the `var` and `desc` attributes.
102
+ /// pub little_config: MyCfg,
103
+ /// }
104
+ ///
105
+ /// // The [`FromEnv`] trait is implemented for the struct, and the struct can
82
106
/// // be loaded from the environment.
83
107
/// # fn use_it() {
84
- /// if let Err(missing) = MyCfg ::check_inventory() {
108
+ /// if let Err(missing) = MyBiggerCfg ::check_inventory() {
85
109
/// println!("Missing environment variables:");
86
110
/// for var in missing {
87
111
/// println!("{}: {}", var.var, var.description);
@@ -90,7 +114,7 @@ use std::{convert::Infallible, env::VarError, num::ParseIntError, str::FromStr};
90
114
/// # }
91
115
/// ```
92
116
///
93
- /// This will generate a `FromEnv` implementation for the struct, and a
117
+ /// This will generate a [ `FromEnv`] implementation for the struct, and a
94
118
/// `MyCfgEnvError` type that is used to represent errors that can occur when
95
119
/// loading from the environment. The error generated will look like this:
96
120
///
@@ -104,6 +128,8 @@ use std::{convert::Infallible, env::VarError, num::ParseIntError, str::FromStr};
104
128
///
105
129
/// [`Infallible`]: std::convert::Infallible
106
130
/// [`SlotCalculator`]: crate::utils::SlotCalculator
131
+ /// [`FromEnv`]: crate::utils::from_env::FromEnv
132
+ /// [`FromEnvVar`]: crate::utils::from_env::FromEnvVar
107
133
pub use init4_from_env_derive:: FromEnv ;
108
134
109
135
/// Details about an environment variable. This is used to generate
0 commit comments