Skip to main content

freya_router/
router_cfg.rs

1use crate::prelude::Routable;
2
3/// Configuration used when a [`Router`](crate::components::Router) is created.
4///
5/// By default, the router starts at `/`.
6/// Use [`Self::with_initial_path`] to change the initial route.
7///
8/// ```rust,no_run
9/// # use freya_router::prelude::*;
10/// # use freya_core::prelude::*;
11/// # #[derive(PartialEq)]
12/// # struct Index;
13/// # impl Component for Index {
14/// #    fn render(&self) -> impl IntoElement {
15/// #        rect()
16/// #    }
17/// # }
18/// #[derive(Clone, Routable)]
19/// enum Route {
20///     #[route("/")]
21///     Index {},
22/// }
23///
24/// let cfg = RouterConfig::<Route>::default().with_initial_path(Route::Index {});
25/// ```
26pub struct RouterConfig<R: Routable> {
27    pub(crate) initial_path: Option<R>,
28}
29
30impl<R: Routable> Default for RouterConfig<R> {
31    fn default() -> Self {
32        Self { initial_path: None }
33    }
34}
35
36impl<R: Routable> RouterConfig<R> {
37    /// Sets the route displayed when the router is first created.
38    pub fn with_initial_path(mut self, initial_path: R) -> Self {
39        self.initial_path = Some(initial_path);
40        self
41    }
42}