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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::{
    html::attributes::{Style, Value},
    vdom::Listener,
};
use std::fmt::{self, Debug};

/// Values of an attribute can be in these variants
pub enum AttributeValue<MSG> {
    /// an argument value, to be called as parameter, the function is called to the element
    FunctionCall(Value),
    /// a simple value, wrapper of primitive types
    Simple(Value),
    /// style values
    Style(Vec<Style>),
    /// Event Listener
    EventListener(Listener<MSG>),
    /// no value
    Empty,
}

/// This is written manually, so we don't push
/// constraint on MSG to be Clone
impl<MSG> Clone for AttributeValue<MSG> {
    fn clone(&self) -> Self {
        match self {
            AttributeValue::FunctionCall(this) => AttributeValue::FunctionCall(this.clone()),
            AttributeValue::Simple(this) => AttributeValue::Simple(this.clone()),
            AttributeValue::Style(this) => AttributeValue::Style(this.clone()),
            AttributeValue::EventListener(this) => AttributeValue::EventListener(this.clone()),
            AttributeValue::Empty => AttributeValue::Empty,
        }
    }
}

/// This is written manually, so we don't push
/// constraint on MSG to be Debug
impl<MSG> Debug for AttributeValue<MSG> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            AttributeValue::FunctionCall(this) => this.fmt(f),
            AttributeValue::Simple(this) => this.fmt(f),
            AttributeValue::Style(this) => this.fmt(f),
            AttributeValue::EventListener(this) => this.fmt(f),
            AttributeValue::Empty => write!(f, "Empty"),
        }
    }
}

/// This is written manually, so we don't push
/// constraint on MSG to be PartialEq
impl<MSG> PartialEq for AttributeValue<MSG> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (AttributeValue::FunctionCall(this), AttributeValue::FunctionCall(other)) => {
                this == other
            }
            (AttributeValue::Simple(this), AttributeValue::Simple(other)) => this == other,
            (AttributeValue::Style(this), AttributeValue::Style(other)) => this == other,
            (AttributeValue::EventListener(this), AttributeValue::EventListener(other)) => {
                this == other
            }
            (AttributeValue::Empty, AttributeValue::Empty) => true,
            (_, _) => false,
        }
    }
}

impl<MSG> From<Listener<MSG>> for AttributeValue<MSG> {
    fn from(listener: Listener<MSG>) -> Self {
        Self::EventListener(listener)
    }
}

impl<MSG, V> From<V> for AttributeValue<MSG>
where
    V: Into<Value>,
{
    fn from(v: V) -> Self {
        Self::Simple(Into::<Value>::into(v))
    }
}

impl<MSG> AttributeValue<MSG> {
    /// create an attribute from Vec<Style>
    pub fn from_styles(styles: impl IntoIterator<Item = Style>) -> Self {
        Self::Style(styles.into_iter().collect())
    }

    /// create an attribute from a function `name` with arguments `value`
    pub fn function_call(value: Value) -> Self {
        Self::FunctionCall(value)
    }

    /// return the value if it is a Simple variant
    pub fn get_simple(&self) -> Option<&Value> {
        match self {
            Self::Simple(v) => Some(v),
            _ => None,
        }
    }

    /// return the function call argument value if it is a FunctionCall variant
    pub fn get_function_call_value(&self) -> Option<&Value> {
        match self {
            Self::FunctionCall(v) => Some(v),
            _ => None,
        }
    }

    /// returns true if this attribute value is a style
    pub fn is_style(&self) -> bool {
        matches!(self, Self::Style(_))
    }

    /// return the styles if the attribute value is a style
    pub fn as_event_listener(&self) -> Option<&Listener<MSG>> {
        match self {
            Self::EventListener(cb) => Some(cb),
            _ => None,
        }
    }

    /// return the styles if the attribute value is a style
    pub fn as_style(&self) -> Option<&Vec<Style>> {
        match self {
            Self::Style(styles) => Some(styles),
            _ => None,
        }
    }

    /// return true if this is a function call
    pub fn is_function_call(&self) -> bool {
        matches!(self, Self::FunctionCall(_))
    }

    /// returns true if this attribute value is empty
    pub fn is_empty(&self) -> bool {
        matches!(self, Self::Empty)
    }
}