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
//! Path to the current value in the input.
use std::{
cell::OnceCell,
fmt::{self, Display},
};
/// A structured representation of a path to the current value in the input,
/// like `dependencies.serde.typo1`.
#[derive(Copy, Clone)]
pub enum Path<'a> {
/// The root of the input.
Root,
/// A sequence index.
Seq {
/// The path to the parent value.
parent: &'a Path<'a>,
/// The index of the current value.
index: usize,
},
/// A map key.
Map {
/// The path to the parent value.
parent: &'a Path<'a>,
/// The key of the current value.
key: &'a str,
},
/// An alias.
Alias {
/// The path to the parent value.
parent: &'a Path<'a>,
},
/// An unknown path.
Unknown {
/// The path to the parent value.
parent: &'a Path<'a>,
},
}
impl Display for Path<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
struct Parent<'a>(&'a Path<'a>);
impl Display for Parent<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self.0 {
Path::Root => Ok(()),
path => write!(formatter, "{}.", path),
}
}
}
match self {
Path::Root => formatter.write_str("."),
Path::Seq { parent, index } => write!(formatter, "{}[{}]", parent, index),
Path::Map { parent, key } => write!(formatter, "{}{}", Parent(parent), key),
Path::Alias { parent } => write!(formatter, "{}", parent),
Path::Unknown { parent } => write!(formatter, "{}?", Parent(parent)),
}
}
}
impl<'a> Path<'a> {
/// Returns an owned version of this path.
pub fn to_owned_path(&self) -> OwnedPath {
match self {
Path::Root => OwnedPath::Root,
Path::Seq { parent, index } => OwnedPath::Seq {
parent: Box::new(parent.to_owned_path()),
index: *index,
borrowed: OnceCell::new(),
},
Path::Map { parent, key } => OwnedPath::Map {
parent: Box::new(parent.to_owned_path()),
key: key.to_string(),
borrowed: OnceCell::new(),
},
Path::Alias { parent } => OwnedPath::Alias {
parent: Box::new(parent.to_owned_path()),
borrowed: OnceCell::new(),
},
Path::Unknown { parent } => OwnedPath::Unknown {
parent: Box::new(parent.to_owned_path()),
borrowed: OnceCell::new(),
},
}
}
}
/// An owned version of a [Path].
pub enum OwnedPath {
/// The root of the input.
Root,
/// A sequence index.
Seq {
/// The path to the parent value.
parent: Box<OwnedPath>,
/// The index of the current value.
index: usize,
/// A cell to hold the borrowed path.
borrowed: OnceCell<Path<'static>>,
},
/// A map key.
Map {
/// The path to the parent value.
parent: Box<OwnedPath>,
/// The key of the current value.
key: String,
/// A cell to hold the borrowed path.
borrowed: OnceCell<Path<'static>>,
},
/// An alias.
Alias {
/// The path to the parent value.
parent: Box<OwnedPath>,
/// A cell to hold the borrowed path.
borrowed: OnceCell<Path<'static>>,
},
/// An unknown path.
Unknown {
/// The path to the parent value.
parent: Box<OwnedPath>,
/// A cell to hold the borrowed path.
borrowed: OnceCell<Path<'static>>,
},
}
impl<'a> OwnedPath {
/// Returns a borrowed version of this path.
pub fn as_path(&'a self) -> &'a Path<'a> {
static ROOT: Path<'static> = Path::Root;
match self {
OwnedPath::Root => &ROOT,
OwnedPath::Seq {
parent,
index,
borrowed,
} => {
let borrowed = borrowed.get_or_init(|| {
let parent = parent.as_path();
// SAFETY: self-reference
unsafe {
std::mem::transmute(Path::Seq {
parent,
index: *index,
})
}
});
borrowed
}
OwnedPath::Map {
parent,
key,
borrowed,
} => {
let borrowed = borrowed.get_or_init(|| {
let parent = parent.as_path();
// SAFETY: self-reference
unsafe {
std::mem::transmute(Path::Map {
parent,
key: key.as_ref(),
})
}
});
borrowed
}
OwnedPath::Alias { parent, borrowed } => {
let borrowed = borrowed.get_or_init(|| {
let parent = parent.as_path();
// SAFETY: self-reference
unsafe { std::mem::transmute(Path::Alias { parent }) }
});
borrowed
}
OwnedPath::Unknown { parent, borrowed } => {
let borrowed = borrowed.get_or_init(|| {
let parent = parent.as_path();
// SAFETY: self-reference
unsafe { std::mem::transmute(Path::Unknown { parent }) }
});
borrowed
}
}
}
}