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
use super::*;

use std::collections::HashSet;


#[derive(Debug)]
pub struct NodePathMatcher {
    paths: HashSet<Opath>,
}

impl NodePathMatcher {
    pub fn new() -> NodePathMatcher {
        NodePathMatcher {
            paths: HashSet::new(),
        }
    }

    pub fn insert(&mut self, n: &NodeRef) {
        let path = Opath::from(n);
        self.paths.insert(path);
    }

    pub fn insert_cache(&mut self, n: &NodeRef, cache: &mut dyn OpathCache) {
        let path = cache.get(n).clone();
        self.paths.insert(path);
    }

    pub fn resolve(&mut self, expr: &Opath, root: &NodeRef, current: &NodeRef) {
        let res = expr.apply(root, current);
        for ref n in res {
            self.insert(n);
        }
    }

    pub fn resolve_cache(&mut self, expr: &Opath, root: &NodeRef, current: &NodeRef, cache: &mut dyn OpathCache) {
        let res = expr.apply(root, current);
        for ref n in res {
            self.insert_cache(n, cache);
        }
    }

    pub fn resolve_ext(&mut self, expr: &Opath, root: &NodeRef, current: &NodeRef, scope: &Scope) {
        let res = expr.apply_ext(root, current, scope);
        for ref n in res {
            self.insert(n);
        }
    }

    pub fn resolve_ext_cache(&mut self, expr: &Opath, root: &NodeRef, current: &NodeRef, scope: &Scope, cache: &mut dyn OpathCache) {
        let res = expr.apply_ext(root, current, scope);
        for ref n in res {
            self.insert_cache(n, cache);
        }
    }

    pub fn matches(&self, path: &Opath) -> bool {
        self.paths.contains(path)
    }

    pub fn clear(&mut self) {
        self.paths.clear();
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    fn test_node<'a>() -> NodeRef {
        let jsona = r#"
        {
            "pa": "papa",
            "star": "*",
            "propa1": {
                "aa": {
                    "bb": "aaaa",
                    "dd": [12,13, 14,20,34],
                    "cc": false
                }
            }
        }"#;

        NodeRef::from_json(jsona).unwrap()
    }

    #[test]
    fn without_cache() {
        let n = test_node();

        let mut m = NodePathMatcher::new();

        let expr = Opath::parse("$.*").unwrap();
        m.resolve(&expr, &n, &n);

        assert!(m.matches(&Opath::parse("$.pa").unwrap()));
    }

    #[test]
    fn with_cache() {
        let n = test_node();

        let mut m = NodePathMatcher::new();
        let mut cache = NodePathLruCache::with_size(128);

        let expr = Opath::parse("$.*").unwrap();
        m.resolve_cache(&expr, &n, &n, &mut cache);

        assert!(m.matches(&Opath::parse("$.pa").unwrap()));
        assert_eq!(cache.len(), 3);
    }
}