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
539
540
541
542
543
//! Enriched JSON object model that allows to associate values with origins.

use std::{collections::BTreeMap, fmt, iter, mem, sync::Arc};

pub use secrecy::{ExposeSecret, SecretString};

use crate::metadata::BasicTypes;

/// Supported file formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FileFormat {
    /// JSON file.
    Json,
    /// YAML file.
    Yaml,
    /// `.env` file.
    Dotenv,
}

impl fmt::Display for FileFormat {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::Json => "JSON",
            Self::Yaml => "YAML",
            Self::Dotenv => ".env",
        })
    }
}

/// Origin of a [`Value`] in configuration input.
#[derive(Debug, Default)]
#[non_exhaustive]
pub enum ValueOrigin {
    /// Unknown / default origin.
    #[default]
    Unknown,
    /// Environment variables.
    EnvVars,
    /// Fallbacks for config params.
    Fallbacks,
    /// File source.
    File {
        /// Filename; may not correspond to a real filesystem path.
        name: String,
        /// File format.
        format: FileFormat,
    },
    /// Path from a structured source.
    Path {
        /// Source of structured data, e.g. a JSON file.
        source: Arc<Self>,
        /// Dot-separated path in the source, like `api.http.port`.
        path: String,
    },
    /// Synthetic value.
    Synthetic {
        /// Original value source.
        source: Arc<Self>,
        /// Human-readable description of the transform.
        transform: String,
    },
}

impl fmt::Display for ValueOrigin {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unknown => formatter.write_str("unknown"),
            Self::EnvVars => formatter.write_str("env variables"),
            Self::Fallbacks => formatter.write_str("fallbacks"),
            Self::File { name, format } => {
                write!(formatter, "{format} file '{name}'")
            }
            Self::Path { source, path } => {
                if matches!(source.as_ref(), ValueOrigin::EnvVars) {
                    write!(formatter, "env variable '{path}'")
                } else {
                    write!(formatter, "{source} -> path '{path}'")
                }
            }
            Self::Synthetic { source, transform } => {
                write!(formatter, "{source} -> {transform}")
            }
        }
    }
}

/// String value: either a plaintext one, or a secret.
#[derive(Clone)]
pub enum StrValue {
    /// Plain string value.
    Plain(String),
    /// Secret string value.
    Secret(SecretString),
}

impl StrValue {
    /// Exposes a secret string if appropriate.
    pub fn expose(&self) -> &str {
        match self {
            Self::Plain(s) => s,
            Self::Secret(s) => s.expose_secret(),
        }
    }

    pub(crate) fn is_secret(&self) -> bool {
        matches!(self, Self::Secret(_))
    }

    pub(crate) fn make_secret(&mut self) {
        match self {
            Self::Plain(s) => {
                *self = Self::Secret(mem::take(s).into());
            }
            Self::Secret(_) => { /* value is already secret; do nothing */ }
        }
    }
}

impl fmt::Debug for StrValue {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Plain(s) => fmt::Debug::fmt(s, formatter),
            Self::Secret(_) => formatter.write_str("[REDACTED]"),
        }
    }
}

impl fmt::Display for StrValue {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::Plain(s) => s,
            Self::Secret(_) => "[REDACTED]",
        })
    }
}

/// JSON value with additional origin information.
#[derive(Clone, Default)]
pub enum Value {
    /// `null`.
    #[default]
    Null,
    /// Boolean value.
    Bool(bool),
    /// Numeric value.
    Number(serde_json::Number),
    /// String value.
    String(StrValue),
    /// Array of values.
    Array(Vec<WithOrigin>),
    /// Object / map of values.
    Object(Map),
}

impl fmt::Debug for Value {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Null => formatter.write_str("null"),
            Self::Bool(value) => fmt::Display::fmt(value, formatter),
            Self::Number(value) => fmt::Display::fmt(value, formatter),
            Self::String(value) => fmt::Debug::fmt(value, formatter),
            Self::Array(array) => formatter.debug_list().entries(array).finish(),
            Self::Object(map) => formatter.debug_map().entries(map).finish(),
        }
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl PartialEq<bool> for Value {
    fn eq(&self, other: &bool) -> bool {
        match self {
            Self::Bool(val) => val == other,
            _ => false,
        }
    }
}

impl From<serde_json::Number> for Value {
    fn from(value: serde_json::Number) -> Self {
        Self::Number(value)
    }
}

impl PartialEq<serde_json::Number> for Value {
    fn eq(&self, other: &serde_json::Number) -> bool {
        match self {
            Self::Number(num) => num == other,
            _ => false,
        }
    }
}

macro_rules! impl_traits_for_number {
    ($num:ty) => {
        impl From<$num> for Value {
            fn from(value: $num) -> Self {
                Self::Number(value.into())
            }
        }

        impl PartialEq<$num> for Value {
            fn eq(&self, other: &$num) -> bool {
                match self {
                    Self::Number(num) => *num == (*other).into(),
                    _ => false,
                }
            }
        }
    };
}

impl_traits_for_number!(u64);
impl_traits_for_number!(i64);

impl From<String> for Value {
    fn from(value: String) -> Self {
        Self::String(StrValue::Plain(value))
    }
}

impl From<Vec<WithOrigin>> for Value {
    fn from(array: Vec<WithOrigin>) -> Self {
        Self::Array(array)
    }
}

impl From<Map> for Value {
    fn from(map: Map) -> Self {
        Self::Object(map)
    }
}

impl Value {
    pub(crate) fn is_supported_by(&self, types: BasicTypes) -> bool {
        match self {
            Self::Null => true,
            Self::Bool(_) => types.contains(BasicTypes::BOOL),
            Self::Number(number) if number.is_u64() || number.is_i64() => {
                types.contains(BasicTypes::INTEGER)
            }
            Self::Number(_) => types.contains(BasicTypes::FLOAT),
            Self::String(_) => {
                // Relax type consistency check in order to be able to deserialize numbers / bools
                // (which is supported on the `ValueDeserializer` level).
                types.contains(BasicTypes::STRING)
                    || types.contains(BasicTypes::INTEGER)
                    || types.contains(BasicTypes::BOOL)
            }
            Self::Array(_) => types.contains(BasicTypes::ARRAY),
            Self::Object(_) => types.contains(BasicTypes::OBJECT),
        }
    }

    /// Attempts to convert this value to a plain (non-secret) string.
    pub fn as_plain_str(&self) -> Option<&str> {
        match self {
            Self::String(StrValue::Plain(s)) => Some(s),
            _ => None,
        }
    }

    /// Attempts to convert this value to an object.
    pub fn as_object(&self) -> Option<&Map> {
        match self {
            Self::Object(map) => Some(map),
            _ => None,
        }
    }
}

/// JSON object.
pub type Map<V = Value> = BTreeMap<String, WithOrigin<V>>;

/// JSON value together with its origin.
#[derive(Debug, Clone, Default)]
pub struct WithOrigin<T = Value> {
    /// Inner value.
    pub inner: T,
    /// Origin of the value.
    pub origin: Arc<ValueOrigin>,
}

impl<T> WithOrigin<T> {
    /// Creates a new value with origin.
    pub fn new(inner: T, origin: Arc<ValueOrigin>) -> Self {
        Self { inner, origin }
    }

    pub(crate) fn set_origin_if_unset(mut self, origin: &Arc<ValueOrigin>) -> Self {
        if matches!(self.origin.as_ref(), ValueOrigin::Unknown) {
            self.origin = origin.clone();
        }
        self
    }

    pub(crate) fn map<U>(self, map_fn: impl FnOnce(T) -> U) -> WithOrigin<U> {
        WithOrigin {
            inner: map_fn(self.inner),
            origin: self.origin,
        }
    }
}

impl WithOrigin {
    pub(crate) fn get(&self, pointer: Pointer<'_>) -> Option<&Self> {
        pointer
            .segments()
            .try_fold(self, |ptr, segment| match &ptr.inner {
                Value::Object(map) => map.get(segment),
                Value::Array(array) => array.get(segment.parse::<usize>().ok()?),
                _ => None,
            })
    }

    /// Returns value at the specified pointer.
    pub fn pointer(&self, pointer: &str) -> Option<&Self> {
        self.get(Pointer(pointer))
    }

    pub(crate) fn get_mut(&mut self, pointer: Pointer) -> Option<&mut Self> {
        pointer
            .segments()
            .try_fold(self, |ptr, segment| match &mut ptr.inner {
                Value::Object(map) => map.get_mut(segment),
                Value::Array(array) => array.get_mut(segment.parse::<usize>().ok()?),
                _ => None,
            })
    }

    /// Ensures that there is an object (possibly empty) at the specified location.
    pub(crate) fn ensure_object(
        &mut self,
        at: Pointer<'_>,
        mut create_origin: impl FnMut(Pointer<'_>) -> Arc<ValueOrigin>,
    ) -> &mut Map {
        for ancestor_path in at.with_ancestors() {
            self.ensure_object_step(ancestor_path, &mut create_origin);
        }

        let Value::Object(map) = &mut self.get_mut(at).unwrap().inner else {
            unreachable!(); // Ensured by calls above
        };
        map
    }

    fn ensure_object_step(
        &mut self,
        at: Pointer<'_>,
        mut create_origin: impl FnMut(Pointer<'_>) -> Arc<ValueOrigin>,
    ) {
        let Some((parent, last_segment)) = at.split_last() else {
            // Nothing to do.
            return;
        };

        // `unwrap()` is safe since `ensure_object()` is always called for the parent
        let parent = &mut self.get_mut(parent).unwrap().inner;
        if !matches!(parent, Value::Object(_)) {
            *parent = Value::Object(Map::new());
        }
        let Value::Object(parent_object) = parent else {
            unreachable!();
        };

        if !parent_object.contains_key(last_segment) {
            parent_object.insert(
                last_segment.to_owned(),
                WithOrigin {
                    inner: Value::Object(Map::new()),
                    origin: create_origin(at),
                },
            );
        }
    }

    /// Deep-merges self and `other`, with `other` having higher priority. Only objects are meaningfully merged;
    /// all other values are replaced.
    pub(crate) fn deep_merge(&mut self, overrides: Self) {
        match (&mut self.inner, overrides.inner) {
            (Value::Object(this), Value::Object(other)) => {
                Self::deep_merge_into_map(this, other);
            }
            (this, value) => {
                *this = value;
                self.origin = overrides.origin;
            }
        }
    }

    fn deep_merge_into_map(dest: &mut Map, source: Map) {
        for (key, value) in source {
            if let Some(existing_value) = dest.get_mut(&key) {
                existing_value.deep_merge(value);
            } else {
                dest.insert(key, value);
            }
        }
    }
}

// TODO: make public for increased type safety?
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) struct Pointer<'a>(pub &'a str);

impl fmt::Display for Pointer<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.0)
    }
}

impl<'a> Pointer<'a> {
    pub(crate) fn segments(self) -> impl Iterator<Item = &'a str> {
        self.0
            .split('.')
            .take(if self.0.is_empty() { 0 } else { usize::MAX })
    }

    pub(crate) fn split_last(self) -> Option<(Self, &'a str)> {
        if self.0.is_empty() {
            None
        } else if let Some((parent, last_segment)) = self.0.rsplit_once('.') {
            Some((Self(parent), last_segment))
        } else {
            Some((Self(""), self.0))
        }
    }

    /// Includes `Self`; doesn't include the empty pointer.
    pub(crate) fn with_ancestors(self) -> impl Iterator<Item = Self> {
        let mut current = self.0;
        iter::from_fn(move || {
            if current.is_empty() {
                None
            } else if let Some((_, tail)) = current.split_once('.') {
                current = tail;
                Some(Self(&self.0[..self.0.len() - tail.len() - 1]))
            } else {
                current = "";
                Some(self)
            }
        })
    }

    pub(crate) fn join(self, suffix: &str) -> String {
        if suffix.is_empty() {
            self.0.to_owned()
        } else if self.0.is_empty() {
            suffix.to_owned()
        } else {
            format!("{}.{suffix}", self.0)
        }
    }

    /// Returns `None` if `self` doesn't contain a sufficient number of segments.
    pub(crate) fn join_path(mut self, suffix: Pointer<'_>) -> Option<String> {
        let prefix_dots = suffix.0.bytes().take_while(|&ch| ch == b'.').count();
        for _ in 0..prefix_dots.saturating_sub(1) {
            (self, _) = self.split_last()?;
        }
        Some(self.join(&suffix.0[prefix_dots..]))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn splitting_pointer() {
        let pointer = Pointer("");
        assert_eq!(pointer.split_last(), None);
        assert_eq!(pointer.segments().collect::<Vec<_>>(), [] as [&str; 0]);
        assert_eq!(pointer.with_ancestors().collect::<Vec<_>>(), []);

        let pointer = Pointer("test");
        assert_eq!(pointer.split_last(), Some((Pointer(""), "test")));
        assert_eq!(pointer.segments().collect::<Vec<_>>(), ["test"]);
        assert_eq!(
            pointer.with_ancestors().collect::<Vec<_>>(),
            [Pointer("test")]
        );

        let pointer = Pointer("test.value");
        assert_eq!(pointer.split_last(), Some((Pointer("test"), "value")));
        assert_eq!(pointer.segments().collect::<Vec<_>>(), ["test", "value"]);
        assert_eq!(
            pointer.with_ancestors().collect::<Vec<_>>(),
            [Pointer("test"), Pointer("test.value")]
        );
    }

    #[test]
    fn joining_pointers() {
        let pointer = Pointer("");
        let joined = pointer.join("test");
        assert_eq!(joined, "test");

        let pointer = Pointer("test");
        let joined = pointer.join("");
        assert_eq!(joined, "test");

        let pointer = Pointer("test");
        let joined = pointer.join("other");
        assert_eq!(joined, "test.other");
    }

    #[test]
    fn joining_pointer_paths() {
        let pointer = Pointer("");
        let joined = pointer.join_path(Pointer("test")).unwrap();
        assert_eq!(joined, "test");

        let pointer = Pointer("");
        let joined = pointer.join_path(Pointer(".test")).unwrap();
        assert_eq!(joined, "test");

        let pointer = Pointer("");
        let joined = pointer.join_path(Pointer(".test.value")).unwrap();
        assert_eq!(joined, "test.value");

        let pointer = Pointer("map");
        let joined = pointer.join_path(Pointer(".test.value")).unwrap();
        assert_eq!(joined, "map.test.value");

        let pointer = Pointer("map");
        let joined = pointer.join_path(Pointer("..test.value")).unwrap();
        assert_eq!(joined, "test.value");

        let pointer = Pointer("map.key");
        let joined = pointer.join_path(Pointer("..test.value")).unwrap();
        assert_eq!(joined, "map.test.value");

        let pointer = Pointer("map.key");
        let joined = pointer.join_path(Pointer("...test.value")).unwrap();
        assert_eq!(joined, "test.value");
    }
}