use crate::tree::Node;
use crate::{InsertError, MatchError, Params};
#[derive(Clone)]
#[cfg_attr(test, derive(Debug))]
pub struct Router<T> {
root: Node<T>,
}
impl<T> Default for Router<T> {
fn default() -> Self {
Self {
root: Node::default(),
}
}
}
impl<T> Router<T> {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, route: impl Into<String>, value: T) -> Result<(), InsertError> {
self.root.insert(route, value)
}
pub fn at<'m, 'p>(&'m self, path: &'p str) -> Result<Match<'m, 'p, &'m T>, MatchError> {
match self.root.at(path.as_bytes()) {
Ok((value, params)) => Ok(Match {
value: unsafe { &*value.get() },
params,
}),
Err(e) => Err(e),
}
}
pub fn at_mut<'m, 'p>(
&'m mut self,
path: &'p str,
) -> Result<Match<'m, 'p, &'m mut T>, MatchError> {
match self.root.at(path.as_bytes()) {
Ok((value, params)) => Ok(Match {
value: unsafe { &mut *value.get() },
params,
}),
Err(e) => Err(e),
}
}
#[cfg(feature = "__test_helpers")]
pub fn check_priorities(&self) -> Result<u32, (u32, u32)> {
self.root.check_priorities()
}
}
#[derive(Debug)]
pub struct Match<'k, 'v, V> {
pub value: V,
pub params: Params<'k, 'v>,
}