This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

yew_router/
macro_helpers.rs

1pub use urlencoding::{decode as decode_for_url, encode as encode_for_url};
2
3use crate::utils::strip_slash_suffix;
4use crate::Routable;
5
6// re-export Router because the macro needs to access it
7pub type Router = route_recognizer::Router<String>;
8
9/// Build a `route_recognizer::Router` from a `Routable` type.
10pub fn build_router<R: Routable>() -> Router {
11    let mut router = Router::new();
12    R::routes().iter().for_each(|path| {
13        let stripped_route = strip_slash_suffix(path);
14        router.add(stripped_route, path.to_string());
15    });
16
17    router
18}
19
20/// Use a `route_recognizer::Router` to build the route of a `Routable`
21pub fn recognize_with_router<R: Routable>(router: &Router, pathname: &str) -> Option<R> {
22    let pathname = strip_slash_suffix(pathname);
23    let matched = router.recognize(pathname);
24
25    match matched {
26        Ok(matched) => R::from_path(matched.handler(), &matched.params().into_iter().collect())
27            .or_else(R::not_found_route),
28        Err(_) => R::not_found_route(),
29    }
30}