1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::service::RouteService;
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Deref};
use stdweb::{unstable::TryFrom, JsSerialize, Value};
use yew::agent::Transferable;
pub trait RouteState: Clone + Default + JsSerialize + TryFrom<Value> + 'static {}
impl<T> RouteState for T where T: Clone + Default + JsSerialize + TryFrom<Value> + 'static {}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Route<T> {
pub route: String,
pub state: Option<T>,
}
pub(crate) fn format_route_string(path: &str, query: &str, fragment: &str) -> String {
format!(
"{path}{query}{fragment}",
path = path,
query = query,
fragment = fragment
)
}
impl<T> Route<T> {
pub fn current_route(route_service: &RouteService<T>) -> Self {
let route = route_service.get_route();
Route { route, state: None }
}
}
impl<T> fmt::Display for Route<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.route.fmt(f)
}
}
impl<T> From<String> for Route<T> {
fn from(string: String) -> Route<T> {
Route {
route: string,
state: None,
}
}
}
impl<T> From<&str> for Route<T> {
fn from(string: &str) -> Route<T> {
Route {
route: string.to_string(),
state: None,
}
}
}
impl<T> Deref for Route<T> {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.route
}
}
impl<T> Transferable for Route<T> where for<'de> T: Serialize + Deserialize<'de> {}