1use serde::{Deserialize, Serialize};
4
5#[derive(Eq, PartialEq, Hash, Debug, Clone, Copy, Deserialize)]
7pub enum EventKind {
8 #[serde(rename = "incoming")]
10 IncomingMessage,
11
12 #[serde(rename = "outgoing")]
14 OutgoingMessage,
15
16 #[serde(rename = "delivery")]
18 DeliveryReport,
19
20 #[serde(rename = "modem_status_update")]
22 ModemStatusUpdate,
23
24 #[serde(rename = "gnss_position_report")]
26 GNSSPositionReport,
27
28 #[serde(rename = "websocket_connection_update")]
30 WebsocketConnectionUpdate,
31}
32impl EventKind {
33 pub const COUNT: usize = 6;
35
36 #[inline]
38 #[must_use]
39 pub const fn to_bit(self) -> u8 {
40 match self {
41 EventKind::IncomingMessage => 1 << 0,
42 EventKind::OutgoingMessage => 1 << 1,
43 EventKind::DeliveryReport => 1 << 2,
44 EventKind::ModemStatusUpdate => 1 << 3,
45 EventKind::GNSSPositionReport => 1 << 4,
46 EventKind::WebsocketConnectionUpdate => 1 << 5,
47 }
48 }
49
50 #[inline]
52 #[must_use]
53 pub const fn all_bits() -> u8 {
54 (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)
55 }
56
57 #[inline]
59 #[must_use]
60 pub fn events_to_mask(events: &[EventKind]) -> u8 {
61 events.iter().fold(0, |acc, event| acc | event.to_bit())
62 }
63}
64impl From<&Event> for EventKind {
65 fn from(value: &Event) -> Self {
66 match value {
67 Event::IncomingMessage(_) => EventKind::IncomingMessage,
68 Event::OutgoingMessage(_) => EventKind::OutgoingMessage,
69 Event::DeliveryReport { .. } => EventKind::DeliveryReport,
70 Event::ModemStatusUpdate { .. } => EventKind::ModemStatusUpdate,
71 Event::WebsocketConnectionUpdate { .. } => EventKind::WebsocketConnectionUpdate,
72
73 #[cfg(feature = "gnss")]
74 Event::GnssPositionReport(_) => EventKind::GNSSPositionReport,
75 }
76 }
77}
78impl TryFrom<&str> for EventKind {
79 type Error = String;
80
81 #[inline]
83 fn try_from(value: &str) -> Result<Self, Self::Error> {
84 match value {
85 "incoming" => Ok(EventKind::IncomingMessage),
86 "outgoing" => Ok(EventKind::OutgoingMessage),
87 "delivery" => Ok(EventKind::DeliveryReport),
88 "modem_status_update" => Ok(EventKind::ModemStatusUpdate),
89 "websocket_connection_upgrade" => Ok(EventKind::WebsocketConnectionUpdate),
90 "gnss_position_report" => Ok(EventKind::GNSSPositionReport),
91 _ => Err(format!("Unknown event type {value}")),
92 }
93 }
94}
95
96#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
98#[serde(tag = "type", content = "data")]
99pub enum Event {
100 #[serde(rename = "incoming")]
102 IncomingMessage(crate::sms::SmsMessage),
103
104 #[serde(rename = "outgoing")]
106 OutgoingMessage(crate::sms::SmsMessage),
107
108 #[serde(rename = "delivery")]
110 DeliveryReport {
111 message_id: i64,
114
115 report: crate::sms::SmsPartialDeliveryReport,
117 },
118
119 #[serde(rename = "modem_status_update")]
122 ModemStatusUpdate {
123 previous: crate::modem::ModemStatusUpdateState,
125
126 current: crate::modem::ModemStatusUpdateState,
128 },
129
130 WebsocketConnectionUpdate {
133 connected: bool,
135
136 reconnect: bool,
138 },
139
140 #[cfg(feature = "gnss")]
142 #[serde(rename = "gnss_position_report")]
143 GnssPositionReport(crate::gnss::PositionReport),
144}