json_pointer_simd/
borrowed.rs

1use std::ops::{Index, IndexMut};
2use crate::{JsonPointer, JsonPointerTarget, IndexError};
3use simd_json::value::borrowed::Value;
4
5/// Implement getting for SIMD JSON Borrowed values
6///
7impl<'a> JsonPointerTarget for Value<'a> 
8    where Self: Sized {
9    fn get<'json, S: AsRef<str>, C: AsRef<[S]>>(&'json self, ptr: &JsonPointer<S,C>) -> Result<&'json Self, IndexError> {
10        ptr.ref_toks.as_ref().iter().try_fold(self, |val, tok| {
11            let tok = tok.as_ref();
12            match *val {
13                Value::Object(ref obj) => obj.get(tok).ok_or_else(|| IndexError::NoSuchKey(tok.to_owned())),
14                Value::Array(ref arr) => {
15                    let idx = if tok == "-" {
16                        arr.len()
17                    } else if let Ok(idx) = tok.parse() {
18                        idx
19                    } else {
20                        return Err(IndexError::NoSuchKey(tok.to_owned()));
21                    };
22                    arr.get(idx).ok_or(IndexError::OutOfBounds(idx))
23                },
24                _ => Err(IndexError::NotIndexable),
25            }
26        })
27    }
28
29    fn get_mut<'json, S: AsRef<str>, C: AsRef<[S]>>(&'json mut self, ptr: &JsonPointer<S,C>) -> Result<&'json mut Self, IndexError> {
30        ptr.ref_toks.as_ref().iter().try_fold(self, |val, tok| {
31            let tok = tok.as_ref();
32            match *val {
33                Value::Object(ref mut obj) => obj.get_mut(tok).ok_or_else(|| IndexError::NoSuchKey(tok.to_owned())),
34                Value::Array(ref mut arr) => {
35                    let idx = if tok == "-" {
36                        arr.len()
37                    } else if let Ok(idx) = tok.parse() {
38                        idx
39                    } else {
40                        return Err(IndexError::NoSuchKey(tok.to_owned()));
41                    };
42                    arr.get_mut(idx).ok_or(IndexError::OutOfBounds(idx))
43                },
44                _ => Err(IndexError::NotIndexable),
45            }
46        })
47    }
48
49    fn get_owned<'json, S: AsRef<str>, C: AsRef<[S]>>(self, ptr: &JsonPointer<S,C>) -> Result<Self, IndexError> {
50        ptr.ref_toks.as_ref().iter().try_fold(self, |val, tok| {
51            let tok = tok.as_ref();
52            match val {
53                Value::Object(mut obj) => obj.remove(tok).ok_or_else(|| IndexError::NoSuchKey(tok.to_owned())),
54                Value::Array(mut arr) => {
55                    let idx = if tok == "-" {
56                        arr.len()
57                    } else if let Ok(idx) = tok.parse() {
58                        idx
59                    } else {
60                        return Err(IndexError::NoSuchKey(tok.to_owned()));
61                    };
62                    if idx >= arr.len() {
63                        Err(IndexError::OutOfBounds(idx))
64                    } else {
65                        Ok(arr.swap_remove(idx))
66                    }
67                },
68                _ => Err(IndexError::NotIndexable),
69            }
70        })
71    }
72}
73impl<'a, S: AsRef<str>, C: AsRef<[S]>> Index<&'a JsonPointer<S, C>> for Value<'a> {
74    type Output = Value<'a>;
75    fn index(&self, ptr: &'a JsonPointer<S, C>) -> &Value<'a> {
76        self.get(ptr).unwrap()
77    }
78}
79
80impl<'a, S: AsRef<str>, C: AsRef<[S]>> IndexMut<&'a JsonPointer<S, C>> for Value<'a> {
81    fn index_mut(&mut self, ptr: &'a JsonPointer<S, C>) -> &mut Value<'a> {
82        self.get_mut(ptr).unwrap()
83    }
84}