1use serde::{Deserialize, Serialize};
21
22use crate::event::TimeMs;
23
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
27pub struct RemoteOpened {
28 pub closes_at: TimeMs,
30 pub url: String,
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
36#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
37pub struct RemoteClosed {
38 pub why: RemoteClosing,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
43#[serde(rename_all = "snake_case")]
44#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
45pub enum RemoteClosing {
46 Console,
48 Locked,
50 Expired,
52}
53
54#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
56#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
57pub struct DevicePaired {
58 pub device: String,
60 pub name: String,
62 pub authority: String,
64}
65
66#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
69pub struct DeviceRevoked {
70 pub device: String,
71 pub name: String,
72}
73
74#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
78pub struct RemoteSessionStarted {
79 pub device: String,
80 pub expires: TimeMs,
81}
82
83#[cfg(test)]
84#[allow(clippy::unwrap_used, clippy::expect_used, reason = "test code")]
85mod tests {
86 use super::*;
87 use crate::event::Payload;
88
89 #[test]
92 fn each_remote_door_line_spells_its_keys_once() {
93 let device = "aaaqeayeaudaocajbifqydiob4".to_owned();
94 let cases: [(Payload, &str); 5] = [
95 (
96 Payload::of(&RemoteOpened {
97 closes_at: TimeMs::new(9),
98 url: "https://city.example".to_owned(),
99 })
100 .unwrap(),
101 "{\"closes_at\":9,\"url\":\"https://city.example\"}",
102 ),
103 (
104 Payload::of(&RemoteClosed {
105 why: RemoteClosing::Locked,
106 })
107 .unwrap(),
108 "{\"why\":\"locked\"}",
109 ),
110 (
111 Payload::of(&DevicePaired {
112 device: device.clone(),
113 name: "phone".to_owned(),
114 authority: "watch".to_owned(),
115 })
116 .unwrap(),
117 "{\"authority\":\"watch\",\"device\":\"aaaqeayeaudaocajbifqydiob4\",\"name\":\"phone\"}",
118 ),
119 (
120 Payload::of(&DeviceRevoked {
121 device: device.clone(),
122 name: "phone".to_owned(),
123 })
124 .unwrap(),
125 "{\"device\":\"aaaqeayeaudaocajbifqydiob4\",\"name\":\"phone\"}",
126 ),
127 (
128 Payload::of(&RemoteSessionStarted {
129 device,
130 expires: TimeMs::new(7),
131 })
132 .unwrap(),
133 "{\"device\":\"aaaqeayeaudaocajbifqydiob4\",\"expires\":7}",
134 ),
135 ];
136 for (payload, wire) in cases {
137 assert_eq!(serde_json::to_string(&payload).unwrap(), wire);
138 let back: Payload = serde_json::from_str(wire).unwrap();
139 assert_eq!(back, payload);
140 }
141 }
142}