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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use core::fmt::{Display, Formatter, Write};
use std::ops::Add;
use std::borrow::Cow;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Item<'a> {
Key(Cow<'a, str>),
Index(usize),
}
impl Display for Item<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Item::Key(s) => f.write_str(s.as_ref()),
Item::Index(n) => f.write_str(&n.to_string()),
}
}
}
impl Item<'_> {
pub fn is_key(&self) -> bool {
use Item::*;
match self {
Key(_) => true,
Index(_) => false,
}
}
pub fn is_index(&self) -> bool {
use Item::*;
match self {
Key(_) => false,
Index(_) => true,
}
}
pub fn as_key(&self) -> Option<&str> {
use Item::*;
match self {
Key(v) => Some(v.as_ref()),
Index(_) => None,
}
}
pub fn as_index(&self) -> Option<usize> {
use Item::*;
match self {
Key(_) => None,
Index(v) => Some(*v),
}
}
}
impl From<usize> for Item<'_> {
fn from(index: usize) -> Self {
use Item::*;
Index(index)
}
}
impl<'a> From<&'a str> for Item<'a> {
fn from(key: &'a str) -> Self {
use Item::*;
Key(Cow::from(key))
}
}
impl From<String> for Item<'_> {
fn from(key: String) -> Self {
use Item::*;
Key(Cow::from(key))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeyPath<'a> {
items: Vec<Item<'a>>
}
impl KeyPath<'_> {
pub fn new() -> Self {
Self { items: vec![] }
}
pub fn len(&self) -> usize {
self.items.len()
}
}
impl Display for KeyPath<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let s = self.items.iter().map(|i| i.to_string()).collect::<Vec<String>>().join(".");
f.write_str(&s)
}
}
impl<'a, T> Add<T> for &KeyPath<'a> where T: Into<Item<'a>> {
type Output = KeyPath<'a>;
fn add(self, rhs: T) -> Self::Output {
let mut items = self.items.clone();
items.push(rhs.into());
KeyPath { items }
}
}
impl<'a, T> Add<T> for KeyPath<'a> where T: Into<Item<'a>> {
type Output = Self;
fn add(self, rhs: T) -> Self::Output {
(&self).add(rhs)
}
}
#[macro_export]
macro_rules! path {
(@single $($x:tt)*) => (());
(@count $($rest:expr),*) => (<[()]>::len(&[$(path!(@single $rest)),*]));
(@item $other: expr) => ($crate::Item::from($other));
($($key:expr,)+) => { path!($($key),+) };
($($key:expr),*) => {
{
let _cap = path!(@count $($key),*);
let mut _items = ::std::vec::Vec::with_capacity(_cap);
$(
let _ = _items.push(path!(@item $key));
)*
$crate::KeyPath { items: _items }
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn macro_works_for_empty() {
let result = path![];
assert_eq!(result, KeyPath::new());
}
#[test]
fn macro_works_for_2_strings() {
let result = path!["a", "b"];
assert_eq!(result, KeyPath { items: vec![Item::Key("a".into()), Item::Key("b".into())]});
}
#[test]
fn macro_works_for_2_numbers() {
let result = path![2, 5];
assert_eq!(result, KeyPath { items: vec![Item::Index(2), Item::Index(5)]});
}
#[test]
fn macro_works_for_2_mixed_items() {
let string = "where".to_owned();
let result = path![string, 5];
assert_eq!(result, KeyPath { items: vec![Item::Key("where".to_owned().into()), Item::Index(5)]});
}
#[test]
fn macro_works_for_2_items_with_trailing_comma() {
let string = "where".to_owned();
let result = path![string, 5,];
assert_eq!(result, KeyPath { items: vec![Item::Key("where".to_owned().into()), Item::Index(5)]});
}
#[test]
fn macro_works_for_3_items() {
let string = "where".to_owned();
let result = path![string, 5, 7];
assert_eq!(result, KeyPath { items: vec![Item::Key("where".to_owned().into()), Item::Index(5), Item::Index(7)]});
}
#[test]
fn macro_works_for_3_items_with_trailing_comma() {
let string = "where".to_owned();
let result = path![string, 5, 7, ];
assert_eq!(result, KeyPath { items: vec![Item::Key("where".to_owned().into()), Item::Index(5), Item::Index(7)]});
}
#[test]
fn add_works_for_number() {
let path = KeyPath::new();
let result = path + 45;
assert_eq!(result, KeyPath { items: vec![Item::Index(45)] })
}
#[test]
fn add_works_for_str() {
let path = KeyPath::new();
let result = path + "";
assert_eq!(result, KeyPath { items: vec![Item::Key("".into())] })
}
#[test]
fn add_works_for_string() {
let path = KeyPath::new();
let result = path + "a".to_owned();
assert_eq!(result, KeyPath { items: vec![Item::Key("a".to_owned().into())] })
}
#[test]
fn key_path_can_be_debug_printed() {
let path = path!["where", "items", 5, "name"];
let result = format!("{}", path);
assert_eq!(&result, "where.items.5.name");
}
}