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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use crate::{
    Callback,
    Value,
};
use std::{
    fmt,
    fmt::Write,
};

/// These are the attributes of an element
#[derive(Debug, Clone, PartialEq)]
pub struct Attribute<ATT, EVENT, MSG>
where
    ATT: Clone,
{
    /// the attribute name
    pub name: ATT,
    /// the attribute value, which could be a simple value, and event or a function call
    pub value: AttribValue<EVENT, MSG>,
    /// namespace of an attribute.
    /// This is specifically used by svg attributes
    /// such as xlink-href
    pub namespace: Option<&'static str>,
}

impl<ATT, EVENT, MSG> Attribute<ATT, EVENT, MSG>
where
    ATT: Clone,
{
    /// create an attribute from Callback
    pub fn from_callback(name: ATT, cb: Callback<EVENT, MSG>) -> Self {
        Attribute {
            name,
            value: cb.into(),
            namespace: None,
        }
    }

    /// create an attribute from Value type
    pub fn from_value(name: ATT, value: Value) -> Self {
        Attribute {
            name,
            value: value.into(),
            namespace: None,
        }
    }
}

/// The value of this attribute with 3 variants
#[derive(Debug, Clone, PartialEq)]
pub enum AttribValue<EVENT, MSG> {
    /// normal attribute value
    Value(Value),
    /// function call such as value, checked, innerHTML
    FuncCall(Value),
    /// callback such as used in oninput, onclick
    Callback(Callback<EVENT, MSG>),
}

impl<ATT, EVENT, MSG> Attribute<ATT, EVENT, MSG>
where
    MSG: 'static,
    EVENT: 'static,
    ATT: PartialEq + Ord + ToString + Clone,
{
    /// map/transform the callback of this attribute where MSG becomes MSG2
    pub(super) fn map_callback<MSG2>(
        self,
        cb: Callback<MSG, MSG2>,
    ) -> Attribute<ATT, EVENT, MSG2>
    where
        MSG2: 'static,
    {
        Attribute {
            name: self.name,
            value: self.value.map_callback(cb),
            namespace: self.namespace,
        }
    }

    /// check whether this attribute is an event listener
    /// TODO: rename this to is_event_listener
    pub fn is_event(&self) -> bool {
        self.value.is_event()
    }

    /// check whether this attribute is a value
    pub fn is_value(&self) -> bool {
        self.value.is_value()
    }

    /// check whether this attribute is a func call value
    /// such as `inner_html` etc.
    pub fn is_func_call(&self) -> bool {
        self.value.is_func_call()
    }

    /// transform the callback of this attribute where EVENT becomes EVENT2
    pub fn reform<F, EVENT2>(self, func: F) -> Attribute<ATT, EVENT2, MSG>
    where
        F: Fn(EVENT2) -> EVENT + 'static,
        EVENT2: 'static,
    {
        Attribute {
            name: self.name,
            value: self.value.reform(func),
            namespace: self.namespace,
        }
    }

    /// returns a reference to the value of this attribute
    pub fn get_value(&self) -> Option<&Value> {
        self.value.get_value()
    }

    /// returns the reference to the callback of this attribute
    pub fn get_callback(&self) -> Option<&Callback<EVENT, MSG>> {
        self.value.get_callback()
    }

    /// consume the attribute and take the callback
    pub fn take_callback(self) -> Option<Callback<EVENT, MSG>> {
        self.value.take_callback()
    }

    /// create a nice string representation of this attribute
    pub fn render(&self, buffer: &mut dyn Write) -> fmt::Result
    where
        ATT: ToString,
    {
        if self.is_value() {
            if let Some(_ns) = self.namespace {
                //TODO: the xlink part of this namespace should be passed by the calling function
                write!(
                    buffer,
                    r#"xlink:{}="{}""#,
                    self.name.to_string(),
                    self.value
                )?;
            } else {
                write!(
                    buffer,
                    r#"{}="{}""#,
                    self.name.to_string(),
                    self.value
                )?;
            }
        }
        Ok(())
    }
}

impl<EVENT, MSG> AttribValue<EVENT, MSG>
where
    MSG: 'static,
    EVENT: 'static,
{
    fn map_callback<MSG2>(
        self,
        cb: Callback<MSG, MSG2>,
    ) -> AttribValue<EVENT, MSG2>
    where
        MSG2: 'static,
    {
        match self {
            AttribValue::Value(value) => AttribValue::Value(value),
            AttribValue::FuncCall(value) => AttribValue::FuncCall(value),
            AttribValue::Callback(existing) => {
                AttribValue::Callback(existing.map_callback(cb))
            }
        }
    }

    fn reform<F, EVENT2>(self, func: F) -> AttribValue<EVENT2, MSG>
    where
        F: Fn(EVENT2) -> EVENT + 'static,
        EVENT2: 'static,
    {
        match self {
            AttribValue::Value(value) => AttribValue::Value(value),
            AttribValue::FuncCall(value) => AttribValue::FuncCall(value),
            AttribValue::Callback(cb) => AttribValue::Callback(cb.reform(func)),
        }
    }

    fn is_value(&self) -> bool {
        match self {
            AttribValue::Value(_) => true,
            _ => false,
        }
    }

    fn is_event(&self) -> bool {
        match self {
            AttribValue::Callback(_) => true,
            _ => false,
        }
    }

    fn is_func_call(&self) -> bool {
        match self {
            AttribValue::FuncCall(_) => true,
            _ => false,
        }
    }

    pub(crate) fn get_callback(&self) -> Option<&Callback<EVENT, MSG>> {
        match self {
            AttribValue::Value(_) => None,
            AttribValue::FuncCall(_) => None,
            AttribValue::Callback(cb) => Some(cb),
        }
    }

    pub(crate) fn take_callback(self) -> Option<Callback<EVENT, MSG>> {
        match self {
            AttribValue::Value(_) => None,
            AttribValue::FuncCall(_) => None,
            AttribValue::Callback(cb) => Some(cb),
        }
    }

    /// returh the value if it is a Value variant
    pub fn get_value(&self) -> Option<&Value> {
        match self {
            AttribValue::Value(value) => Some(value),
            AttribValue::FuncCall(value) => Some(value),
            AttribValue::Callback(_) => None,
        }
    }
}

impl<EVENT, MSG> fmt::Display for AttribValue<EVENT, MSG> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            AttribValue::Value(value) => write!(f, "{}", value),
            AttribValue::FuncCall(_) => write!(f, ""),
            AttribValue::Callback(_) => write!(f, ""),
        }
    }
}

impl<EVENT, MSG> From<Callback<EVENT, MSG>> for AttribValue<EVENT, MSG> {
    fn from(cb: Callback<EVENT, MSG>) -> Self {
        AttribValue::Callback(cb)
    }
}

impl<EVENT, MSG> From<Value> for AttribValue<EVENT, MSG> {
    fn from(value: Value) -> Self {
        AttribValue::Value(value)
    }
}