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
use crate::parser;
use crate::traverse;
use serde_value::Value;
use std::str::FromStr;
#[derive(Eq, PartialEq, PartialOrd, Debug, Clone)]
pub struct Pointer {
steps: Vec<Step>,
}
#[derive(Eq, PartialEq, PartialOrd, Debug, Clone)]
pub enum Step {
Name(String),
Index(usize),
NewElement,
}
pub type ParseError = parser::ParseError;
pub type ValuePointer<'a> = traverse::ValuePointer<'a>;
pub type ValuePointerMut<'a> = traverse::ValuePointerMut<'a>;
impl Pointer {
pub fn push(&mut self, step: Step) -> &mut Self {
self.steps.push(step);
self
}
pub fn pop(&mut self) -> Option<Step> {
self.steps.pop()
}
pub fn insert(&mut self, index: usize, step: Step) {
self.steps.insert(index, step)
}
pub fn remove(&mut self, index: usize) -> Step {
self.steps.remove(index)
}
pub fn traverse<'a>(&self, val: &'a Value) -> Option<ValuePointer<'a>> {
traverse::traverse(val, self)
}
pub fn traverse_mut<'a>(&self, val: &'a mut Value) -> Option<ValuePointerMut<'a>> {
traverse::traverse_mut(val, self)
}
pub fn find<'a>(&self, val: &'a Value) -> Option<&'a Value> {
match self.traverse(val) {
Some(ValuePointer::Existing(v)) => Some(v),
_ => None,
}
}
pub fn find_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> {
match self.traverse_mut(val) {
Some(ValuePointerMut::Existing(v)) => Some(v),
_ => None,
}
}
}
impl FromStr for Pointer {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
parser::parse(s)
}
}
impl From<Vec<Step>> for Pointer {
fn from(ps: Vec<Step>) -> Self {
Self { steps: ps }
}
}
impl Into<Vec<Step>> for Pointer {
fn into(self) -> Vec<Step> {
self.steps
}
}
impl IntoIterator for Pointer {
type Item = Step;
type IntoIter = std::vec::IntoIter<Step>;
fn into_iter(self) -> Self::IntoIter {
self.steps.into_iter()
}
}
impl Default for Pointer {
fn default() -> Self {
Self {
steps: Vec::default(),
}
}
}