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) {
let mut info = RouteInfo::new(route.handler)
.with_before(route.before)
.with_socket(route.socket);
if route.xhr_only {
info = info.xhr_only();
}
self.trie.add_route(&route.method, &route.path, info);
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
}
}