Struct Router

Source
pub struct Router<T> { /* private fields */ }
Expand description

The complete route matcher

Implementations§

Source§

impl<T> Router<T>

Source

pub fn build() -> RouterBuilder<T>

Build a new router

Examples found in repository?
examples/simple.rs (line 2)
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}
Source

pub fn recognize<'a>(&'a self, path: &str) -> Result<Match<'a, T>>

Match a route and return match data

Examples found in repository?
examples/simple.rs (line 12)
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}

Auto Trait Implementations§

§

impl<T> !Freeze for Router<T>

§

impl<T> !RefUnwindSafe for Router<T>

§

impl<T> Send for Router<T>
where T: Send,

§

impl<T> Sync for Router<T>
where T: Sync,

§

impl<T> Unpin for Router<T>
where T: Unpin,

§

impl<T> UnwindSafe for Router<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.