1use std::collections::HashMap;
23pub use yew_router_macro::Routable;
45/// Marks an `enum` as routable.
6///
7/// # Implementation
8///
9/// Use derive macro to implement it. Although it *is* possible to implement it manually,
10/// it is discouraged.
11///
12/// # Usage
13///
14/// The functions exposed by this trait are **not** supposed to be consumed directly. Instead use
15/// the functions exposed at the [crate's root][crate] to perform operations with the router.
16pub trait Routable: Clone + PartialEq {
17/// Converts path to an instance of the routes enum.
18fn from_path(path: &str, params: &HashMap<&str, &str>) -> Option<Self>;
1920/// Converts the route to a string that can passed to the history API.
21fn to_path(&self) -> String;
2223/// Lists all the available routes
24fn routes() -> Vec<&'static str>;
2526/// The route to redirect to on 404
27fn not_found_route() -> Option<Self>;
2829/// Match a route based on the path
30fn recognize(pathname: &str) -> Option<Self>;
31}
3233/// A special route that accepts any route.
34///
35/// This can be used with [`History`](gloo::history::History) and
36/// [`Location`](gloo::history::Location) when the type of [`Routable`] is unknown.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct AnyRoute {
39 path: String,
40}
4142impl Routable for AnyRoute {
43fn from_path(path: &str, params: &HashMap<&str, &str>) -> Option<Self> {
44// No params allowed.
45if params.is_empty() {
46Some(Self {
47 path: path.to_string(),
48 })
49 } else {
50None
51}
52 }
5354fn to_path(&self) -> String {
55self.path.to_string()
56 }
5758fn routes() -> Vec<&'static str> {
59vec!["/*path"]
60 }
6162fn not_found_route() -> Option<Self> {
63Some(Self {
64 path: "/404".to_string(),
65 })
66 }
6768fn recognize(pathname: &str) -> Option<Self> {
69Some(Self {
70 path: pathname.to_string(),
71 })
72 }
73}
7475impl AnyRoute {
76pub fn new<S: Into<String>>(pathname: S) -> Self {
77Self {
78 path: pathname.into(),
79 }
80 }
81}