use super::trie::{RouteInfo, TrieRouter};
use super::Route;
use std::collections::HashMap;
pub struct Router {
trie: TrieRouter,
route_count: usize,
}
impl Default for Router {
fn default() -> Self {
Self::new()
}
}
impl Router {
pub fn new() -> Self {
Self {
trie: TrieRouter::new(),
route_count: 0,
}
}
pub fn add_route(&mut self, route: Route) {
self.trie.add_route(
&route.method,
&route.path,
route.handler,
route.xhr_only,
route.before,
);
self.route_count += if route.method == "XHR" { 2 } else { 1 };
}
pub fn match_route(
&self,
method: &str,
path: &str,
) -> Option<(&RouteInfo, HashMap<String, String>)> {
self.trie.match_route(method, path)
}
pub fn route_count(&self) -> usize {
self.route_count
}
pub fn is_empty(&self) -> bool {
self.route_count == 0
}
}