Coverage Report

Created: 2025-08-29 06:07

/src/serde-yaml/src/path.rs
Line
Count
Source
1
use std::fmt::{self, Display};
2
3
/// Path to the current value in the input, like `dependencies.serde.typo1`.
4
#[derive(Copy, Clone)]
5
pub enum Path<'a> {
6
    Root,
7
    Seq { parent: &'a Path<'a>, index: usize },
8
    Map { parent: &'a Path<'a>, key: &'a str },
9
    Alias { parent: &'a Path<'a> },
10
    Unknown { parent: &'a Path<'a> },
11
}
12
13
impl<'a> Display for Path<'a> {
14
6.52k
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
15
        struct Parent<'a>(&'a Path<'a>);
16
17
        impl<'a> Display for Parent<'a> {
18
454
            fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
19
454
                match self.0 {
20
31
                    Path::Root => Ok(()),
21
423
                    path => write!(formatter, "{}.", path),
22
                }
23
454
            }
24
        }
25
26
6.52k
        match self {
27
2.83k
            Path::Root => formatter.write_str("."),
28
3.20k
            Path::Seq { parent, index } => write!(formatter, "{}[{}]", parent, index),
29
385
            Path::Map { parent, key } => write!(formatter, "{}{}", Parent(parent), key),
30
38
            Path::Alias { parent } => write!(formatter, "{}", parent),
31
69
            Path::Unknown { parent } => write!(formatter, "{}?", Parent(parent)),
32
        }
33
6.52k
    }
34
}