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
use serde_json::Value;

use crate::{MalformedPointerError, Pointer};
/// Delete is implemented by types which can internally remove a value based on
/// a JSON Pointer
pub trait Delete {
    /// Error associated with `Delete`
    type Error;
    /// Attempts to internally delete a value based upon a [Pointer].
    fn delete(&mut self, ptr: &Pointer) -> Option<Value>;
}

impl Delete for Value {
    type Error = MalformedPointerError;
    fn delete(&mut self, ptr: &Pointer) -> Option<Value> {
        ptr.delete(self)
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    #[test]
    fn test_issue_18() {
        let mut data = json!(
            {
                "Example": 21,
                "test": "test"
              }
        );
        let pointer = Pointer::new(["Example"]);
        println!("{}", pointer);
        pointer.delete(&mut data);
        assert_eq!(json!({"test": "test"}), data);
    }
}