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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.
//! `Flattened<T>` — capture the underlying [`Value`] alongside the
//! typed deserialization for `#[serde(flatten)]` targets.
//!
//! `#[serde(flatten)]` is the idiomatic way to collect "extra" fields
//! into a residue type, but the built-in residue types (`HashMap<String,
//! Value>`, `serde_json::Value`, our `Value`) erase the typed view: you
//! get the residue keys *or* the typed struct, never both.
//!
//! `Flattened<T>` solves that by capturing the [`Value`] tree first,
//! then re-running deserialization into `T` from the captured tree.
//! Both views are exposed:
//!
//! - `flattened.value: T` — the typed struct view.
//! - `flattened.raw: Value` — the dynamic view that lets callers walk
//! the original data, look up unknown keys, run schema validation,
//! or attach span info via [`crate::cst::Document::span_at`].
use crateValue;
/// Wrapper that pairs a typed deserialization of `T` with the
/// underlying [`Value`] tree captured from the source.
///
/// `Flattened<T>` is the answer to "I want `#[serde(flatten)]` plus
/// the dynamic view for span lookup / unknown-field detection /
/// schema validation". It deserializes by:
///
/// 1. First capturing the input as a [`Value`] (preserving every
/// key the source supplied, including ones the typed `T` may
/// not declare).
/// 2. Then re-running `T::deserialize` against the captured
/// [`Value`] via [`crate::from_value`].
///
/// The cost is one extra [`Value`] tree allocation per
/// `Flattened<T>` field and one extra deserialize pass — small
/// compared to the typical config-loading hot path.
///
/// # Examples
///
/// ```
/// use noyalib::{from_str, Flattened, Value};
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Inner {
/// port: u16,
/// }
///
/// #[derive(Debug, Deserialize)]
/// struct Config {
/// name: String,
/// // Capture both the typed view and the raw mapping so the
/// // application layer can still inspect unknown keys.
/// inner: Flattened<Inner>,
/// }
///
/// let cfg: Config = from_str(
/// "name: noyalib\ninner:\n port: 8080\n extra: not-in-Inner\n",
/// ).unwrap();
///
/// // Typed view — the fields Inner declares.
/// assert_eq!(cfg.inner.value.port, 8080);
///
/// // Raw view — the full mapping including the `extra` key.
/// match &cfg.inner.raw {
/// Value::Mapping(m) => {
/// assert!(m.contains_key("port"));
/// assert!(m.contains_key("extra"));
/// }
/// other => panic!("expected mapping, got {other:?}"),
/// }
/// ```