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
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
/// A normalized browser event delivered to a LiveView.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Event {
/// Logical event name, for example `inc`, `save_profile`, or `validate`.
pub name: String,
/// Optional DOM target id or client-provided target scope.
pub target: Option<String>,
/// Event value. For forms, this is usually an object built from FormData.
pub value: Value,
/// Non-authoritative browser metadata.
///
/// Treat this data as untrusted. Use it for diagnostics or UX hints, not
/// authorization.
#[serde(default)]
pub metadata: Map<String, Value>,
}
impl Event {
/// Create a simple event with `null` value and no metadata.
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
target: None,
value: Value::Null,
metadata: Map::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::Event;
use serde_json::Value;
#[test]
fn event_new_defaults_to_null_value() {
let event = Event::new("inc");
assert_eq!(event.name, "inc");
assert_eq!(event.value, Value::Null);
assert!(event.target.is_none());
assert!(event.metadata.is_empty());
}
}