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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use crate::{
agent::{RouteAgentBridge, RouteRequest},
route::Route,
RouteState, Switch,
};
use std::{
fmt::{self, Debug, Error as FmtError, Formatter},
rc::Rc,
};
use yew::{html, virtual_dom::VNode, Component, ComponentLink, Html, Properties, ShouldRender};
pub trait RouterState: RouteState + PartialEq {}
impl<STATE> RouterState for STATE where STATE: RouteState + PartialEq {}
#[derive(Debug)]
pub struct Router<SW: Switch + Clone + 'static, STATE: RouterState = ()> {
switch: Option<SW>,
props: Props<STATE, SW>,
router_agent: RouteAgentBridge<STATE>,
}
impl<SW, STATE> Router<SW, STATE>
where
STATE: RouterState,
SW: Switch + Clone + 'static,
{
pub fn render<F: RenderFn<Router<SW, STATE>, SW> + 'static>(f: F) -> Render<SW, STATE> {
Render::new(f)
}
pub fn redirect<F: RedirectFn<SW, STATE> + 'static>(f: F) -> Option<Redirect<SW, STATE>> {
Some(Redirect::new(f))
}
}
#[derive(Debug, Clone)]
pub enum Msg<STATE> {
UpdateRoute(Route<STATE>),
}
pub trait RenderFn<CTX: Component, SW>: Fn(SW) -> Html {}
impl<T, CTX: Component, SW> RenderFn<CTX, SW> for T where T: Fn(SW) -> Html {}
#[derive(Clone)]
pub struct Render<SW: Switch + Clone + 'static, STATE: RouterState = ()>(
pub(crate) Rc<dyn RenderFn<Router<SW, STATE>, SW>>,
);
impl<STATE: RouterState, SW: Switch + Clone> Render<SW, STATE> {
fn new<F: RenderFn<Router<SW, STATE>, SW> + 'static>(f: F) -> Self {
Render(Rc::new(f))
}
}
impl<STATE: RouterState, SW: Switch + Clone> Debug for Render<SW, STATE> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Render").finish()
}
}
pub trait RedirectFn<SW, STATE>: Fn(Route<STATE>) -> SW {}
impl<T, SW, STATE> RedirectFn<SW, STATE> for T where T: Fn(Route<STATE>) -> SW {}
#[derive(Clone)]
pub struct Redirect<SW: Switch + 'static, STATE: RouterState>(
pub(crate) Rc<dyn RedirectFn<SW, STATE>>,
);
impl<STATE: RouterState, SW: Switch + 'static> Redirect<SW, STATE> {
fn new<F: RedirectFn<SW, STATE> + 'static>(f: F) -> Self {
Redirect(Rc::new(f))
}
}
impl<STATE: RouterState, SW: Switch> Debug for Redirect<SW, STATE> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Redirect").finish()
}
}
#[derive(Properties, Clone)]
pub struct Props<STATE: RouterState, SW: Switch + Clone + 'static> {
pub render: Render<SW, STATE>,
#[prop_or_default]
pub redirect: Option<Redirect<SW, STATE>>,
}
impl<STATE: RouterState, SW: Switch + Clone> Debug for Props<STATE, SW> {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
f.debug_struct("Props").finish()
}
}
impl<STATE, SW> Component for Router<SW, STATE>
where
STATE: RouterState,
SW: Switch + Clone + 'static,
{
type Message = Msg<STATE>;
type Properties = Props<STATE, SW>;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
let callback = link.callback(Msg::UpdateRoute);
let mut router_agent = RouteAgentBridge::new(callback);
router_agent.send(RouteRequest::GetCurrentRoute);
Router {
switch: Default::default(),
props,
router_agent,
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::UpdateRoute(route) => {
let mut switch = SW::switch(route.clone());
if switch.is_none() {
if let Some(redirect) = &self.props.redirect {
let redirected: SW = (&redirect.0)(route);
log::trace!(
"Route failed to match, but redirecting route to a known switch."
);
self.router_agent
.send(RouteRequest::ReplaceRouteNoBroadcast(
redirected.clone().into(),
));
switch = Some(redirected)
}
}
self.switch = switch;
true
}
}
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.props = props;
true
}
fn view(&self) -> VNode {
match self.switch.clone() {
Some(switch) => (&self.props.render.0)(switch),
None => {
log::warn!("No route matched, provide a redirect prop to the router to handle cases where no route can be matched");
html! {"No route matched"}
}
}
}
}