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
use std::ops::{Deref, DerefMut};
use yew::html::IntoPropValue;
use yew::prelude::*;

/// A wrapper around [`Option<Html>`], making it easier to assign to components.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct OptionalHtml(pub Option<Html>);

impl Deref for OptionalHtml {
    type Target = Option<Html>;

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

impl DerefMut for OptionalHtml {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl ToHtml for OptionalHtml {
    fn to_html(&self) -> Html {
        self.0.to_html()
    }

    fn into_html(self) -> Html
    where
        Self: Sized,
    {
        self.0.into_html()
    }
}

impl From<Html> for OptionalHtml {
    fn from(value: Html) -> Self {
        Self(Some(value))
    }
}

impl From<Option<Html>> for OptionalHtml {
    fn from(value: Option<Html>) -> Self {
        Self(value)
    }
}

impl From<&str> for OptionalHtml {
    fn from(value: &str) -> Self {
        Self(Some(Html::from(value)))
    }
}

impl From<String> for OptionalHtml {
    fn from(value: String) -> Self {
        Self(Some(Html::from(value)))
    }
}

impl IntoPropValue<OptionalHtml> for &str {
    fn into_prop_value(self) -> OptionalHtml {
        self.into()
    }
}

impl IntoPropValue<OptionalHtml> for String {
    fn into_prop_value(self) -> OptionalHtml {
        self.into()
    }
}

impl IntoPropValue<OptionalHtml> for Html {
    fn into_prop_value(self) -> OptionalHtml {
        self.into()
    }
}

impl IntoPropValue<OptionalHtml> for Option<Html> {
    fn into_prop_value(self) -> OptionalHtml {
        self.into()
    }
}

impl<COMP: BaseComponent> IntoPropValue<OptionalHtml> for yew::virtual_dom::VChild<COMP> {
    fn into_prop_value(self) -> OptionalHtml {
        let html: Html = self.into();
        html.into()
    }
}