forked from Azure/iotedge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.rs
320 lines (264 loc) · 7.78 KB
/
model.rs
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) Microsoft. All rights reserved.
use std::default::Default;
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum AuthType {
None,
Sas,
X509,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Twin {
device_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
module_id: Option<String>,
version: i32,
authentication_type: AuthType,
properties: Properties,
}
impl Twin {
pub fn new(
device_id: &str,
version: i32,
authentication_type: AuthType,
properties: Properties,
) -> Self {
Twin {
device_id: device_id.to_string(),
module_id: None,
version,
authentication_type,
properties,
}
}
pub fn with_device_id(mut self, device_id: String) -> Self {
self.device_id = device_id;
self
}
pub fn with_module_id(mut self, module_id: String) -> Self {
self.module_id = Some(module_id);
self
}
pub fn with_version(mut self, version: i32) -> Self {
self.version = version;
self
}
pub fn with_authentication_type(mut self, authentication_type: AuthType) -> Self {
self.authentication_type = authentication_type;
self
}
pub fn with_properties(mut self, properties: Properties) -> Self {
self.properties = properties;
self
}
pub fn device_id(&self) -> &str {
&self.device_id
}
pub fn module_id(&self) -> Option<&str> {
self.module_id.as_ref().map(AsRef::as_ref)
}
pub fn version(&self) -> &i32 {
&self.version
}
pub fn authentication_type(&self) -> &AuthType {
&self.authentication_type
}
pub fn properties(&self) -> &Properties {
&self.properties
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Properties {
desired: Value,
}
impl Properties {
pub fn new(desired: Value) -> Properties {
Properties { desired }
}
pub fn desired(&self) -> &Value {
&self.desired
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct X509Thumbprint {
#[serde(skip_serializing_if = "Option::is_none")]
primary_thumbprint: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
secondary_thumbprint: Option<String>,
}
impl X509Thumbprint {
pub fn new() -> Self {
X509Thumbprint {
primary_thumbprint: None,
secondary_thumbprint: None,
}
}
pub fn with_primary_thumbprint(mut self, primary_thumbprint: String) -> Self {
self.primary_thumbprint = Some(primary_thumbprint);
self
}
pub fn primary_thumbprint(&self) -> Option<&str> {
self.primary_thumbprint.as_ref().map(AsRef::as_ref)
}
pub fn with_secondary_thumbprint(mut self, secondary_thumbprint: String) -> Self {
self.secondary_thumbprint = Some(secondary_thumbprint);
self
}
pub fn secondary_thumbprint(&self) -> Option<&str> {
self.secondary_thumbprint.as_ref().map(AsRef::as_ref)
}
}
impl Default for X509Thumbprint {
fn default() -> Self {
X509Thumbprint::new()
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SymmetricKey {
#[serde(skip_serializing_if = "Option::is_none")]
primary_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
secondary_key: Option<String>,
}
impl SymmetricKey {
pub fn new() -> Self {
SymmetricKey {
primary_key: None,
secondary_key: None,
}
}
pub fn with_primary_key(mut self, primary_key: String) -> Self {
self.primary_key = Some(primary_key);
self
}
pub fn primary_key(&self) -> Option<&str> {
self.primary_key.as_ref().map(AsRef::as_ref)
}
pub fn with_secondary_key(mut self, secondary_key: String) -> Self {
self.secondary_key = Some(secondary_key);
self
}
pub fn secondary_key(&self) -> Option<&str> {
self.secondary_key.as_ref().map(AsRef::as_ref)
}
}
impl Default for SymmetricKey {
fn default() -> Self {
SymmetricKey::new()
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AuthMechanism {
#[serde(skip_serializing_if = "Option::is_none")]
symmetric_key: Option<SymmetricKey>,
#[serde(skip_serializing_if = "Option::is_none")]
x509_thumbprint: Option<X509Thumbprint>,
#[serde(skip_serializing_if = "Option::is_none")]
type_: Option<AuthType>,
}
impl AuthMechanism {
pub fn new() -> Self {
AuthMechanism {
symmetric_key: None,
x509_thumbprint: None,
type_: None,
}
}
pub fn with_symmetric_key(mut self, symmetric_key: SymmetricKey) -> Self {
self.symmetric_key = Some(symmetric_key);
self
}
pub fn symmetric_key(&self) -> Option<&SymmetricKey> {
self.symmetric_key.as_ref()
}
pub fn with_x509_thumbprint(mut self, x509_thumbprint: X509Thumbprint) -> Self {
self.x509_thumbprint = Some(x509_thumbprint);
self
}
pub fn x509_thumbprint(&self) -> Option<&X509Thumbprint> {
self.x509_thumbprint.as_ref()
}
pub fn with_type(mut self, type_: AuthType) -> Self {
self.type_ = Some(type_);
self
}
pub fn type_(&self) -> Option<AuthType> {
self.type_
}
}
impl Default for AuthMechanism {
fn default() -> Self {
AuthMechanism::new()
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Module {
#[serde(skip_serializing_if = "Option::is_none")]
module_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
managed_by: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
device_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
generation_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
authentication: Option<AuthMechanism>,
}
impl Module {
pub fn new() -> Self {
Module {
module_id: None,
managed_by: None,
device_id: None,
generation_id: None,
authentication: None,
}
}
pub fn with_module_id(mut self, module_id: String) -> Self {
self.module_id = Some(module_id);
self
}
pub fn module_id(&self) -> Option<&str> {
self.module_id.as_ref().map(AsRef::as_ref)
}
pub fn with_managed_by(mut self, managed_by: String) -> Self {
self.managed_by = Some(managed_by);
self
}
pub fn managed_by(&self) -> Option<&str> {
self.managed_by.as_ref().map(AsRef::as_ref)
}
pub fn with_device_id(mut self, device_id: String) -> Self {
self.device_id = Some(device_id);
self
}
pub fn device_id(&self) -> Option<&str> {
self.device_id.as_ref().map(AsRef::as_ref)
}
pub fn with_generation_id(mut self, generation_id: String) -> Self {
self.generation_id = Some(generation_id);
self
}
pub fn generation_id(&self) -> Option<&str> {
self.generation_id.as_ref().map(AsRef::as_ref)
}
pub fn with_authentication(mut self, authentication: AuthMechanism) -> Self {
self.authentication = Some(authentication);
self
}
pub fn authentication(&self) -> Option<&AuthMechanism> {
self.authentication.as_ref()
}
}
impl Default for Module {
fn default() -> Self {
Module::new()
}
}