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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use std::cmp::{PartialOrd, Ordering};
use std::time::Duration;
use std::str::FromStr;
use std::sync::Arc;
use serde_json;
use chrono;
use serde::ser::{Serialize, Serializer};
use serde::de::{Deserialize, Deserializer, Error};
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize)]
pub enum Type {
Unit,
Bool,
Duration,
TimeStamp,
Temperature,
String,
Color,
Json,
Binary,
ExtNumeric,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Temperature {
F(f64),
C(f64),
}
impl Temperature {
pub fn as_f(&self) -> f64 {
unimplemented!();
}
pub fn as_c(&self) -> f64 {
unimplemented!();
}
}
impl PartialOrd for Temperature {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.as_c().partial_cmp(&other.as_c())
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum Color {
RGBA(f64, f64, f64, f64, f64)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Json(pub serde_json::value::Value);
impl PartialOrd for Json {
fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
None
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExtNumeric {
pub value: f64,
pub vendor: String,
pub adapter: String,
pub kind: String,
}
impl PartialOrd for ExtNumeric {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.vendor != other.vendor {
return None;
} else if self.kind != other.kind {
return None;
} else {
self.value.partial_cmp(&other.value)
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
Unit,
Bool(bool),
Duration(ValDuration),
TimeStamp(TimeStamp),
Temperature(Temperature),
Color(Color),
String(Arc<String>),
ExtNumeric(ExtNumeric),
Json(Arc<Json>),
Binary {
data: Arc<Vec<u8>>,
mimetype: String
}
}
impl Value {
pub fn get_type(&self) -> Type {
match *self {
Value::Unit => Type::Unit,
Value::Bool(_) => Type::Bool,
Value::String(_) => Type::String,
Value::Duration(_) => Type::Duration,
Value::TimeStamp(_) => Type::TimeStamp,
Value::Temperature(_) => Type::Temperature,
Value::Color(_) => Type::Color,
Value::Json(_) => Type::Json,
Value::Binary{..} => Type::Binary,
Value::ExtNumeric(_) => Type::ExtNumeric,
}
}
}
impl PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
use self::Value::*;
use std::cmp::Ordering::*;
match (self, other) {
(&Unit, &Unit) => Some(Equal),
(&Unit, _) => None,
(&Bool(a), &Bool(b)) => a.partial_cmp(&b),
(&Bool(_), _) => None,
(&Duration(ref a), &Duration(ref b)) => a.partial_cmp(b),
(&Duration(_), _) => None,
(&TimeStamp(ref a), &TimeStamp(ref b)) => a.partial_cmp(b),
(&TimeStamp(_), _) => None,
(&Temperature(ref a), &Temperature(ref b)) => a.partial_cmp(b),
(&Temperature(_), _) => None,
(&Color(ref a), &Color(ref b)) => a.partial_cmp(b),
(&Color(_), _) => None,
(&ExtNumeric(ref a), &ExtNumeric(ref b)) => a.partial_cmp(b),
(&ExtNumeric(_), _) => None,
(&String(ref a), &String(ref b)) => a.partial_cmp(b),
(&String(_), _) => None,
(&Json(ref a), &Json(ref b)) => a.partial_cmp(b),
(&Json(_), _) => None,
(&Binary{mimetype: ref a_mimetype, data: ref a_data},
&Binary{mimetype: ref b_mimetype, data: ref b_data}) if a_mimetype == b_mimetype => a_data.partial_cmp(b_data),
(&Binary{..}, _) => None,
}
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct ValDuration(Duration);
impl ValDuration {
pub fn new(duration: Duration) -> Self {
ValDuration(duration)
}
}
impl Serialize for ValDuration {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {
let as_ms : u64 = self.0.as_secs() * 1000
+ (self.0.subsec_nanos() as u64) / 1_000_000;
as_ms.serialize(serializer)
}
}
impl Deserialize for ValDuration {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer {
let as_sec : f64 = try!(f64::deserialize(deserializer));
Ok(ValDuration(Duration::new(as_sec as u64, as_sec.fract() as u32)))
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct TimeStamp(chrono::DateTime<chrono::Local>);
impl TimeStamp {
pub fn from_s(s: i64) -> Self {
use chrono::*;
let naive = chrono::naive::datetime::NaiveDateTime::from_timestamp(s, 0);
let offset = chrono::offset::fixed::FixedOffset::east(0);
let date = DateTime::<Local>::from_utc(naive, offset);
TimeStamp(date)
}
}
impl Serialize for TimeStamp {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {
let str = self.0.to_rfc3339();
str.serialize(serializer)
}
}
impl Deserialize for TimeStamp {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer {
let str = try!(String::deserialize(deserializer));
match chrono::DateTime::<chrono::Local>::from_str(&str) {
Ok(dt) => Ok(TimeStamp(dt)),
Err(_) => Err(D::Error::syntax("Invalid date"))
}
}
}
#[derive(Clone, Deserialize, Serialize)]
pub enum Range {
Leq(Value),
Geq(Value),
BetweenEq {min:Value, max:Value},
OutOfStrict {min:Value, max:Value},
Eq(Value),
}
impl Range {
pub fn contains(&self, value: &Value) -> bool {
use self::Range::*;
match *self {
Leq(ref max) => value <= max,
Geq(ref min) => value >= min,
BetweenEq {ref min, ref max} => min <= value && value <= max,
OutOfStrict {ref min, ref max} => value < min || max < value,
Eq(ref val) => value == val,
}
}
pub fn get_type(&self) -> Result<Type, ()> {
use self::Range::*;
match *self {
Leq(ref v) | Geq(ref v) | Eq(ref v) => Ok(v.get_type()),
BetweenEq{ref min, ref max} | OutOfStrict{ref min, ref max} => {
let min_typ = min.get_type();
let max_typ = max.get_type();
if min_typ == max_typ {
Ok(min_typ)
} else {
Err(())
}
}
}
}
}