pub struct RouterBuilder<T> { /* private fields */ }
Expand description
Builder for a Router
Implementations§
Source§impl<T> RouterBuilder<T>
impl<T> RouterBuilder<T>
Sourcepub fn add<I: Into<String>>(self, regex: I, handler: T) -> Self
pub fn add<I: Into<String>>(self, regex: I, handler: T) -> Self
Add a route with handler and default priority 0
Examples found in repository?
examples/simple.rs (line 3)
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let router = reset_recognizer::Router::build()
3 .add(r"^/posts/(\d+)/comments/(\d+)$", "comment".to_string())
4 .add(r"^/posts/(\d+)/comments$", "comments".to_string())
5 .add(r"^/posts/(\d+)$", "post".to_string())
6 .add(r"^/posts$", "posts".to_string())
7 .add(r"^/comments$", "comments2".to_string())
8 .add(r"^/comments/(\d+)$", "comment2".to_string())
9 .add_with_priority(r"^/(.+)$", -1, "not_found".to_string())
10 .finish()?;
11
12 let matched = router.recognize("/posts/100/comments/200")?;
13
14 let (post_id, comment_id) = matched.captures.parsed::<(i32, i32)>()?;
15
16 std::thread::spawn(move || {
17 println!("{:?}", (&post_id, &comment_id));
18 }).join().unwrap();
19
20
21
22 Ok(())
23}
Sourcepub fn add_with_priority<I: Into<String>>(
self,
regex: I,
priority: i8,
handler: T,
) -> Self
pub fn add_with_priority<I: Into<String>>( self, regex: I, priority: i8, handler: T, ) -> Self
Add a route with handler and priority (higher is better)
Examples found in repository?
examples/simple.rs (line 9)
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let router = reset_recognizer::Router::build()
3 .add(r"^/posts/(\d+)/comments/(\d+)$", "comment".to_string())
4 .add(r"^/posts/(\d+)/comments$", "comments".to_string())
5 .add(r"^/posts/(\d+)$", "post".to_string())
6 .add(r"^/posts$", "posts".to_string())
7 .add(r"^/comments$", "comments2".to_string())
8 .add(r"^/comments/(\d+)$", "comment2".to_string())
9 .add_with_priority(r"^/(.+)$", -1, "not_found".to_string())
10 .finish()?;
11
12 let matched = router.recognize("/posts/100/comments/200")?;
13
14 let (post_id, comment_id) = matched.captures.parsed::<(i32, i32)>()?;
15
16 std::thread::spawn(move || {
17 println!("{:?}", (&post_id, &comment_id));
18 }).join().unwrap();
19
20
21
22 Ok(())
23}
Sourcepub fn finish(self) -> Result<Router<T>>
pub fn finish(self) -> Result<Router<T>>
Build finished router
Examples found in repository?
examples/simple.rs (line 10)
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let router = reset_recognizer::Router::build()
3 .add(r"^/posts/(\d+)/comments/(\d+)$", "comment".to_string())
4 .add(r"^/posts/(\d+)/comments$", "comments".to_string())
5 .add(r"^/posts/(\d+)$", "post".to_string())
6 .add(r"^/posts$", "posts".to_string())
7 .add(r"^/comments$", "comments2".to_string())
8 .add(r"^/comments/(\d+)$", "comment2".to_string())
9 .add_with_priority(r"^/(.+)$", -1, "not_found".to_string())
10 .finish()?;
11
12 let matched = router.recognize("/posts/100/comments/200")?;
13
14 let (post_id, comment_id) = matched.captures.parsed::<(i32, i32)>()?;
15
16 std::thread::spawn(move || {
17 println!("{:?}", (&post_id, &comment_id));
18 }).join().unwrap();
19
20
21
22 Ok(())
23}
Trait Implementations§
Source§impl<T: Default> Default for RouterBuilder<T>
impl<T: Default> Default for RouterBuilder<T>
Source§fn default() -> RouterBuilder<T>
fn default() -> RouterBuilder<T>
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl<T> Freeze for RouterBuilder<T>
impl<T> RefUnwindSafe for RouterBuilder<T>where
T: RefUnwindSafe,
impl<T> Send for RouterBuilder<T>where
T: Send,
impl<T> Sync for RouterBuilder<T>where
T: Sync,
impl<T> Unpin for RouterBuilder<T>where
T: Unpin,
impl<T> UnwindSafe for RouterBuilder<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more