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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! Composable settings fragment trait
//!
//! Defines the [`SettingsFragment`] trait that root settings fragments implement.
//! Each root fragment maps to a TOML section and can be validated independently.
//!
//! # TOML section mapping
//!
//! Each fragment's [`SettingsFragment::section()`] determines which TOML section it
//! deserializes from. For example, `CoreSettings` (section `"core"`) reads from `[core]`,
//! and `I18nSettings` (section `"i18n"`) reads from `[i18n]`.
//!
//! # Nested structs within fragments
//!
//! When a fragment contains nested structs (e.g., `CoreSettings.security`), the
//! nested struct maps to a TOML sub-section named after the field:
//!
//! ```toml
//! [core]
//! secret_key = "..."
//! debug = false
//!
//! [core.security]
//! secure_ssl_redirect = true
//! session_cookie_secure = true
//! ```
//!
//! In the legacy `Settings` format (where `CoreSettings` is flattened at the root),
//! the sub-section becomes a top-level section:
//!
//! ```toml
//! secret_key = "..."
//! debug = false
//!
//! [security]
//! secure_ssl_redirect = true
//! session_cookie_secure = true
//! ```
//!
//! # Typed schema references
//!
//! Composed settings expose a typed schema through `ProjectSettings::schema()`.
//! Embedded settings nodes can be followed with field access, for example
//! `ProjectSettings::schema().database.default.password`. The rendered path is
//! composed from the root composition key, the embedded field key, and serde
//! rename attributes, such as `database.default.db-password`.
//!
//! Schema generation peels semantically agnostic wrappers when producing nested
//! references: `Option<T>`, `Vec<T>`, `HashMap<String, T>`,
//! `BTreeMap<String, T>`, `IndexMap<String, T>`, and `Box<T>`.
//! `#[setting(node)]` forces a nested settings node, while `#[setting(leaf)]`
//! keeps a field as a leaf. Without a shape hint, `*Config` types may infer node
//! behavior; `*Settings` types should be annotated explicitly unless they are
//! built-in fragments annotated by the crate.
//!
//! Recursive required-field validation reports missing nested required leaves as
//! [`BuildError::MissingRequiredPath`](super::builder::BuildError::MissingRequiredPath).
//! A missing direct field on the fragment section remains
//! [`BuildError::MissingRequiredField`](super::builder::BuildError::MissingRequiredField).
//!
//! A struct annotated with `#[settings(fragment = true)]` but no `section = "..."`
//! is an embedded settings node. It implements
//! [`SettingsNode`](super::schema::SettingsNode) for recursive schema support,
//! but not [`SettingsFragment`], because it is not a top-level composition
//! section.
//!
//! # Merge semantics
//!
//! When multiple TOML files are merged (e.g., `base.toml` + `local.toml`),
//! the resulting layout depends on the
//! [`MergeStrategy`](super::builder::MergeStrategy) chosen on the
//! [`SettingsBuilder`](super::builder::SettingsBuilder):
//!
//! - With [`MergeStrategy::Shallow`](super::builder::MergeStrategy::Shallow)
//! — the legacy default for
//! [`build`](super::builder::SettingsBuilder::build) — each top-level
//! section is replaced as a whole. If `local.toml` defines `[core]`, it
//! replaces the entire `[core]` section from `base.toml`, so
//! environment-specific files must be self-contained for any section
//! they touch.
//! - With [`MergeStrategy::Deep`](super::builder::MergeStrategy::Deep)
//! — the default for
//! [`build_composed`](super::builder::SettingsBuilder::build_composed)
//! — nested tables are merged recursively. Defining `[core].debug =
//! true` in `local.toml` no longer erases sibling fields like
//! `[core].secret_key` or sub-sections like `[core.security]` that were
//! set in `base.toml`. Arrays and scalars are still replaced
//! wholesale.
//!
//! See [issue #4260](https://github.com/kent8192/reinhardt-web/issues/4260)
//! for the design discussion.
use FieldPolicy;
use Profile;
use ValidationResult;
use Serialize;
use DeserializeOwned;
use Debug;
/// Profile-aware validation for settings fragments.
///
/// Implement this trait to add custom validation logic to a settings fragment.
/// The `#[settings(fragment = true)]` macro generates a default (no-op) implementation
/// automatically. To provide custom validation, use `validate = false` in the macro
/// and implement this trait manually:
///
/// ```ignore
/// #[settings(fragment = true, section = "security", validate = false)]
/// struct SecuritySettings { /* ... */ }
///
/// impl SettingsValidation for SecuritySettings {
/// fn validate(&self, profile: &Profile) -> ValidationResult {
/// // custom validation logic
/// Ok(())
/// }
/// }
/// ```
/// A rootable composable unit of configuration.
///
/// Each root fragment maps to a TOML section and can be validated independently.
/// Root fragments are composed into a `ProjectSettings` struct using the
/// `#[settings(key: Type | Type)]` macro.
///
/// # Implementing
///
/// Use `#[settings(fragment = true, section = "...")]` to auto-derive this trait,
/// or implement it manually for custom validation. Omit `section = "..."`
/// only for embedded settings nodes that should not be composed as root
/// fragments.
/// Generic accessor trait for settings fragments.
///
/// The `#[settings]` macro generates implementations of this trait
/// using fully-qualified paths, so users do not need to manually
/// import individual `Has*Settings` traits.
///
/// Fragment macros bridge `HasSettings<F>` to the specific
/// `Has*Settings` traits for each generated fragment.
/// Marker trait bundling the commonly required fragment accessors used by
/// management commands and the database layer.
///
/// A composed settings type that derives `HasCoreSettings` and
/// `HasContactSettings` (i.e. it includes both `CoreSettings` and
/// `ContactSettings` fragments) automatically satisfies this trait via
/// the blanket implementation below.
///
/// Consumers that need an erased handle to settings (for example
/// `CommandContext`) hold an `Arc<dyn HasCommonSettings>` instead of a
/// concrete type, enabling cross-crate trait-object plumbing without
/// leaking the legacy `Settings` type.