7
7
use serde_json:: Value ;
8
8
use std:: iter;
9
9
10
+ /// An iterator over matcher selection results.
11
+ type Iter < ' a > = Box < dyn Iterator < Item = & ' a Value > + ' a > ;
12
+
10
13
// Matcher maps a node to a list of nodes. If the input node is not matched by the matcher or
11
14
// the matcher does not select any subnodes of the input node, then the result is empty.
12
15
pub trait Matcher {
13
- fn select < ' a > ( & ' a self , node : & ' a Value ) -> Box < dyn Iterator < Item = & ' a Value > + ' a > ;
16
+ fn select < ' a > ( & ' a self , node : & ' a Value ) -> Iter < ' a > ;
14
17
}
15
18
16
19
pub struct RootSelector { }
17
20
18
21
impl Matcher for RootSelector {
19
- fn select < ' a > ( & ' a self , node : & ' a Value ) -> Box < dyn Iterator < Item = & ' a Value > + ' a > {
22
+ fn select < ' a > ( & ' a self , node : & ' a Value ) -> Iter < ' a > {
20
23
Box :: new ( iter:: once ( node) )
21
24
}
22
25
}
23
26
24
27
pub struct WildcardedChild { }
25
28
26
29
impl Matcher for WildcardedChild {
27
- fn select < ' a > ( & self , node : & ' a Value ) -> Box < dyn Iterator < Item = & ' a Value > + ' a > {
30
+ fn select < ' a > ( & self , node : & ' a Value ) -> Iter < ' a > {
28
31
if let Some ( m) = node. as_object ( ) {
29
32
Box :: new ( m. values ( ) )
30
33
} else {
@@ -42,7 +45,7 @@ pub fn new_child_matcher(name: String) -> Child {
42
45
}
43
46
44
47
impl Matcher for Child {
45
- fn select < ' a > ( & ' a self , node : & ' a Value ) -> Box < dyn Iterator < Item = & ' a Value > + ' a > {
48
+ fn select < ' a > ( & ' a self , node : & ' a Value ) -> Iter < ' a > {
46
49
Box :: new ( node. get ( & self . name ) . into_iter ( ) )
47
50
}
48
51
}
@@ -56,7 +59,7 @@ pub fn new_union(elements: Vec<Box<dyn Matcher>>) -> Union {
56
59
}
57
60
58
61
impl Matcher for Union {
59
- fn select < ' a > ( & ' a self , node : & ' a Value ) -> Box < dyn Iterator < Item = & ' a Value > + ' a > {
62
+ fn select < ' a > ( & ' a self , node : & ' a Value ) -> Iter < ' a > {
60
63
Box :: new ( self . elements . iter ( ) . flat_map ( move |it| it. select ( node) ) )
61
64
}
62
65
}
0 commit comments