serde_yaml/
path.rs

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