1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Type-safe configuration with layered overrides.
//!
//! gifnoc provides a [`config!`] macro that generates a configuration struct
//! with typed fields and compile-time defaults. The struct implements the
//! [`Configurable`] trait, whose [`update`][Configurable::update] method
//! applies overrides from any source — environment variables, CLI flags, TOML
//! or YAML files — without clobbering unrelated fields.
//!
//! # Quick start
//!
//! ```rust
//! use gifnoc::{config, Configurable};
//!
//! config! {
//! ServerConfig {
//! host: String = "localhost",
//! port: u32 = 8080u32,
//! }
//! }
//!
//! let config = ServerConfig::default();
//! assert_eq!(config.host, "localhost");
//! assert_eq!(config.port, 8080);
//! ```
//!
//! # Sources
//!
//! All source functions return [`serde_json::Value`] and plug into
//! [`Configurable::update`] via the same flatten → merge → nest pipeline.
//! Sources are layered by chaining `.update()` calls — later calls win:
//!
//! ```rust,no_run
//! # use gifnoc::{config, Configurable};
//! # config! { AppConfig { port: u32 = 8080u32 } }
//! let (actions, flags) = gifnoc::args::parse();
//! let config = AppConfig::default()
//! .update(gifnoc::env::with_prefix("APP")) // env vars override defaults
//! .update(flags); // CLI flags override env vars
//! ```
//!
//! | Source | Function | Convention |
//! |--------|----------|------------|
//! | Environment variables | [`env::with_prefix`] | `APP_KEY`, `APP_SECTION__KEY` |
//! | CLI flags | [`args::parse`] | `--key value`, `--section.key value` |
//! | JSON file | [`json::from_file`] | round-trips [`Configurable::to_json`] output |
//! | TOML file | [`toml::from_file`] | *(requires feature `toml`)* |
//! | YAML file | [`yaml::from_file`] | *(requires feature `yaml`)* |
//!
//! # Recommended pattern: composition root
//!
//! Build the full config once in `main`, then distribute slices to subsystems
//! via their constructors. Each subsystem receives only the section it needs,
//! keeping dependencies narrow:
//!
//! ```rust,no_run
//! use gifnoc::{config, Configurable};
//!
//! config! { ServerConfig { port: u32 = 8080u32 } }
//! config! { AppConfig { server: ServerConfig = ServerConfig::default() } }
//!
//! struct Server { port: u32 }
//!
//! impl Server {
//! fn new(config: &ServerConfig) -> Self {
//! Server { port: config.port }
//! }
//! fn run(&self) { println!("listening on :{}", self.port); }
//! }
//!
//! fn main() {
//! let (actions, flags) = gifnoc::args::parse();
//! let config = AppConfig::default()
//! .update(gifnoc::env::with_prefix("APP"))
//! .update(flags);
//!
//! let server = Server::new(&config.server);
//!
//! for action in &actions {
//! match action.as_str() {
//! "serve" => server.run(),
//! other => { eprintln!("unknown action: {other}"); std::process::exit(1); }
//! }
//! }
//! }
//! ```
extern crate self as gifnoc;
/// Defines a configuration struct with typed fields and compile-time defaults.
///
/// Generates a `pub struct` with all fields public, a [`Default`] impl wired
/// to the supplied default expressions, and an empty [`Configurable`] impl.
/// The struct derives [`serde::Serialize`] and [`serde::Deserialize`].
///
/// # Syntax
///
/// ```text
/// config! {
/// StructName {
/// field_name: Type = default_expr,
/// ...
/// }
/// }
/// ```
///
/// Default expressions are passed through `.into()`, so string literals work
/// for `String` fields. For numeric types, include a type suffix to avoid
/// ambiguity (`8080u32`).
///
/// # Examples
///
/// A flat config with scalar fields:
///
/// ```rust
/// use gifnoc::{config, Configurable};
///
/// config! {
/// ServerConfig {
/// host: String = "localhost",
/// port: u32 = 8080u32,
/// debug: bool = false,
/// }
/// }
///
/// let config = ServerConfig::default();
/// assert_eq!(config.host, "localhost");
/// assert_eq!(config.port, 8080);
/// assert!(!config.debug);
/// ```
///
/// Nested configs by using another `config!`-generated type as a field:
///
/// ```rust
/// use gifnoc::{config, Configurable};
///
/// config! { DbConfig { port: u32 = 5432u32 } }
/// config! { AppConfig { db: DbConfig = DbConfig::default() } }
///
/// let config = AppConfig::default();
/// assert_eq!(config.db.port, 5432);
/// ```
pub use config;
/// Trait for configuration structs that support layered overrides.
///
/// Implemented automatically by the [`config!`] macro. Any type that is
/// [`serde::Serialize`] + [`serde::de::DeserializeOwned`] can also implement
/// it manually with an empty `impl` body — the default [`update`][Self::update]
/// method handles everything.