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
use super::super::OrdersContainer;
use crate::browser::Url;
use crate::virtual_dom::View;

#[allow(clippy::module_name_repetitions)]
pub struct UndefinedAfterMount;

// ------ UrlHandling ------

/// Used for handling initial routing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UrlHandling {
    PassToRoutes,
    None,
}

impl Default for UrlHandling {
    fn default() -> Self {
        Self::PassToRoutes
    }
}

// ------ AfterMount ------

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AfterMount<Mdl> {
    /// Initial model to be used when the app begins.
    pub(crate) model: Mdl,
    /// How to handle initial url routing. Defaults to [`UrlHandling::PassToRoutes`] in the
    /// constructors.
    pub(crate) url_handling: UrlHandling,
}

impl<Mdl> AfterMount<Mdl> {
    /// Creates a new `AfterMount` instance. You can also use `AfterMount::default`
    /// if your `Model` implements `Default`.
    pub fn new(model: Mdl) -> Self {
        Self {
            model,
            url_handling: UrlHandling::default(),
        }
    }

    /// - `UrlHandling::PassToRoutes` - your function `routes` will be called with initial URL. _[Default]_
    /// - `UrlHandling::None` - URL won't be handled by Seed.
    pub const fn url_handling(mut self, url_handling: UrlHandling) -> Self {
        self.url_handling = url_handling;
        self
    }
}

// ------ IntoAfterMount ------

#[allow(clippy::module_name_repetitions)]
pub trait IntoAfterMount<Ms: 'static, Mdl, ElC: View<Ms>, GMs> {
    fn into_after_mount(
        self: Box<Self>,
        init_url: Url,
        orders: &mut OrdersContainer<Ms, Mdl, ElC, GMs>,
    ) -> AfterMount<Mdl>;
}

impl<Ms: 'static, Mdl, ElC: View<Ms>, GMs, F> IntoAfterMount<Ms, Mdl, ElC, GMs> for F
where
    F: FnOnce(Url, &mut OrdersContainer<Ms, Mdl, ElC, GMs>) -> AfterMount<Mdl>,
{
    fn into_after_mount(
        self: Box<Self>,
        init_url: Url,
        orders: &mut OrdersContainer<Ms, Mdl, ElC, GMs>,
    ) -> AfterMount<Mdl> {
        self(init_url, orders)
    }
}

impl<Ms: 'static, Mdl: Default, ElC: View<Ms>, GMs> IntoAfterMount<Ms, Mdl, ElC, GMs>
    for UndefinedAfterMount
{
    fn into_after_mount(
        self: Box<Self>,
        _: Url,
        _: &mut OrdersContainer<Ms, Mdl, ElC, GMs>,
    ) -> AfterMount<Mdl> {
        AfterMount::default()
    }
}