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 {
@@ -44,7 +47,7 @@ impl Child {
44
47
}
45
48
46
49
impl Matcher for Child {
47
- fn select < ' a > ( & ' a self , node : & ' a Value ) -> Box < dyn Iterator < Item = & ' a Value > + ' a > {
50
+ fn select < ' a > ( & ' a self , node : & ' a Value ) -> Iter < ' a > {
48
51
Box :: new ( node. get ( & self . name ) . into_iter ( ) )
49
52
}
50
53
}
@@ -60,7 +63,7 @@ impl Union {
60
63
}
61
64
62
65
impl Matcher for Union {
63
- fn select < ' a > ( & ' a self , node : & ' a Value ) -> Box < dyn Iterator < Item = & ' a Value > + ' a > {
66
+ fn select < ' a > ( & ' a self , node : & ' a Value ) -> Iter < ' a > {
64
67
Box :: new ( self . elements . iter ( ) . flat_map ( move |it| it. select ( node) ) )
65
68
}
66
69
}
0 commit comments