macro_rules! route {
(/ => $callback:ident) => { ... };
(/?$($expected_query:tt)+ => $callback:ident) => { ... };
(/$($expected_segment:tt)/+ => $callback:ident) => { ... };
(/$($expected_segment:tt)/+?$($expected_query:tt)&+ => $callback: ident) => { ... };
(@try $callback:ident [$($expected_segment:tt)*] [$($expected_query:tt)*] $uri:ident) => { ... };
(@parse_segments [$($expected_segment:tt)*] $path:ident) => { ... };
(@parse_segment ($name:tt : $type:ty) $data:expr) => { ... };
(@parse_segment $name:tt $data:ident) => { ... };
(@segment_pattern ($name:tt : $type:ty)) => { ... };
(@segment_pattern $name:tt) => { ... };
(@parse_queries [$($expected_query:tt)*] $query_string:ident) => { ... };
(@query_name ($name:tt : $type:ty)) => { ... };
(@query_name $name:tt) => { ... };
(@query_type ($name:tt : $type:ty)) => { ... };
(@query_type $name:tt) => { ... };
(@parse_query ($name:tt : $type:ty) $data:ident) => { ... };
(@parse_query $name:tt $data:ident) => { ... };
}Expand description
Build a route from a pattern and a callback.
route!(pattern => callback) is a function from &str to Option<T>. The input represents a
URL’s path and query string. If the input matches the pattern, its segments and query
parameters can be captured and exposed as parameters of the callback.
A pattern is defined with the following grammar:
pattern := path | path ? query_string
path := root | root some_segments
root := /
some_segments := segment | segment / some_segments
segment := ident | (ident: type)
query_string := query | query & query_string
query := ident | (ident: type)The callback is a function from captured segments and query parameters to T. Captured
segments are passed to the callback. All query parameters are captured. Query parameters
without type annotation are passed as Option<()>, query parameters with type annotation are
passed as Option<U>. Any type U can be captured as long as they implement the FromStr
trait.
Examples:
- Matches
/[?query_string], captures nothing and callscallback():
route!(/ => callback)- Matches
/help[?query_string], captures nothing and callscallback():
route!(/help => callback)- Matches
/sum/<a: u32>/<b: u32>[?query_string], capturesaandbasu32s and callscallback(a: u32, b: u32):
route!(/sum/(a: u32)/(b: u32) => callback)- Matches
/maps/<lon: f64>/<lat: f64>?show_marker&color=<Color>, captureslonandlatasf64s,show_markerasOption<()>andcolorasOption<Color>, and callscallback(lon: f64, lat: f64, show_marker: Option<()>, color: Option<Color>):
route!(/maps/(lon: f64)/(lat: f64)?show_marker&(color: Color) => callback)