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
pub mod capabilities;
pub mod config;
pub mod group;
pub mod light;
pub mod resourcelink;
pub mod rule;
pub mod scene;
pub mod schedule;
pub mod sensor;
pub use capabilities::Capabilities;
pub use config::Config;
pub use group::Group;
pub use light::Light;
pub use resourcelink::Resourcelink;
pub use rule::Rule;
pub use scene::Scene;
pub use schedule::Schedule;
pub use sensor::Sensor;
use serde::{de, de::Error as DeError, Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::{collections::HashMap, fmt};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Alert {
Select,
LSelect,
None,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Effect {
Colorloop,
None,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize)]
pub enum ColorMode {
#[serde(rename = "ct")]
ColorTemperature,
#[serde(rename = "hs")]
HueAndSaturation,
#[serde(rename = "xy")]
ColorSpaceCoordinates,
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Action {
pub address: String,
#[serde(rename = "method")]
pub request_type: ActionRequestType,
pub body: HashMap<String, JsonValue>,
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum ActionRequestType {
Put,
Post,
Delete,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Scan {
pub last_scan: LastScan,
pub resources: Vec<ScanResource>,
}
impl<'de> Deserialize<'de> for Scan {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
enum Field {
LastScan,
ResourceId(String),
}
impl<'de> Deserialize<'de> for Field {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value: String = Deserialize::deserialize(deserializer)?;
Ok(match value.as_ref() {
"lastscan" => Field::LastScan,
v => Field::ResourceId(v.to_owned()),
})
}
}
struct ScanVisitor;
impl<'de> de::Visitor<'de> for ScanVisitor {
type Value = Scan;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("struct Scan")
}
fn visit_map<V: de::MapAccess<'de>>(self, mut map: V) -> Result<Scan, V::Error> {
let mut resources = Vec::new();
let mut last_scan = None;
while let Some(key) = map.next_key()? {
match key {
Field::LastScan => {
last_scan = serde_json::from_value(map.next_value()?)
.map_err(V::Error::custom)?
}
Field::ResourceId(v) => {
let resource = ScanResource {
id: v,
name: map.next_value()?,
};
resources.push(resource);
}
}
}
let last_scan = last_scan.ok_or_else(|| de::Error::missing_field("lastscan"))?;
Ok(Scan {
resources,
last_scan,
})
}
}
const FIELDS: &[&str] = &["lastscan", "resources"];
deserializer.deserialize_struct("Scan", FIELDS, ScanVisitor)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LastScan {
DateTime(chrono::NaiveDateTime),
Active,
None,
}
impl<'de> Deserialize<'de> for LastScan {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value: String = Deserialize::deserialize(deserializer)?;
Ok(match value.as_ref() {
"active" => LastScan::Active,
"none" => LastScan::None,
v => LastScan::DateTime(
chrono::NaiveDateTime::parse_from_str(v, "%Y-%m-%dT%H:%M:%S")
.map_err(D::Error::custom)?,
),
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ScanResource {
pub id: String,
pub name: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ModifierType {
Override,
Increment,
Decrement,
}
pub trait Resource {}
pub trait Modifier: Default + PartialEq {
fn new() -> Self {
Default::default()
}
fn is_empty(&self) -> bool {
self == &Default::default()
}
}
pub trait Creator {}