jmap_tools/pointer/
mod.rs1pub(crate) mod eval;
8pub(crate) mod parser;
9
10use crate::{Element, Key, Property, Value};
11use std::{
12 borrow::Cow,
13 fmt::{Debug, Display, Formatter},
14 iter::Peekable,
15 slice::Iter,
16};
17
18pub trait JsonPointerHandler<'x, P: Property, E: Element>: Debug {
19 fn eval_jptr<'y>(
20 &'y self,
21 pointer: JsonPointerIter<'_, P>,
22 results: &mut Vec<Cow<'y, Value<'x, P, E>>>,
23 );
24 fn patch_jptr<'y: 'x>(
25 &mut self,
26 pointer: JsonPointerIter<'_, P>,
27 value: Value<'y, P, E>,
28 ) -> bool;
29 fn to_value<'y>(&'y self) -> Cow<'y, Value<'x, P, E>>;
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct JsonPointer<P: Property>(pub(crate) Vec<JsonPointerItem<P>>);
34
35pub type JsonPointerIter<'x, P> = Peekable<Iter<'x, JsonPointerItem<P>>>;
36
37#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
38pub enum JsonPointerItem<P: Property> {
39 Root,
40 Wildcard,
41 Key(Key<'static, P>),
42 Number(u64),
43}
44
45impl<P: Property> JsonPointer<P> {
46 pub fn iter(&self) -> JsonPointerIter<'_, P> {
47 self.0.iter().peekable()
48 }
49
50 #[allow(clippy::should_implement_trait)]
51 pub fn into_iter(self) -> impl Iterator<Item = JsonPointerItem<P>> {
52 self.0.into_iter()
53 }
54
55 pub fn into_inner(self) -> Vec<JsonPointerItem<P>> {
56 self.0
57 }
58
59 pub fn encode<I, T>(items: I) -> String
60 where
61 I: IntoIterator<Item = T>,
62 T: AsRef<str>,
63 {
64 let mut encoded = String::with_capacity(8);
65 for (pos, item) in items.into_iter().enumerate() {
66 if pos > 0 {
67 encoded.push('/');
68 }
69 let item = item.as_ref();
70 for c in item.chars() {
71 match c {
72 '~' => encoded.push_str("~0"),
73 '/' => encoded.push_str("~1"),
74 _ => encoded.push(c),
75 }
76 }
77 }
78 encoded
79 }
80
81 pub fn first(&self) -> Option<&JsonPointerItem<P>> {
82 self.0.first()
83 }
84
85 pub fn last(&self) -> Option<&JsonPointerItem<P>> {
86 self.0.last()
87 }
88
89 pub fn len(&self) -> usize {
90 self.0.len()
91 }
92
93 pub fn is_empty(&self) -> bool {
94 self.0.is_empty()
95 }
96
97 pub fn as_slice(&self) -> &[JsonPointerItem<P>] {
98 &self.0
99 }
100
101 pub fn as_mut_slice(&mut self) -> &mut [JsonPointerItem<P>] {
102 &mut self.0
103 }
104}
105
106impl<P: Property> JsonPointerItem<P> {
107 pub fn as_key(&self) -> Option<&Key<'static, P>> {
108 match self {
109 JsonPointerItem::Key(key) => Some(key),
110 _ => None,
111 }
112 }
113
114 pub fn as_property_key(&self) -> Option<&P> {
115 match self {
116 JsonPointerItem::Key(Key::Property(key)) => Some(key),
117 _ => None,
118 }
119 }
120
121 pub fn as_string_key(&self) -> Option<&str> {
122 match self {
123 JsonPointerItem::Key(Key::Borrowed(key)) => Some(key),
124 JsonPointerItem::Key(Key::Owned(key)) => Some(key),
125 _ => None,
126 }
127 }
128
129 pub fn to_cow(&self) -> Option<Cow<'_, str>> {
130 match self {
131 JsonPointerItem::Key(Key::Property(key)) => Some(key.to_cow()),
132 JsonPointerItem::Key(Key::Borrowed(key)) => Some(Cow::Borrowed(key)),
133 JsonPointerItem::Key(Key::Owned(key)) => Some(Cow::Borrowed(key)),
134 _ => None,
135 }
136 }
137}
138
139impl<P: Property> Display for JsonPointer<P> {
140 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
141 for (i, ptr) in self.0.iter().enumerate() {
142 if i > 0 {
143 write!(f, "/")?;
144 }
145
146 match ptr {
147 JsonPointerItem::Root => {}
148 JsonPointerItem::Wildcard => write!(f, "*")?,
149 JsonPointerItem::Key(k) => {
150 for c in k.to_string().chars() {
151 match c {
152 '~' => write!(f, "~0")?,
153 '/' => write!(f, "~1")?,
154 _ => write!(f, "{}", c)?,
155 }
156 }
157 }
158 JsonPointerItem::Number(n) => write!(f, "{}", n)?,
159 }
160 }
161 Ok(())
162 }
163}