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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use crate::{Coord, Number, Ref};
use chrono::{DateTime, FixedOffset, NaiveDate, NaiveTime};
use serde_json::Value;

/// An extension trait for the `serde_json::Value` enum,
/// containing helper functions which make it easier to
/// parse specific Haystack types from the underlying JSON
/// value.
pub trait ValueExt {
    /// Convert the JSON value to a Haystack Coord.
    fn as_hs_coord(&self) -> Option<Coord>;
    /// Convert the JSON value to a Haystack Date.
    fn as_hs_date(&self) -> Option<NaiveDate>;
    /// Convert the JSON value to a tuple containing a
    /// DateTime with a fixed timezone offset, and a string
    /// containing the Haystack timezone name.
    fn as_hs_date_time(&self) -> Option<(DateTime<FixedOffset>, &str)>;
    /// Convert the JSON value to a Haystack Number.
    fn as_hs_number(&self) -> Option<Number>;
    /// Convert the JSON value to a Haystack Ref.
    fn as_hs_ref(&self) -> Option<Ref>;
    /// Parse the JSON value as a Haystack Str, removing
    /// the "s:" prefix if necessary.
    fn as_hs_str(&self) -> Option<&str>;
    /// Convert the JSON value to a Haystack Time.
    fn as_hs_time(&self) -> Option<NaiveTime>;
    /// Returns the Haystack URI value as a string.
    fn as_hs_uri(&self) -> Option<&str>;
    /// Return the Haystack XStr value as a string.
    fn as_hs_xstr(&self) -> Option<&str>;
    /// Returns true if the JSON value represents a Haystack
    /// Coord.
    fn is_hs_coord(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Date.
    fn is_hs_date(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// DateTime.
    fn is_hs_date_time(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// marker.
    fn is_hs_marker(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// NA value.
    fn is_hs_na(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Number.
    fn is_hs_number(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Ref.
    fn is_hs_ref(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// remove marker.
    fn is_hs_remove_marker(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Str.
    fn is_hs_str(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Time.
    fn is_hs_time(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// URI.
    fn is_hs_uri(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// XStr.
    fn is_hs_xstr(&self) -> bool;
}

impl ValueExt for Value {
    fn as_hs_coord(&self) -> Option<Coord> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::Coord => {
                let mut split = trim_hs_prefix(s).split(',');
                let lat = split.next().and_then(|s| str::parse(s).ok());
                let lng = split.next().and_then(|s| str::parse(s).ok());
                match (lat, lng) {
                    (Some(lat), Some(lng)) => Some(Coord::new(lat, lng)),
                    _ => None,
                }
            }
            _ => None,
        })
    }

    fn as_hs_date(&self) -> Option<NaiveDate> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::Date => {
                let date_str = trim_hs_prefix(s);
                NaiveDate::parse_from_str(date_str, "%Y-%m-%d").ok()
            }
            _ => None,
        })
    }

    fn as_hs_date_time(&self) -> Option<(DateTime<FixedOffset>, &str)> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::DateTime => {
                let mut split = trim_hs_prefix(s).split(' ');
                let date_time = split
                    .next()
                    .and_then(|s| DateTime::parse_from_rfc3339(s).ok());
                let time_zone_name = split.next();
                let tuple = (date_time, time_zone_name);
                match tuple {
                    (Some(date_time), Some(time_zone_name)) => {
                        Some((date_time, time_zone_name))
                    }
                    _ => None,
                }
            }
            _ => None,
        })
    }

    fn as_hs_number(&self) -> Option<Number> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::Number => {
                Number::from_encoded_json_string(s).ok()
            }
            _ => None,
        })
    }

    fn as_hs_ref(&self) -> Option<Ref> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::Ref => {
                Ref::from_encoded_json_string(s).ok()
            }
            _ => None,
        })
    }

    fn as_hs_str(&self) -> Option<&str> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::PlainString => Some(s),
            JsonStringHaystackType::PrefixedString => Some(trim_hs_prefix(s)),
            _ => None,
        })
    }

    fn as_hs_time(&self) -> Option<NaiveTime> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::Time => {
                let time_str = trim_hs_prefix(s);
                NaiveTime::parse_from_str(time_str, "%k:%M:%S")
                    .ok()
                    .or_else(|| {
                        NaiveTime::parse_from_str(time_str, "%k:%M").ok()
                    })
            }
            _ => None,
        })
    }

    fn as_hs_uri(&self) -> Option<&str> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::Uri => Some(trim_hs_prefix(s)),
            _ => None,
        })
    }

    fn as_hs_xstr(&self) -> Option<&str> {
        self.as_str().and_then(|s| match haystack_type(s) {
            JsonStringHaystackType::XStr => Some(trim_hs_prefix(s)),
            _ => None,
        })
    }

    fn is_hs_coord(&self) -> bool {
        self.as_hs_coord().is_some()
    }

    fn is_hs_date(&self) -> bool {
        self.as_hs_date().is_some()
    }

    fn is_hs_date_time(&self) -> bool {
        self.as_hs_date_time().is_some()
    }

    fn is_hs_marker(&self) -> bool {
        if let Some(s) = self.as_str() {
            match haystack_type(s) {
                JsonStringHaystackType::Marker => true,
                _ => false,
            }
        } else {
            false
        }
    }

    fn is_hs_na(&self) -> bool {
        if let Some(s) = self.as_str() {
            match haystack_type(s) {
                JsonStringHaystackType::Na => true,
                _ => false,
            }
        } else {
            false
        }
    }

    fn is_hs_number(&self) -> bool {
        self.as_hs_number().is_some()
    }

    fn is_hs_ref(&self) -> bool {
        self.as_hs_ref().is_some()
    }

    fn is_hs_remove_marker(&self) -> bool {
        if let Some(s) = self.as_str() {
            match haystack_type(s) {
                JsonStringHaystackType::RemoveMarker => true,
                _ => false,
            }
        } else {
            false
        }
    }

    fn is_hs_str(&self) -> bool {
        self.as_hs_str().is_some()
    }

    fn is_hs_time(&self) -> bool {
        self.as_hs_time().is_some()
    }

    fn is_hs_uri(&self) -> bool {
        self.as_hs_uri().is_some()
    }

    fn is_hs_xstr(&self) -> bool {
        self.as_hs_xstr().is_some()
    }
}

/// Determine the Haystack type of the given string by
/// looking for specific prefix characters.
fn haystack_type(s: &str) -> JsonStringHaystackType {
    if let Some(prefix) = first_two_chars(s) {
        match prefix.as_ref() {
            "m:" => JsonStringHaystackType::Marker,
            "-:" => JsonStringHaystackType::RemoveMarker,
            "z:" => JsonStringHaystackType::Na,
            "n:" => JsonStringHaystackType::Number,
            "r:" => JsonStringHaystackType::Ref,
            "s:" => JsonStringHaystackType::PrefixedString,
            "d:" => JsonStringHaystackType::Date,
            "h:" => JsonStringHaystackType::Time,
            "t:" => JsonStringHaystackType::DateTime,
            "u:" => JsonStringHaystackType::Uri,
            "c:" => JsonStringHaystackType::Coord,
            "x:" => JsonStringHaystackType::XStr,
            _ => JsonStringHaystackType::PlainString,
        }
    } else {
        JsonStringHaystackType::PlainString
    }
}

/// If possible, return the first two characters of the
/// original string. Otherwise, return `None`.
fn first_two_chars(s: &str) -> Option<String> {
    let prefix: String = s.chars().take(2).collect();
    if prefix.chars().count() == 2 {
        Some(prefix)
    } else {
        None
    }
}

/// Return a string slice with the first two characters removed.
fn trim_hs_prefix(s: &str) -> &str {
    // Since we know the first two chars are valid ASCII chars,
    // we can trim the first two bytes and still have a valid
    // UTF8 string:
    &s[2..]
}

/// Haystack types which are represented as JSON strings.
#[derive(Clone, Debug, Eq, PartialEq)]
enum JsonStringHaystackType {
    Marker,
    RemoveMarker,
    Na,
    Number,
    Ref,
    PlainString,
    PrefixedString,
    Date,
    Time,
    DateTime,
    Uri,
    Coord,
    XStr,
}

#[cfg(test)]
mod test {
    use super::ValueExt;
    use serde_json::json;

    #[test]
    fn haystack_type_strings() {
        use super::{haystack_type, JsonStringHaystackType};
        assert_eq!(haystack_type(""), JsonStringHaystackType::PlainString);
        assert_eq!(haystack_type(":"), JsonStringHaystackType::PlainString);
        assert_eq!(haystack_type("5"), JsonStringHaystackType::PlainString);
        assert_eq!(haystack_type("w:"), JsonStringHaystackType::PlainString);
        assert_eq!(haystack_type("hello"), JsonStringHaystackType::PlainString);

        assert_eq!(haystack_type("s:"), JsonStringHaystackType::PrefixedString);
        assert_eq!(
            haystack_type("s:hello"),
            JsonStringHaystackType::PrefixedString
        );
        assert_eq!(
            haystack_type("s:hello world"),
            JsonStringHaystackType::PrefixedString
        );
    }

    #[test]
    fn haystack_type_non_strings() {
        use super::{haystack_type, JsonStringHaystackType};
        assert_eq!(haystack_type("m:"), JsonStringHaystackType::Marker);
        assert_eq!(haystack_type("m:junk"), JsonStringHaystackType::Marker);
        assert_eq!(haystack_type("-:"), JsonStringHaystackType::RemoveMarker);
        assert_eq!(
            haystack_type("-:junk"),
            JsonStringHaystackType::RemoveMarker
        );

        assert_eq!(haystack_type("z:"), JsonStringHaystackType::Na);
        assert_eq!(haystack_type("z: junk"), JsonStringHaystackType::Na);

        assert_eq!(
            haystack_type("n:55 celsius"),
            JsonStringHaystackType::Number
        );
        assert_eq!(
            haystack_type("r:p:proj:r:abcd1234-abcd1234"),
            JsonStringHaystackType::Ref
        );
        assert_eq!(haystack_type("d:2014-01-03"), JsonStringHaystackType::Date);
        assert_eq!(haystack_type("h:23:59"), JsonStringHaystackType::Time);
        assert_eq!(
            haystack_type("t:2015-06-08T15:47:41-04:00 New_York"),
            JsonStringHaystackType::DateTime
        );
        assert_eq!(
            haystack_type("u:http://project-haystack.org/"),
            JsonStringHaystackType::Uri
        );
        assert_eq!(
            haystack_type("c:37.545,-77.449"),
            JsonStringHaystackType::Coord
        );
        assert_eq!(haystack_type("x:Type:value"), JsonStringHaystackType::XStr);
    }

    #[test]
    fn as_hs_str() {
        let plain_val = json!("hello world");
        let prefixed_val = json!("s:hello world");

        assert_eq!(plain_val.as_hs_str(), Some("hello world"));
        assert_eq!(prefixed_val.as_hs_str(), Some("hello world"));
    }

    #[test]
    fn as_hs_ref() {
        let ref_val = json!("r:abc-123");
        let ref_val_and_display_name = json!("r:abc-123 RTU #3");
        assert_eq!(ref_val.as_hs_ref().unwrap().as_ref(), "@abc-123");
        assert_eq!(
            ref_val_and_display_name.as_hs_ref().unwrap().as_ref(),
            "@abc-123"
        );
    }

    #[test]
    fn as_hs_number() {
        use crate::Number;

        let number_val = json!("n:25.123 celsius");
        assert_eq!(
            number_val.as_hs_number().unwrap(),
            Number::new(25.123, Some("celsius".to_owned()))
        );

        let number_no_unit_val = json!("n:25.123");
        assert_eq!(
            number_no_unit_val.as_hs_number().unwrap(),
            Number::new(25.123, None)
        );
    }

    #[test]
    fn as_hs_date() {
        use chrono::NaiveDate;

        let number_val = json!("d:2014-12-01");
        assert_eq!(
            number_val.as_hs_date().unwrap(),
            NaiveDate::from_ymd(2014, 12, 1)
        );
    }

    #[test]
    fn as_hs_time() {
        use chrono::NaiveTime;

        let time_val = json!("h:23:59");
        let time = time_val.as_hs_time().unwrap();
        assert_eq!(time, NaiveTime::from_hms(23, 59, 0));
    }

    #[test]
    fn as_hs_time_with_seconds() {
        use chrono::NaiveTime;

        let time_val = json!("h:23:59:15");
        let time = time_val.as_hs_time().unwrap();
        assert_eq!(time, NaiveTime::from_hms(23, 59, 15));
    }

    #[test]
    fn as_hs_time_with_no_hour_padding() {
        use chrono::NaiveTime;

        let time_val = json!("h:3:59");
        let time = time_val.as_hs_time().unwrap();
        assert_eq!(time, NaiveTime::from_hms(3, 59, 0));
    }

    #[test]
    fn as_hs_time_with_no_hour_padding_and_seconds() {
        use chrono::NaiveTime;

        let time_val = json!("h:3:59:15");
        let time = time_val.as_hs_time().unwrap();
        assert_eq!(time, NaiveTime::from_hms(3, 59, 15));
    }

    #[test]
    fn as_hs_uri() {
        let uri_val = json!("u:www.test.com");
        let uri = uri_val.as_hs_uri().unwrap();
        assert_eq!(uri, "www.test.com");
    }

    #[test]
    fn as_hs_xstr() {
        let xstr_val = json!("x:Type:value");
        let xstr = xstr_val.as_hs_xstr().unwrap();
        assert_eq!(xstr, "Type:value");
    }

    #[test]
    fn as_hs_coord() {
        use crate::Coord;

        let coord_val = json!("c:37.545,-77.449");
        let coord = coord_val.as_hs_coord().unwrap();
        assert_eq!(coord, Coord::new(37.545, -77.449));
    }

    #[test]
    fn as_hs_date_time() {
        use chrono::{FixedOffset, TimeZone};
        let hour = 3600;

        let dt_val = json!("t:2015-06-08T15:47:41-04:00 New_York");
        let (dt, tz_name) = dt_val.as_hs_date_time().unwrap();
        assert_eq!(tz_name, "New_York");
        assert_eq!(
            dt,
            FixedOffset::west(4 * hour)
                .ymd(2015, 6, 8)
                .and_hms(15, 47, 41),
        );
    }
}