pub use urlencoding::{decode as decode_for_url, encode as encode_for_url};
pub fn encode_path_for_url(path: &str) -> String {
path.split('/')
.map(encode_for_url)
.collect::<Vec<_>>()
.join("/")
}
use crate::utils::strip_slash_suffix;
use crate::Routable;
pub type Router = route_recognizer::Router<String>;
pub fn build_router<R: Routable>() -> Router {
let mut router = Router::new();
R::routes().iter().for_each(|path| {
let stripped_route = strip_slash_suffix(path);
router.add(stripped_route, path.to_string());
});
router
}
pub fn recognize_with_router<R: Routable>(router: &Router, pathname: &str) -> Option<R> {
let pathname = strip_slash_suffix(pathname);
let matched = router.recognize(pathname);
match matched {
Ok(matched) => R::from_path(matched.handler(), &matched.params().into_iter().collect())
.or_else(R::not_found_route),
Err(_) => R::not_found_route(),
}
}