@@ -55,16 +55,94 @@ impl<'a> Router<'a> {
5555 }
5656
5757 pub fn execute ( & self , context : & mut Context ) -> bool {
58+ if let Some ( m) = self . try_match ( context) {
59+ context. result = Some ( m) ;
60+ true
61+ } else {
62+ false
63+ }
64+ }
65+
66+ /// Note that unlike `execute`, this doesn't set `Context.result`
67+ /// but it also doesn't need a `&mut Context`.
68+ pub fn try_match ( & self , context : & Context ) -> Option < Match > {
5869 for ( MatcherKey ( _, id) , m) in self . matchers . iter ( ) . rev ( ) {
5970 let mut mat = Match :: new ( ) ;
6071 if m. execute ( context, & mut mat) {
6172 mat. uuid = * id;
62- context. result = Some ( mat) ;
63-
64- return true ;
73+ return Some ( mat) ;
6574 }
6675 }
6776
68- false
77+ None
78+ }
79+ }
80+
81+ #[ cfg( test) ]
82+ mod tests {
83+ use uuid:: Uuid ;
84+
85+ use crate :: { ast:: Type , context:: Context , schema:: Schema } ;
86+
87+ use super :: Router ;
88+
89+ #[ test]
90+ fn execute_succeeds ( ) {
91+ let mut schema = Schema :: default ( ) ;
92+ schema. add_field ( "http.path" , Type :: String ) ;
93+
94+ let mut router = Router :: new ( & schema) ;
95+ router
96+ . add_matcher ( 0 , Uuid :: default ( ) , "http.path == \" /dev\" " )
97+ . expect ( "should add" ) ;
98+
99+ let mut ctx = Context :: new ( & schema) ;
100+ ctx. add_value ( "http.path" , "/dev" . to_owned ( ) . into ( ) ) ;
101+ assert ! ( router. execute( & mut ctx) ) ;
102+ }
103+
104+ #[ test]
105+ fn execute_fails ( ) {
106+ let mut schema = Schema :: default ( ) ;
107+ schema. add_field ( "http.path" , Type :: String ) ;
108+
109+ let mut router = Router :: new ( & schema) ;
110+ router
111+ . add_matcher ( 0 , Uuid :: default ( ) , "http.path == \" /dev\" " )
112+ . expect ( "should add" ) ;
113+
114+ let mut ctx = Context :: new ( & schema) ;
115+ ctx. add_value ( "http.path" , "/not-dev" . to_owned ( ) . into ( ) ) ;
116+ assert ! ( !router. execute( & mut ctx) ) ;
117+ }
118+
119+ #[ test]
120+ fn try_match_succeeds ( ) {
121+ let mut schema = Schema :: default ( ) ;
122+ schema. add_field ( "http.path" , Type :: String ) ;
123+
124+ let mut router = Router :: new ( & schema) ;
125+ router
126+ . add_matcher ( 0 , Uuid :: default ( ) , "http.path == \" /dev\" " )
127+ . expect ( "should add" ) ;
128+
129+ let mut ctx = Context :: new ( & schema) ;
130+ ctx. add_value ( "http.path" , "/dev" . to_owned ( ) . into ( ) ) ;
131+ router. try_match ( & ctx) . expect ( "matches" ) ;
132+ }
133+
134+ #[ test]
135+ fn try_match_fails ( ) {
136+ let mut schema = Schema :: default ( ) ;
137+ schema. add_field ( "http.path" , Type :: String ) ;
138+
139+ let mut router = Router :: new ( & schema) ;
140+ router
141+ . add_matcher ( 0 , Uuid :: default ( ) , "http.path == \" /dev\" " )
142+ . expect ( "should add" ) ;
143+
144+ let mut ctx = Context :: new ( & schema) ;
145+ ctx. add_value ( "http.path" , "/not-dev" . to_owned ( ) . into ( ) ) ;
146+ router. try_match ( & ctx) . ok_or ( ( ) ) . expect_err ( "should fail" ) ;
69147 }
70148}
0 commit comments