use std::collections::HashMap;
pub use yew_router_macro::Routable;
pub trait Routable: Clone + PartialEq {
fn from_path(path: &str, params: &HashMap<&str, &str>) -> Option<Self>;
fn to_path(&self) -> String;
fn routes() -> Vec<&'static str>;
fn not_found_route() -> Option<Self>;
fn recognize(pathname: &str) -> Option<Self>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AnyRoute {
path: String,
}
impl Routable for AnyRoute {
fn from_path(path: &str, params: &HashMap<&str, &str>) -> Option<Self> {
if params.is_empty() {
Some(Self {
path: path.to_string(),
})
} else {
None
}
}
fn to_path(&self) -> String {
self.path.to_string()
}
fn routes() -> Vec<&'static str> {
vec!["/*path"]
}
fn not_found_route() -> Option<Self> {
Some(Self {
path: "/404".to_string(),
})
}
fn recognize(pathname: &str) -> Option<Self> {
Some(Self {
path: pathname.to_string(),
})
}
}
impl AnyRoute {
pub fn new<S: Into<String>>(pathname: S) -> Self {
Self {
path: pathname.into(),
}
}
}