serde_pointer/
pointer.rs

1/*
2 *   Copyright (c) 2019 Lukas Krejci
3 *   All rights reserved.
4
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8
9 *   http://www.apache.org/licenses/LICENSE-2.0
10
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18use crate::parser;
19use crate::traverse;
20use serde_value::Value;
21use std::str::FromStr;
22
23/// Represents a pointer to the data as defined in the RFC6901 - the JSON Pointer specification.
24#[derive(Eq, PartialEq, PartialOrd, Debug, Clone)]
25pub struct Pointer {
26    steps: Vec<Step>,
27}
28
29/// Represents a single traversal step of the pointer.
30#[derive(Eq, PartialEq, PartialOrd, Debug, Clone)]
31pub enum Step {
32    Name(String),
33    Index(usize),
34    NewElement,
35}
36
37pub type ParseError = parser::ParseError;
38pub type ValuePointer<'a> = traverse::ValuePointer<'a>;
39pub type ValuePointerMut<'a> = traverse::ValuePointerMut<'a>;
40
41impl Pointer {
42    pub fn push(&mut self, step: Step) -> &mut Self {
43        self.steps.push(step);
44        self
45    }
46
47    pub fn pop(&mut self) -> Option<Step> {
48        self.steps.pop()
49    }
50
51    pub fn insert(&mut self, index: usize, step: Step) {
52        self.steps.insert(index, step)
53    }
54
55    pub fn remove(&mut self, index: usize) -> Step {
56        self.steps.remove(index)
57    }
58
59    /// Traverses the provided value and finds the data this pointer points to in it, if any.
60    pub fn traverse<'a>(&self, val: &'a Value) -> Option<ValuePointer<'a>> {
61        traverse::traverse(val, self)
62    }
63
64    pub fn traverse_mut<'a>(&self, val: &'a mut Value) -> Option<ValuePointerMut<'a>> {
65        traverse::traverse_mut(val, self)
66    }
67
68    /// A simple override of `traverse()` that directly exposes the found value, if any.
69    pub fn find<'a>(&self, val: &'a Value) -> Option<&'a Value> {
70        match self.traverse(val) {
71            Some(ValuePointer::Existing(v)) => Some(v),
72            _ => None,
73        }
74    }
75
76    pub fn find_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> {
77        match self.traverse_mut(val) {
78            Some(ValuePointerMut::Existing(v)) => Some(v),
79            _ => None,
80        }
81    }
82}
83
84impl FromStr for Pointer {
85    type Err = ParseError;
86
87    fn from_str(s: &str) -> Result<Self, Self::Err> {
88        parser::parse(s)
89    }
90}
91
92impl From<Vec<Step>> for Pointer {
93    fn from(ps: Vec<Step>) -> Self {
94        Self { steps: ps }
95    }
96}
97
98impl Into<Vec<Step>> for Pointer {
99    fn into(self) -> Vec<Step> {
100        self.steps
101    }
102}
103
104impl IntoIterator for Pointer {
105    type Item = Step;
106    type IntoIter = std::vec::IntoIter<Step>;
107
108    fn into_iter(self) -> Self::IntoIter {
109        self.steps.into_iter()
110    }
111}
112
113impl Default for Pointer {
114    fn default() -> Self {
115        Self {
116            steps: Vec::default(),
117        }
118    }
119}