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
106
107
108
109
110
111
112
113
//! Leaf node for html dom tree
use crate::vdom::Node;
use std::fmt;

/// A leaf node value of html dom tree
pub enum Leaf<MSG> {
    /// Text variant of a virtual node
    Text(String),
    /// A safe html variant
    SafeHtml(String),
    /// A comment node
    Comment(String),
    /// a vec of nodes
    Fragment(Vec<Node<MSG>>),
    /// doctype: html, math, svg
    /// <https://www.w3.org/QA/2002/04/valid-dtd-list.html>
    DocType(String),
}

impl<MSG> Leaf<MSG> {
    /// returns true if this a text node
    pub fn is_text(&self) -> bool {
        matches!(self, Self::Text(_))
    }

    /// returns true if this is a safe html text node
    pub fn is_safe_html(&self) -> bool {
        matches!(self, Self::SafeHtml(_))
    }

    /// unwrap the text content if this a text node,
    /// panics if it is not a text node
    pub fn unwrap_text(&self) -> &str {
        match self {
            Self::Text(ref text) => text,
            _ => panic!("node is not a text"),
        }
    }

    /// unwrap the text content if this a text node,
    /// panics if it is not a text node
    pub fn unwrap_safe_html(&self) -> &str {
        match self {
            Self::SafeHtml(ref html) => html,
            _ => panic!("node is not a text"),
        }
    }
}

impl<MSG> fmt::Debug for Leaf<MSG> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Text(v) => write!(f, "Text({v})"),
            Self::SafeHtml(v) => write!(f, "SafeHtml({v})"),
            Self::Comment(v) => write!(f, "Comment({v})"),
            Self::Fragment(v) => {
                write!(f, "Fragment:")?;
                f.debug_list().entries(v).finish()
            }
            Self::DocType(v) => write!(f, "DocType({v})"),
        }
    }
}

impl<MSG> Clone for Leaf<MSG> {
    fn clone(&self) -> Self {
        match self {
            Self::Text(v) => Self::Text(v.clone()),
            Self::SafeHtml(v) => Self::SafeHtml(v.clone()),
            Self::Comment(v) => Self::Comment(v.clone()),
            Self::Fragment(v) => Self::Fragment(v.clone()),
            Self::DocType(v) => Self::DocType(v.clone()),
        }
    }
}

impl<MSG> PartialEq for Leaf<MSG> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Text(v), Self::Text(o)) => v == o,
            (Self::SafeHtml(v), Self::SafeHtml(o)) => v == o,
            (Self::Comment(v), Self::Comment(o)) => v == o,
            (Self::Fragment(v), Self::Fragment(o)) => v == o,
            (Self::DocType(v), Self::DocType(o)) => v == o,
            _ => false,
        }
    }
}

/// create a text leaf
pub fn text<MSG>(s: impl ToString) -> Leaf<MSG> {
    Leaf::Text(s.to_string())
}

/// create a safe html leaf
pub fn safe_html<MSG>(s: impl ToString) -> Leaf<MSG> {
    Leaf::SafeHtml(s.to_string())
}

/// create a comment leaf
pub fn comment<MSG>(s: impl ToString) -> Leaf<MSG> {
    Leaf::Comment(s.to_string())
}

/// create a fragment leaf
pub fn fragment<MSG>(nodes: impl IntoIterator<Item = Node<MSG>>) -> Leaf<MSG> {
    Leaf::Fragment(nodes.into_iter().collect())
}

/// create a doctype leaf
pub fn doctype<MSG>(s: impl ToString) -> Leaf<MSG> {
    Leaf::DocType(s.to_string())
}