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
use std::fmt::{self, Display, Formatter};
use std::iter::FromIterator;
use std::ops::Deref;
use std::slice::Iter;
use std::str::FromStr;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use error::Error;
use value::Key;
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Path(Vec<Key>);
impl Path {
pub fn new() -> Self {
Default::default()
}
pub fn with_capacity(capacity: usize) -> Self {
Path(Vec::with_capacity(capacity))
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
let Path(ref keys) = *self;
let count = keys.len();
if count > 0 {
keys.iter()
.map(|key| key.len())
.fold(count - 1, |prev, next| prev + next)
} else {
count
}
}
pub fn to_bytes(&self) -> Vec<u8> {
let capacity = self.len();
let bytes = Vec::with_capacity(capacity);
if capacity == 0 {
return bytes;
}
self.iter().fold(bytes, |mut bytes, key| {
if !bytes.is_empty() {
bytes.push(b'.');
}
bytes.extend_from_slice(key.as_bytes());
bytes
})
}
pub fn to_string(&self) -> String {
let bytes = self.to_bytes();
unsafe { String::from_utf8_unchecked(bytes) }
}
}
impl Deref for Path {
type Target = [Key];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Display for Path {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
fmtr.write_str(&self.to_string())
}
}
impl From<Path> for String {
fn from(path: Path) -> Self {
path.to_string()
}
}
impl From<Path> for Vec<u8> {
fn from(path: Path) -> Self {
path.to_bytes()
}
}
impl FromIterator<Key> for Path {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = Key>,
{
Path(Vec::from_iter(iter))
}
}
impl FromStr for Path {
type Err = Error;
fn from_str(value: &str) -> Result<Self, Self::Err> {
value.split('.').map(|item| item.parse()).collect()
}
}
impl IntoIterator for Path {
type Item = Key;
type IntoIter = <Vec<Key> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a Path {
type Item = &'a Key;
type IntoIter = Iter<'a, Key>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl PartialEq<String> for Path {
fn eq(&self, rhs: &String) -> bool {
self == &*rhs
}
}
impl PartialEq<str> for Path {
fn eq(&self, rhs: &str) -> bool {
let mut parts = rhs.split('.');
for part in self.iter().map(|key| Some(&**key)) {
if part != parts.next() {
return false;
}
}
parts.next().is_none()
}
}
impl<'de> Deserialize<'de> for Path {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::{Error, Visitor};
struct PathVisitor;
impl<'de> Visitor<'de> for PathVisitor {
type Value = Path;
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(r#"a string of json api member names, separated by a ".""#)
}
fn visit_str<E: Error>(self, value: &str) -> Result<Self::Value, E> {
value.parse().map_err(Error::custom)
}
}
deserializer.deserialize_str(PathVisitor)
}
}
impl Serialize for Path {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}