serde_yaml/
path.rs

1use std::fmt::{self, Display};
2
3/// Path to the current value in the input, like `dependencies.serde.typo1`.
4#[derive(Copy, Clone)]
5pub 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
13impl<'a> Display for Path<'a> {
14    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            fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
19                match self.0 {
20                    Path::Root => Ok(()),
21                    path => write!(formatter, "{}.", path),
22                }
23            }
24        }
25
26        match self {
27            Path::Root => formatter.write_str("."),
28            Path::Seq { parent, index } => write!(formatter, "{}[{}]", parent, index),
29            Path::Map { parent, key } => write!(formatter, "{}{}", Parent(parent), key),
30            Path::Alias { parent } => write!(formatter, "{}", parent),
31            Path::Unknown { parent } => write!(formatter, "{}?", Parent(parent)),
32        }
33    }
34}