reinhardt-conf 0.3.2

Configuration management framework for Reinhardt - Django-inspired settings with encryption and secrets management
Documentation
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Typed settings schema references and recursive settings metadata.

use std::fmt;
use std::marker::PhantomData;

use indexmap::IndexMap;
use serde::de::DeserializeOwned;
use serde_json::Value;

use super::builder::BuildError;
use super::policy::{FieldPolicy, FieldRequirement};

/// A segment in a typed settings path.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SettingsPathSegment {
	/// A static serialized settings key.
	Key(&'static str),
	/// A concrete key discovered at runtime.
	DynamicKey(String),
	/// A wildcard key for map-like values.
	AnyKey,
	/// A wildcard index for sequence-like values.
	AnyIndex,
}

/// Owned settings path segments.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct SettingsPathBuf {
	segments: Vec<SettingsPathSegment>,
}

impl SettingsPathBuf {
	/// Create an empty settings path.
	pub fn new() -> Self {
		Self::default()
	}

	/// Create a settings path from a single static key.
	pub fn from_key(key: &'static str) -> Self {
		Self {
			segments: vec![SettingsPathSegment::Key(key)],
		}
	}

	/// Create a settings path from owned segments.
	pub fn from_segments(segments: impl IntoIterator<Item = SettingsPathSegment>) -> Self {
		Self {
			segments: segments.into_iter().collect(),
		}
	}

	/// Return a new settings path with a static key appended.
	pub fn with_key(&self, key: &'static str) -> Self {
		let mut path = self.clone();
		path.segments.push(SettingsPathSegment::Key(key));
		path
	}

	/// Return a new settings path with a concrete runtime key appended.
	pub fn with_dynamic_key(&self, key: impl Into<String>) -> Self {
		let mut path = self.clone();
		path.segments
			.push(SettingsPathSegment::DynamicKey(key.into()));
		path
	}

	/// Return a new settings path with a wildcard map key appended.
	pub fn with_any_key(&self) -> Self {
		let mut path = self.clone();
		path.segments.push(SettingsPathSegment::AnyKey);
		path
	}

	/// Return a new settings path with a wildcard sequence index appended.
	pub fn with_any_index(&self) -> Self {
		let mut path = self.clone();
		path.segments.push(SettingsPathSegment::AnyIndex);
		path
	}

	/// Extend this path with another owned path.
	pub fn extend(mut self, other: SettingsPathBuf) -> Self {
		self.segments.extend(other.segments);
		self
	}

	/// Borrow this path's segments.
	pub fn segments(&self) -> &[SettingsPathSegment] {
		&self.segments
	}
}

impl fmt::Display for SettingsPathBuf {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		for (index, segment) in self.segments.iter().enumerate() {
			if index > 0 {
				f.write_str(".")?;
			}
			match segment {
				SettingsPathSegment::Key(key) => f.write_str(key)?,
				SettingsPathSegment::DynamicKey(key) => f.write_str(key)?,
				SettingsPathSegment::AnyKey | SettingsPathSegment::AnyIndex => f.write_str("*")?,
			}
		}
		Ok(())
	}
}

/// Typed reference to a settings field.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FieldRef<Root, Value> {
	path: SettingsPathBuf,
	_marker: PhantomData<fn() -> (Root, Value)>,
}

impl<Root, Value> FieldRef<Root, Value> {
	/// Create a typed field reference at the given path.
	pub fn new(path: SettingsPathBuf) -> Self {
		Self {
			path,
			_marker: PhantomData,
		}
	}

	/// Borrow the settings path referenced by this field.
	pub fn path(&self) -> &SettingsPathBuf {
		&self.path
	}
}

/// Typed reference to a secret settings field.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SecretFieldRef<Root, Value> {
	path: SettingsPathBuf,
	_marker: PhantomData<fn() -> (Root, Value)>,
}

impl<Root, Value> SecretFieldRef<Root, Value> {
	/// Create a typed secret field reference at the given path.
	pub fn new(path: SettingsPathBuf) -> Self {
		Self {
			path,
			_marker: PhantomData,
		}
	}

	/// Borrow the settings path referenced by this field.
	pub fn path(&self) -> &SettingsPathBuf {
		&self.path
	}

	/// Erase the referenced value type while preserving root and path.
	pub fn erase_value(&self) -> SecretFieldRef<Root, ()> {
		SecretFieldRef::new(self.path.clone())
	}
}

/// Typed reference to an optional settings value.
#[derive(Clone, Debug)]
pub struct OptionalRef<Root, Value, SomeRef> {
	path: SettingsPathBuf,
	builder: fn(SettingsPathBuf) -> SomeRef,
	_marker: PhantomData<fn() -> (Root, Value)>,
}

impl<Root, Value, SomeRef> OptionalRef<Root, Value, SomeRef> {
	/// Create a typed optional reference.
	pub fn new(path: SettingsPathBuf, builder: fn(SettingsPathBuf) -> SomeRef) -> Self {
		Self {
			path,
			builder,
			_marker: PhantomData,
		}
	}

	/// Build the reference for the contained value.
	pub fn some(&self) -> SomeRef {
		(self.builder)(self.path.clone())
	}

	/// Borrow the settings path referenced by this optional.
	pub fn path(&self) -> &SettingsPathBuf {
		&self.path
	}
}

impl<Root, Value, SomeRef> PartialEq for OptionalRef<Root, Value, SomeRef> {
	fn eq(&self, other: &Self) -> bool {
		self.path == other.path
	}
}

impl<Root, Value, SomeRef> Eq for OptionalRef<Root, Value, SomeRef> {}

/// Typed reference to a sequence settings value.
#[derive(Clone, Debug)]
pub struct SequenceRef<Root, Value, ItemRef> {
	path: SettingsPathBuf,
	builder: fn(SettingsPathBuf) -> ItemRef,
	_marker: PhantomData<fn() -> (Root, Value)>,
}

impl<Root, Value, ItemRef> SequenceRef<Root, Value, ItemRef> {
	/// Create a typed sequence reference.
	pub fn new(path: SettingsPathBuf, builder: fn(SettingsPathBuf) -> ItemRef) -> Self {
		Self {
			path,
			builder,
			_marker: PhantomData,
		}
	}

	/// Build the reference for any item in the sequence.
	pub fn any(&self) -> ItemRef {
		(self.builder)(self.path.with_any_index())
	}

	/// Borrow the settings path referenced by this sequence.
	pub fn path(&self) -> &SettingsPathBuf {
		&self.path
	}
}

impl<Root, Value, ItemRef> PartialEq for SequenceRef<Root, Value, ItemRef> {
	fn eq(&self, other: &Self) -> bool {
		self.path == other.path
	}
}

impl<Root, Value, ItemRef> Eq for SequenceRef<Root, Value, ItemRef> {}

/// Typed reference to a map settings value.
#[derive(Clone, Debug)]
pub struct MapRef<Root, Value, ItemRef> {
	path: SettingsPathBuf,
	builder: fn(SettingsPathBuf) -> ItemRef,
	_marker: PhantomData<fn() -> (Root, Value)>,
}

impl<Root, Value, ItemRef> MapRef<Root, Value, ItemRef> {
	/// Create a typed map reference.
	pub fn new(path: SettingsPathBuf, builder: fn(SettingsPathBuf) -> ItemRef) -> Self {
		Self {
			path,
			builder,
			_marker: PhantomData,
		}
	}

	/// Build the reference for any item in the map.
	pub fn any(&self) -> ItemRef {
		(self.builder)(self.path.with_any_key())
	}

	/// Build the reference for a concrete runtime map entry.
	pub fn entry(&self, key: impl Into<String>) -> ItemRef {
		(self.builder)(self.path.with_dynamic_key(key))
	}

	/// Borrow the settings path referenced by this map.
	pub fn path(&self) -> &SettingsPathBuf {
		&self.path
	}
}

impl<Root, Value, ItemRef> PartialEq for MapRef<Root, Value, ItemRef> {
	fn eq(&self, other: &Self) -> bool {
		self.path == other.path
	}
}

impl<Root, Value, ItemRef> Eq for MapRef<Root, Value, ItemRef> {}

/// Runtime metadata for a single settings field.
#[derive(Clone, Debug)]
pub struct SettingsFieldSchema {
	/// Rust struct field name.
	pub rust_name: &'static str,
	/// Serialized settings key.
	pub key: &'static str,
	/// Required/default policy for this field.
	pub policy: FieldPolicy,
	/// Runtime schema for the field value.
	pub value: SettingsValueSchema,
}

/// Runtime metadata for a settings field value.
#[derive(Clone, Debug)]
pub enum SettingsValueSchema {
	/// A leaf value with its Rust type name and secret classification.
	Leaf {
		/// Rust type name.
		type_name: &'static str,
		/// Whether this leaf contains secret material.
		secret: bool,
	},
	/// A nested settings node.
	Node {
		/// Rust type name.
		type_name: &'static str,
		/// Build node metadata for a concrete path.
		node: fn(SettingsPathBuf) -> SettingsNodeSchema,
	},
	/// Optional nested value.
	Optional {
		/// Inner value schema.
		inner: Box<SettingsValueSchema>,
	},
	/// Sequence nested value.
	Sequence {
		/// Inner item schema.
		inner: Box<SettingsValueSchema>,
	},
	/// Map nested value.
	Map {
		/// Inner item schema.
		inner: Box<SettingsValueSchema>,
	},
}

impl SettingsValueSchema {
	fn validate_required(
		&self,
		value: Option<&Value>,
		path: SettingsPathBuf,
	) -> Result<(), BuildError> {
		match self {
			SettingsValueSchema::Leaf { .. } => Ok(()),
			SettingsValueSchema::Node { node, .. } => {
				if let Some(map) = value.and_then(Value::as_object) {
					node(path.clone()).validate_required_map_at(map, path)?;
				}
				Ok(())
			}
			SettingsValueSchema::Optional { inner } => {
				if let Some(value) = value {
					inner.validate_required(Some(value), path)?;
				}
				Ok(())
			}
			SettingsValueSchema::Sequence { inner } => {
				if let Some(items) = value.and_then(Value::as_array) {
					for (index, item) in items.iter().enumerate() {
						inner.validate_required(
							Some(item),
							path.with_dynamic_key(index.to_string()),
						)?;
					}
				}
				Ok(())
			}
			SettingsValueSchema::Map { inner } => {
				if let Some(entries) = value.and_then(Value::as_object) {
					for (key, item) in entries {
						inner.validate_required(Some(item), path.with_dynamic_key(key.clone()))?;
					}
				}
				Ok(())
			}
		}
	}

	fn collect_secret_paths(&self, path: SettingsPathBuf, output: &mut Vec<SettingsPathBuf>) {
		match self {
			SettingsValueSchema::Leaf { secret, .. } => {
				if *secret {
					output.push(path);
				}
			}
			SettingsValueSchema::Node { node, .. } => {
				node(path.clone()).collect_secret_paths_at(path, output);
			}
			SettingsValueSchema::Optional { inner } => {
				inner.collect_secret_paths(path, output);
			}
			SettingsValueSchema::Sequence { inner } => {
				inner.collect_secret_paths(path.with_any_index(), output);
			}
			SettingsValueSchema::Map { inner } => {
				inner.collect_secret_paths(path.with_any_key(), output);
			}
		}
	}
}

/// Runtime metadata for a settings node.
#[derive(Clone, Debug)]
pub struct SettingsNodeSchema {
	/// Rust type name.
	pub type_name: &'static str,
	/// Field schemas for this node.
	pub fields: Vec<SettingsFieldSchema>,
}

impl SettingsNodeSchema {
	/// Validate required fields in a JSON object map.
	pub fn validate_required_map(
		&self,
		map: &serde_json::Map<String, Value>,
	) -> Result<(), BuildError> {
		self.validate_required_map_at(map, SettingsPathBuf::new())
	}

	/// Validate required fields in a JSON object map rooted at the given path.
	pub fn validate_required_map_at(
		&self,
		map: &serde_json::Map<String, Value>,
		base_path: SettingsPathBuf,
	) -> Result<(), BuildError> {
		self.validate_required_map_inner(map, base_path)
	}

	/// Collect all secret paths reachable from this node.
	pub fn collect_secret_paths(&self, output: &mut Vec<SettingsPathBuf>) {
		self.collect_secret_paths_at(SettingsPathBuf::new(), output);
	}

	fn validate_required_map_inner(
		&self,
		map: &serde_json::Map<String, Value>,
		base_path: SettingsPathBuf,
	) -> Result<(), BuildError> {
		for field in &self.fields {
			let path = base_path.with_key(field.key);
			let value = map.get(field.key);
			if field.policy.requirement == FieldRequirement::Required && value.is_none() {
				return Err(BuildError::MissingRequiredPath { path });
			}
			field.value.validate_required(value, path)?;
		}
		Ok(())
	}

	fn collect_secret_paths_at(
		&self,
		base_path: SettingsPathBuf,
		output: &mut Vec<SettingsPathBuf>,
	) {
		for field in &self.fields {
			field
				.value
				.collect_secret_paths(base_path.with_key(field.key), output);
		}
	}
}

/// Trait for recursive settings nodes that can expose typed schema references.
pub trait SettingsNode:
	Clone + fmt::Debug + serde::Serialize + DeserializeOwned + Send + Sync + 'static
{
	/// Typed reference schema rooted at `Root`.
	type Schema<Root>;

	/// Build typed schema references for this node at the provided path.
	fn schema_at<Root>(path: SettingsPathBuf) -> Self::Schema<Root>;

	/// Build runtime metadata for this node.
	fn node_schema() -> SettingsNodeSchema;
}

/// Trait for root settings values that expose typed schema references.
pub trait HasSettingsSchema {
	/// Typed schema reference type.
	type Schema;

	/// Build root schema references.
	fn schema() -> Self::Schema;

	/// Build root schema references.
	fn settings_schema() -> Self::Schema {
		Self::schema()
	}
}

/// Generated-code support for selecting composed root sections.
#[doc(hidden)]
pub fn root_section<'a>(
	merged: &'a IndexMap<String, Value>,
	primary_key: &'static str,
	fallback_key: &'static str,
) -> Option<&'a serde_json::Map<String, Value>> {
	merged
		.get(primary_key)
		.or_else(|| {
			if primary_key == fallback_key {
				None
			} else {
				merged.get(fallback_key)
			}
		})
		.and_then(Value::as_object)
}

#[cfg(test)]
mod tests {
	use indexmap::IndexMap;
	use serde_json::{Value, json};

	use super::root_section;

	#[test]
	fn root_section_primary_object_wins_over_fallback_object() {
		let mut merged = IndexMap::new();
		merged.insert("primary".to_string(), json!({ "source": "primary" }));
		merged.insert("fallback".to_string(), json!({ "source": "fallback" }));

		let section = root_section(&merged, "primary", "fallback").expect("primary object");

		assert_eq!(
			section.get("source"),
			Some(&Value::String("primary".into()))
		);
	}

	#[test]
	fn root_section_uses_fallback_object_when_primary_absent() {
		let mut merged = IndexMap::new();
		merged.insert("fallback".to_string(), json!({ "source": "fallback" }));

		let section = root_section(&merged, "primary", "fallback").expect("fallback object");

		assert_eq!(
			section.get("source"),
			Some(&Value::String("fallback".into()))
		);
	}

	#[test]
	fn root_section_malformed_primary_scalar_does_not_fall_back() {
		let mut merged = IndexMap::new();
		merged.insert("primary".to_string(), json!("malformed"));
		merged.insert("fallback".to_string(), json!({ "source": "fallback" }));

		let section = root_section(&merged, "primary", "fallback");

		assert!(section.is_none());
	}
}