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
use std::fmt;
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub struct LensPathElement {
id: u64,
}
impl LensPathElement {
pub fn new(id: u64) -> LensPathElement {
LensPathElement { id }
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct LensPath {
pub elements: Vec<LensPathElement>,
}
impl LensPath {
pub fn empty() -> LensPath {
LensPath { elements: vec![] }
}
pub fn new(id: u64) -> LensPath {
LensPath {
elements: vec![LensPathElement { id }],
}
}
pub fn from_index(index: usize) -> LensPath {
LensPath {
elements: vec![LensPathElement { id: index as u64 }],
}
}
pub fn from_pair(id0: u64, id1: u64) -> LensPath {
LensPath {
elements: vec![LensPathElement { id: id0 }, LensPathElement { id: id1 }],
}
}
pub fn from_vec(ids: Vec<u64>) -> LensPath {
LensPath {
elements: ids.iter().map(|id| LensPathElement { id: *id }).collect(),
}
}
pub fn concat(lhs: LensPath, rhs: LensPath) -> LensPath {
let mut elements = lhs.elements;
elements.extend(&rhs.elements);
LensPath { elements }
}
}
impl fmt::Debug for LensPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{}]",
self.elements
.iter()
.map(|elem| elem.id.to_string())
.collect::<Vec<String>>()
.join(", ")
)
}
}
#[test]
fn test_lens_path_concat() {
let p0 = LensPath::from_vec(vec![1, 2, 3]);
let p1 = LensPath::from_vec(vec![4, 5]);
let p2 = LensPath::concat(p0, p1);
assert_eq!(p2, LensPath::from_vec(vec![1, 2, 3, 4, 5]));
}
#[test]
fn test_lens_path_debug() {
let path = LensPath::from_vec(vec![1, 2, 3, 4, 5]);
assert_eq!(format!("{:?}", path), "[1, 2, 3, 4, 5]".to_string());
}