Skip to main content

px_native/events/
model.rs

1//! Data model for a single sensor event. The runtime emits arrays of
2//! these and the encryptor JSON-stringifies the array before XOR.
3//!
4//! Field values are deliberately constrained to the JSON value kinds
5//! we have actually observed inside `o.d[…]` assignments in the
6//! deobfuscated init.js (line 7706-7738): strings, integers, booleans,
7//! the JSON `null`, and one level of nested objects.
8
9use std::collections::BTreeMap;
10
11use serde::Serialize;
12
13#[derive(Debug, Clone, PartialEq, Serialize)]
14#[serde(untagged)]
15pub enum EventField {
16    Null,
17    Bool(bool),
18    Int(i64),
19    String(String),
20    Object(BTreeMap<String, EventField>),
21}
22
23impl From<bool> for EventField {
24    fn from(b: bool) -> Self {
25        EventField::Bool(b)
26    }
27}
28
29impl From<i64> for EventField {
30    fn from(n: i64) -> Self {
31        EventField::Int(n)
32    }
33}
34
35impl From<u64> for EventField {
36    fn from(n: u64) -> Self {
37        EventField::Int(n as i64)
38    }
39}
40
41impl From<&str> for EventField {
42    fn from(s: &str) -> Self {
43        EventField::String(s.to_owned())
44    }
45}
46
47impl From<String> for EventField {
48    fn from(s: String) -> Self {
49        EventField::String(s)
50    }
51}
52
53#[derive(Debug, Clone, PartialEq, Serialize)]
54pub struct SensorEvent {
55    pub t: String,
56    pub d: BTreeMap<String, EventField>,
57}
58
59impl SensorEvent {
60    pub fn new(tag: impl Into<String>) -> Self {
61        Self {
62            t: tag.into(),
63            d: BTreeMap::new(),
64        }
65    }
66
67    pub fn with(mut self, key: impl Into<String>, value: impl Into<EventField>) -> Self {
68        self.d.insert(key.into(), value.into());
69        self
70    }
71}