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
//! Routing target

use std::fmt::Debug;
use yew::Callback;

/// A target for used by a router.
pub trait Target: Clone + Debug + Eq + 'static {
    /// Render only our path segment.
    fn render_self(&self) -> Vec<String> {
        let mut path = vec![];
        self.render_self_into(&mut path);
        path
    }

    /// Render the full path, including our children.
    fn render_path(&self) -> Vec<String> {
        let mut path = vec![];
        self.render_path_into(&mut path);
        path
    }

    /// Render only our own path component.
    fn render_self_into(&self, path: &mut Vec<String>);

    /// Render the full path downwards.
    fn render_path_into(&self, path: &mut Vec<String>);

    /// Parse the target from the provided (segmented) path.
    ///
    /// The path will be the local path, with the prefix already removed.
    fn parse_path(path: &[&str]) -> Option<Self>;
}

#[derive(Debug, PartialEq)]
pub struct Mapper<P, C> {
    pub downwards: Callback<P, Option<C>>,
    pub upwards: Callback<C, P>,
}

impl<P, C> Clone for Mapper<P, C>
where
    P: Target,
    C: Target,
{
    fn clone(&self) -> Self {
        Self {
            downwards: self.downwards.clone(),
            upwards: self.upwards.clone(),
        }
    }
}

impl<P, C> Mapper<P, C>
where
    P: Target,
    C: Target,
{
    pub fn new<PF, CF>(downwards: PF, upwards: CF) -> Self
    where
        PF: Fn(P) -> Option<C> + 'static,
        CF: Fn(C) -> P + 'static,
    {
        Self {
            downwards: downwards.into(),
            upwards: upwards.into(),
        }
    }

    pub fn new_callback<PF, CF>(downwards: PF, upwards: CF) -> Callback<(), Self>
    where
        PF: Fn(P) -> Option<C> + 'static,
        CF: Fn(C) -> P + 'static,
    {
        Self::new(downwards, upwards).into()
    }
}

impl<P, C> From<Mapper<P, C>> for Callback<(), Mapper<P, C>>
where
    P: Target,
    C: Target,
{
    fn from(mapper: Mapper<P, C>) -> Self {
        Callback::from(move |()| mapper.clone())
    }
}

impl<P, C, PF, CF> From<(PF, CF)> for Mapper<P, C>
where
    P: Target,
    C: Target,
    PF: Fn(P) -> Option<C> + 'static,
    CF: Fn(C) -> P + 'static,
{
    fn from((down, up): (PF, CF)) -> Self {
        Self::new(down, up)
    }
}