yew_struct_component/
lib.rs

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
//! Define [Yew](https://yew.rs/) components using structs.

use std::{collections::HashMap, ops::Deref};

use yew::{html::IntoPropValue, AttrValue};
pub use yew_struct_component_macro::*;

#[derive(Clone, Debug, PartialEq)]
pub struct Attributes(HashMap<AttrValue, Option<AttrValue>>);

impl Deref for Attributes {
    type Target = HashMap<AttrValue, Option<AttrValue>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl IntoPropValue<Attributes> for HashMap<AttrValue, Option<AttrValue>> {
    fn into_prop_value(self) -> Attributes {
        Attributes(self)
    }
}

impl IntoPropValue<Attributes> for HashMap<AttrValue, AttrValue> {
    fn into_prop_value(self) -> Attributes {
        Attributes(
            self.into_iter()
                .map(|(key, value)| (key, Some(value)))
                .collect(),
        )
    }
}

impl IntoPropValue<Attributes> for HashMap<String, Option<String>> {
    fn into_prop_value(self) -> Attributes {
        Attributes(
            self.into_iter()
                .map(|(key, value)| (AttrValue::from(key), value.map(AttrValue::from)))
                .collect(),
        )
    }
}

impl IntoPropValue<Attributes> for HashMap<String, String> {
    fn into_prop_value(self) -> Attributes {
        Attributes(
            self.into_iter()
                .map(|(key, value)| (AttrValue::from(key), Some(AttrValue::from(value))))
                .collect(),
        )
    }
}

impl<const N: usize> IntoPropValue<Attributes> for [(AttrValue, Option<AttrValue>); N] {
    fn into_prop_value(self) -> Attributes {
        Attributes(HashMap::from_iter(self))
    }
}

impl<const N: usize> IntoPropValue<Attributes> for [(AttrValue, AttrValue); N] {
    fn into_prop_value(self) -> Attributes {
        Attributes(HashMap::from_iter(
            self.map(|(key, value)| (key, Some(value))),
        ))
    }
}

impl<const N: usize> IntoPropValue<Attributes> for [(&str, Option<&str>); N] {
    fn into_prop_value(self) -> Attributes {
        Attributes(HashMap::from_iter(self.map(|(key, value)| {
            (
                AttrValue::from(key.to_string()),
                value.map(|value| AttrValue::from(value.to_string())),
            )
        })))
    }
}

impl<const N: usize> IntoPropValue<Attributes> for [(&str, &str); N] {
    fn into_prop_value(self) -> Attributes {
        Attributes(HashMap::from_iter(self.map(|(key, value)| {
            (
                AttrValue::from(key.to_string()),
                Some(AttrValue::from(value.to_string())),
            )
        })))
    }
}

impl<const N: usize> IntoPropValue<Attributes> for [(String, Option<String>); N] {
    fn into_prop_value(self) -> Attributes {
        Attributes(HashMap::from_iter(self.map(|(key, value)| {
            (AttrValue::from(key), value.map(AttrValue::from))
        })))
    }
}

impl<const N: usize> IntoPropValue<Attributes> for [(String, String); N] {
    fn into_prop_value(self) -> Attributes {
        Attributes(HashMap::from_iter(self.map(|(key, value)| {
            (AttrValue::from(key), Some(AttrValue::from(value)))
        })))
    }
}