tier 0.1.14

Rust configuration library for layered TOML, env, and CLI settings
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
use std::collections::{BTreeMap, BTreeSet};

use serde::Serialize;
use serde_json::{Map, Value};

use crate::loader::record_direct_array_state;
use crate::report::normalize_path;
use crate::{ConfigError, Layer, SourceKind, SourceTrace};

/// Sparse patch field wrapper used by typed override structs.
///
/// `Patch<T>` makes intent explicit:
///
/// - `Patch::Unset` means "do not touch this field"
/// - `Patch::Set(value)` means "override this field with `value`"
///
/// For optional config fields, use `Patch<Option<T>>` when the patch needs to
/// distinguish "unset" from "set this field to null".
///
/// # Examples
///
/// ```
/// use tier::Patch;
///
/// let port = Patch::set(8080);
/// assert!(port.is_set());
///
/// let untouched: Patch<u16> = Patch::Unset;
/// assert_eq!(untouched.into_option(), None);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum Patch<T> {
    /// Do not override this field.
    #[default]
    Unset,
    /// Override this field with the contained value.
    Set(T),
}

impl<T> Patch<T> {
    /// Creates a patch that overrides a field with `value`.
    #[must_use]
    pub fn set(value: T) -> Self {
        Self::Set(value)
    }

    /// Returns the contained value by reference when the patch is set.
    #[must_use]
    pub fn as_ref(&self) -> Option<&T> {
        match self {
            Self::Unset => None,
            Self::Set(value) => Some(value),
        }
    }

    /// Returns `true` when this patch carries an override value.
    #[must_use]
    pub fn is_set(&self) -> bool {
        matches!(self, Self::Set(_))
    }

    /// Consumes the patch and returns the override value, when present.
    #[must_use]
    pub fn into_option(self) -> Option<T> {
        match self {
            Self::Unset => None,
            Self::Set(value) => Some(value),
        }
    }
}

impl<T> From<T> for Patch<T> {
    fn from(value: T) -> Self {
        Self::Set(value)
    }
}

/// Trait implemented by typed sparse override structures.
///
/// The easiest way to implement this trait is `#[derive(TierPatch)]` with the
/// `derive` feature enabled. A `TierPatch` value can then be turned into a
/// [`Layer`] or applied directly to a [`crate::ConfigLoader`].
///
/// Fields are sparse by default:
///
/// - `Option<T>` fields only write when they are `Some(...)`
/// - nested patch structs can be connected with `#[tier(nested)]`
/// - CLI-only fields can be ignored with `#[tier(skip)]`
/// - enums can be used for subcommand-like patch models
/// - use [`Patch<Option<T>>`] when the patch must distinguish "unset" from
///   "explicitly clear this optional config field"
///
/// # Examples
///
/// ```no_run
/// # #[cfg(feature = "derive")] {
/// # fn main() -> Result<(), tier::ConfigError> {
/// use serde::{Deserialize, Serialize};
/// use tier::{Layer, Patch, TierPatch};
///
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// struct ServerConfig {
///     port: u16,
///     tls: TlsConfig,
/// }
///
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// struct TlsConfig {
///     cert: Option<String>,
/// }
///
/// #[derive(Debug, TierPatch, Default)]
/// struct ServerPatch {
///     port: Option<u16>,
///     #[tier(path_expr = tier::path!(ServerConfig.tls.cert))]
///     cert_path: Patch<Option<String>>,
/// }
///
/// let patch = ServerPatch {
///     port: Some(8443),
///     cert_path: Patch::set(None),
/// };
///
/// let _layer = Layer::from_patch("typed-cli", &patch)?;
/// # Ok(())
/// # }
/// # }
/// ```
pub trait TierPatch {
    /// Writes sparse overrides into the provided layer builder.
    fn write_layer(&self, builder: &mut PatchLayerBuilder, prefix: &str)
    -> Result<(), ConfigError>;

    /// Converts the patch into a custom configuration layer.
    ///
    /// This is useful when the patch is built separately from the loader and
    /// should be applied with [`crate::ConfigLoader::layer`].
    fn to_layer(&self, name: impl Into<String>) -> Result<Layer, ConfigError>
    where
        Self: Sized,
    {
        Layer::from_patch(name, self)
    }
}

pub(crate) struct DeferredPatchLayer {
    trace: SourceTrace,
    writes: Vec<(String, Value)>,
}

impl DeferredPatchLayer {
    pub(crate) fn into_layer_with_shape(self, shape: Value) -> Result<Layer, ConfigError> {
        let mut builder = PatchLayerBuilder::from_trace_with_shape(self.trace, shape);
        for (path, value) in self.writes {
            builder.insert_value(&path, value)?;
        }
        Ok(builder.finish())
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.writes.is_empty()
    }
}

/// Hidden helper used by the `TierPatch` derive macro.
#[doc(hidden)]
pub struct PatchLayerBuilder {
    trace: SourceTrace,
    value: Value,
    shape: Value,
    entries: BTreeMap<String, SourceTrace>,
    claimed_paths: BTreeSet<String>,
    indexed_array_paths: BTreeSet<String>,
    indexed_array_base_lengths: BTreeMap<String, usize>,
    current_array_lengths: BTreeMap<String, usize>,
    direct_array_paths: BTreeSet<String>,
    deferred_writes: Option<Vec<(String, Value)>>,
}

impl PatchLayerBuilder {
    /// Creates a builder for a synthetic patch layer.
    #[must_use]
    pub fn new(kind: SourceKind, name: impl Into<String>) -> Self {
        Self::from_trace(SourceTrace {
            kind,
            name: name.into(),
            location: None,
        })
    }

    /// Creates a builder from an explicit source trace.
    #[must_use]
    pub fn from_trace(trace: SourceTrace) -> Self {
        Self::from_trace_with_shape(trace, Value::Object(Map::new()))
    }

    #[must_use]
    pub(crate) fn from_trace_deferred(trace: SourceTrace) -> Self {
        Self {
            trace,
            value: Value::Object(Map::new()),
            shape: Value::Object(Map::new()),
            entries: BTreeMap::new(),
            claimed_paths: BTreeSet::new(),
            indexed_array_paths: BTreeSet::new(),
            indexed_array_base_lengths: BTreeMap::new(),
            current_array_lengths: BTreeMap::new(),
            direct_array_paths: BTreeSet::new(),
            deferred_writes: Some(Vec::new()),
        }
    }

    /// Creates a builder from an explicit source trace and an existing shape.
    #[must_use]
    pub fn from_trace_with_shape(trace: SourceTrace, shape: Value) -> Self {
        Self {
            trace,
            value: Value::Object(Map::new()),
            shape,
            entries: BTreeMap::new(),
            claimed_paths: BTreeSet::new(),
            indexed_array_paths: BTreeSet::new(),
            indexed_array_base_lengths: BTreeMap::new(),
            current_array_lengths: BTreeMap::new(),
            direct_array_paths: BTreeSet::new(),
            deferred_writes: None,
        }
    }

    /// Inserts a serializable leaf override.
    pub fn insert_serialized<T>(&mut self, path: &str, value: &T) -> Result<(), ConfigError>
    where
        T: Serialize,
    {
        let value = serde_json::to_value(value)?;
        self.insert_value(path, value)
    }

    /// Inserts a pre-built JSON value override.
    pub fn insert_value(&mut self, path: &str, value: Value) -> Result<(), ConfigError> {
        let (segments, explicit_array_segments) =
            parse_patch_path(path).map_err(|message| ConfigError::InvalidPatch {
                name: self.trace.name.clone(),
                path: path.to_owned(),
                message,
            })?;
        if segments.is_empty() {
            return Err(ConfigError::InvalidPatch {
                name: self.trace.name.clone(),
                path: String::new(),
                message: "configuration path cannot be empty".to_owned(),
            });
        }
        if let Some(writes) = &mut self.deferred_writes {
            let shape_snapshot = self.shape.clone();
            let (segments, array_segments) =
                canonicalize_patch_path(&shape_snapshot, &segments, &explicit_array_segments);
            let canonical_path = normalize_path(&segments.join("."));
            claim_patch_path(&self.trace.name, &canonical_path, &mut self.claimed_paths)?;

            let indexed_array_container_paths =
                patch_indexed_array_container_paths(&segments, &array_segments);
            record_patch_indexed_array_state(
                &mut self.current_array_lengths,
                &mut self.indexed_array_base_lengths,
                &canonical_path,
                &indexed_array_container_paths,
            );
            if value.is_array() {
                record_direct_array_state(
                    &mut self.current_array_lengths,
                    &mut self.indexed_array_base_lengths,
                    &canonical_path,
                    &value,
                );
            }
            insert_path_with_shape(
                &mut self.shape,
                Some(&shape_snapshot),
                &segments,
                &array_segments,
                0,
                value.clone(),
            )
            .map_err(|message| ConfigError::InvalidPatch {
                name: self.trace.name.clone(),
                path: canonical_path,
                message,
            })?;
            writes.push((path.to_owned(), value));
            return Ok(());
        }
        let shape_snapshot = self.shape.clone();
        let (segments, array_segments) =
            canonicalize_patch_path(&shape_snapshot, &segments, &explicit_array_segments);
        let path = normalize_path(&segments.join("."));
        claim_patch_path(&self.trace.name, &path, &mut self.claimed_paths)?;

        let indexed_array_container_paths =
            patch_indexed_array_container_paths(&segments, &array_segments);
        record_patch_indexed_array_state(
            &mut self.current_array_lengths,
            &mut self.indexed_array_base_lengths,
            &path,
            &indexed_array_container_paths,
        );
        if value.is_array() {
            record_direct_array_state(
                &mut self.current_array_lengths,
                &mut self.indexed_array_base_lengths,
                &path,
                &value,
            );
            self.direct_array_paths.insert(path.clone());
        }

        insert_path_with_shape(
            &mut self.value,
            Some(&shape_snapshot),
            &segments,
            &array_segments,
            0,
            value.clone(),
        )
        .map_err(|message| ConfigError::InvalidPatch {
            name: self.trace.name.clone(),
            path: path.clone(),
            message,
        })?;
        insert_path_with_shape(
            &mut self.shape,
            Some(&shape_snapshot),
            &segments,
            &array_segments,
            0,
            value,
        )
        .map_err(|message| ConfigError::InvalidPatch {
            name: self.trace.name.clone(),
            path: path.clone(),
            message,
        })?;
        self.indexed_array_paths
            .extend(indexed_array_container_paths);

        self.entries.insert(path.clone(), self.trace.clone());
        let mut prefix = String::new();
        for segment in &segments {
            if !prefix.is_empty() {
                prefix.push('.');
            }
            prefix.push_str(segment);
            self.entries
                .entry(prefix.clone())
                .or_insert_with(|| self.trace.clone());
        }

        Ok(())
    }

    /// Finalizes the builder into a [`Layer`].
    #[must_use]
    pub fn finish(self) -> Layer {
        Layer::from_parts(
            self.trace,
            self.value,
            self.entries,
            BTreeSet::new(),
            self.indexed_array_paths,
            self.indexed_array_base_lengths,
            self.direct_array_paths,
        )
    }

    pub(crate) fn finish_deferred(self) -> DeferredPatchLayer {
        DeferredPatchLayer {
            trace: self.trace,
            writes: self.deferred_writes.unwrap_or_default(),
        }
    }
}

/// Hidden helper used by the `TierPatch` derive macro.
#[doc(hidden)]
#[must_use]
pub fn join_patch_prefix(prefix: &str, path: impl AsRef<str>) -> String {
    let path = path.as_ref();
    match (prefix.is_empty(), path.is_empty()) {
        (true, true) => String::new(),
        (true, false) => path.to_owned(),
        (false, true) => prefix.to_owned(),
        (false, false) => format!("{prefix}.{path}"),
    }
}

fn parse_patch_path(path: &str) -> Result<(Vec<String>, BTreeSet<usize>), String> {
    if path == "." {
        return Ok((Vec::new(), BTreeSet::new()));
    }

    let mut segments = Vec::new();
    let mut explicit_array_segments = BTreeSet::new();
    let mut current = String::new();
    let mut chars = path.chars().peekable();
    let mut after_index = false;
    let mut expecting_segment = true;
    let mut segment_index = 0usize;

    while let Some(ch) = chars.next() {
        if after_index {
            match ch {
                '.' => {
                    if chars.peek().is_none() {
                        return Err("configuration path cannot end with `.`".to_owned());
                    }
                    after_index = false;
                    expecting_segment = true;
                }
                '[' => {
                    let index = parse_patch_array_index(&mut chars)?;
                    explicit_array_segments.insert(segment_index);
                    segments.push(index);
                    segment_index += 1;
                    after_index = true;
                    expecting_segment = false;
                }
                _ => {
                    return Err(
                        "expected `.` or `[` after an array index in configuration path".to_owned(),
                    );
                }
            }
            continue;
        }

        match ch {
            '.' => {
                if current.is_empty() {
                    return Err("empty path segment in configuration path".to_owned());
                }
                segments.push(std::mem::take(&mut current));
                segment_index += 1;
                expecting_segment = true;
            }
            '[' => {
                if current.is_empty() {
                    return Err("array indices must follow a field name".to_owned());
                }
                segments.push(std::mem::take(&mut current));
                segment_index += 1;
                let index = parse_patch_array_index(&mut chars)?;
                explicit_array_segments.insert(segment_index);
                segments.push(index);
                segment_index += 1;
                after_index = true;
                expecting_segment = false;
            }
            ']' => return Err("unexpected `]` in configuration path".to_owned()),
            _ => {
                current.push(ch);
                expecting_segment = false;
            }
        }
    }

    if expecting_segment && !segments.is_empty() && current.is_empty() && !after_index {
        return Err("configuration path cannot end with `.`".to_owned());
    }

    if !current.is_empty() {
        segments.push(current);
    }

    Ok((segments, explicit_array_segments))
}

fn parse_patch_array_index<I>(chars: &mut std::iter::Peekable<I>) -> Result<String, String>
where
    I: Iterator<Item = char>,
{
    let mut index = String::new();
    let mut closed = false;
    for next in chars.by_ref() {
        if next == ']' {
            closed = true;
            break;
        }
        index.push(next);
    }
    if !closed {
        return Err("unclosed `[` in configuration path".to_owned());
    }
    if index.is_empty() {
        return Err("empty array index in configuration path".to_owned());
    }
    if !index.chars().all(|ch| ch.is_ascii_digit()) {
        return Err("array indices in configuration paths must be numeric".to_owned());
    }

    let normalized = index
        .parse::<usize>()
        .expect("checked numeric array indices")
        .to_string();
    Ok(normalized)
}

fn canonicalize_patch_path(
    root: &Value,
    segments: &[String],
    explicit_array_segments: &BTreeSet<usize>,
) -> (Vec<String>, BTreeSet<usize>) {
    enum PatchShape<'a> {
        Value(&'a Value),
        Object,
        Array,
    }

    let mut canonical = Vec::with_capacity(segments.len());
    let mut array_segments = BTreeSet::new();
    let mut current = PatchShape::Value(root);
    let mut index = 0;

    while index < segments.len() {
        let segment = &segments[index];
        let is_last = index + 1 == segments.len();
        let next_is_explicit_array = !is_last && explicit_array_segments.contains(&(index + 1));

        match current {
            PatchShape::Value(Value::Object(map)) => {
                canonical.push(segment.clone());
                current = if is_last {
                    PatchShape::Object
                } else if let Some(next) = map.get(segment) {
                    PatchShape::Value(next)
                } else if next_is_explicit_array {
                    PatchShape::Array
                } else {
                    PatchShape::Object
                };
            }
            PatchShape::Value(Value::Array(values)) => {
                let Ok(array_index) = segment.parse::<usize>() else {
                    canonical.extend(segments[index..].iter().cloned());
                    break;
                };
                canonical.push(array_index.to_string());
                array_segments.insert(index);
                current = if is_last {
                    PatchShape::Array
                } else if let Some(next) = values.get(array_index) {
                    PatchShape::Value(next)
                } else if next_is_explicit_array {
                    PatchShape::Array
                } else {
                    PatchShape::Object
                };
            }
            PatchShape::Value(_) => {
                canonical.extend(segments[index..].iter().cloned());
                break;
            }
            PatchShape::Object => {
                canonical.push(segment.clone());
                current = if is_last {
                    PatchShape::Object
                } else if next_is_explicit_array {
                    PatchShape::Array
                } else {
                    PatchShape::Object
                };
            }
            PatchShape::Array => {
                let Ok(array_index) = segment.parse::<usize>() else {
                    canonical.extend(segments[index..].iter().cloned());
                    break;
                };
                canonical.push(array_index.to_string());
                array_segments.insert(index);
                current = if is_last || next_is_explicit_array {
                    PatchShape::Array
                } else {
                    PatchShape::Object
                };
            }
        }

        index += 1;
    }

    (canonical, array_segments)
}

fn patch_indexed_array_container_paths(
    segments: &[String],
    array_segments: &BTreeSet<usize>,
) -> BTreeSet<String> {
    array_segments
        .iter()
        .map(|index| normalize_path(&segments[..*index].join(".")))
        .collect()
}

fn record_patch_indexed_array_state(
    current_array_lengths: &mut BTreeMap<String, usize>,
    indexed_array_base_lengths: &mut BTreeMap<String, usize>,
    path: &str,
    indexed_array_container_paths: &BTreeSet<String>,
) {
    for container_path in indexed_array_container_paths {
        let Some(index) = direct_patch_child_array_index(container_path, path) else {
            continue;
        };
        let Some(current_length) = current_array_lengths.get_mut(container_path) else {
            continue;
        };

        indexed_array_base_lengths
            .entry(container_path.clone())
            .or_insert(*current_length);
        if index >= *current_length {
            *current_length = index + 1;
        }
    }
}

fn direct_patch_child_array_index(container_path: &str, entry_path: &str) -> Option<usize> {
    let remainder = if container_path.is_empty() {
        entry_path
    } else {
        entry_path.strip_prefix(container_path)?.strip_prefix('.')?
    };
    remainder.split('.').next()?.parse::<usize>().ok()
}

fn insert_path_with_shape(
    current: &mut Value,
    shape: Option<&Value>,
    segments: &[String],
    array_segments: &BTreeSet<usize>,
    depth: usize,
    value: Value,
) -> Result<(), String> {
    let segment = &segments[depth];
    if segment.is_empty() {
        return Err("configuration path contains an empty segment".to_owned());
    }

    let is_last = depth + 1 == segments.len();
    match current {
        Value::Object(map) => {
            if is_last {
                map.insert(segment.clone(), value);
                return Ok(());
            }

            let shape_child = match shape {
                Some(Value::Object(shape_map)) => shape_map.get(segment),
                _ => None,
            };
            let next_is_array = array_segments.contains(&(depth + 1));
            let child = map.entry(segment.clone()).or_insert(patch_next_container(
                shape_child,
                next_is_array,
                &segments[depth + 1],
            )?);
            ensure_patch_container(child, shape_child, next_is_array, segment)?;
            insert_path_with_shape(
                child,
                shape_child,
                segments,
                array_segments,
                depth + 1,
                value,
            )
        }
        Value::Array(values) => {
            let index = segment.parse::<usize>().map_err(|_| {
                format!("path segment {segment} must be an array index at this position")
            })?;

            if values.len() <= index {
                values.resize(index + 1, Value::Null);
            }

            if is_last {
                values[index] = value;
                return Ok(());
            }

            let shape_child = match shape {
                Some(Value::Array(shape_values)) => shape_values.get(index),
                _ => None,
            };
            if values[index].is_null() {
                let next_is_array = array_segments.contains(&(depth + 1));
                values[index] =
                    patch_next_container(shape_child, next_is_array, &segments[depth + 1])?;
            }
            let next_is_array = array_segments.contains(&(depth + 1));
            ensure_patch_container(&values[index], shape_child, next_is_array, segment)?;
            insert_path_with_shape(
                &mut values[index],
                shape_child,
                segments,
                array_segments,
                depth + 1,
                value,
            )
        }
        _ => Err(format!(
            "path segment {segment} conflicts with an existing non-container value"
        )),
    }
}

fn patch_next_container(
    shape_child: Option<&Value>,
    next_is_array: bool,
    next_segment: &str,
) -> Result<Value, String> {
    match shape_child {
        Some(Value::Object(_)) => Ok(Value::Object(Map::new())),
        Some(Value::Array(_)) => Ok(Value::Array(Vec::new())),
        Some(_) => Err(format!(
            "path segment {next_segment} conflicts with an existing non-container value"
        )),
        None => Ok(if next_is_array {
            Value::Array(Vec::new())
        } else {
            Value::Object(Map::new())
        }),
    }
}

fn ensure_patch_container(
    child: &Value,
    shape_child: Option<&Value>,
    next_is_array: bool,
    segment: &str,
) -> Result<(), String> {
    let expected_array =
        matches!(shape_child, Some(Value::Array(_))) || (shape_child.is_none() && next_is_array);
    let expected_object =
        matches!(shape_child, Some(Value::Object(_))) || (shape_child.is_none() && !next_is_array);

    match child {
        Value::Object(_) if expected_object => Ok(()),
        Value::Array(_) if expected_array => Ok(()),
        _ => Err(format!(
            "path segment {segment} conflicts with an existing non-container value"
        )),
    }
}

fn claim_patch_path(
    layer_name: &str,
    path: &str,
    claimed_paths: &mut BTreeSet<String>,
) -> Result<(), ConfigError> {
    for existing_path in claimed_paths.iter() {
        if existing_path == path {
            return Err(ConfigError::InvalidPatch {
                name: layer_name.to_owned(),
                path: path.to_owned(),
                message: format!("duplicate patch path `{path}`"),
            });
        }

        if existing_path
            .strip_prefix(path)
            .is_some_and(|suffix| suffix.starts_with('.'))
            || path
                .strip_prefix(existing_path)
                .is_some_and(|suffix| suffix.starts_with('.'))
        {
            return Err(ConfigError::InvalidPatch {
                name: layer_name.to_owned(),
                path: path.to_owned(),
                message: format!("conflicting patch paths `{existing_path}` and `{path}` overlap"),
            });
        }
    }

    claimed_paths.insert(path.to_owned());
    Ok(())
}