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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::{self, Debug, Display, Formatter};

/// DataCollection is returned when a find or search returns
/// multiple Data objects
#[derive(Serialize, Deserialize, Debug)]
pub struct DataCollection {
    pub data: Vec<Data>,
}

/// `Data` stores a unit of data in the redact system. A chunk of
/// data is a `DataValue` (contained within), which can be a `bool`,
/// `u64`, `i64`, `f64`, or `string`. Each data is associated with a `DataPath`
/// which is just a json-style path, and can optionally be encrypted
/// by a variety of keys as specified by the key names in `encryptedby`.
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Data {
    path: DataPath,
    #[serde(default)]
    value: DataValue,
    encryptedby: Option<Vec<String>>,
}

impl Data {
    /// Builds a new Data struct using the provided values
    pub fn new(path: &str, value: DataValue, encryptedby: Option<Vec<String>>) -> Self {
        Data {
            path: DataPath::from(path),
            value,
            encryptedby,
        }
    }

    /// Returns an owned string representing the data's jsonpath
    pub fn path(&self) -> String {
        self.path.to_string()
    }

    /// Returns the optional list of keys this data is encrypted by
    pub fn encryptedby(&self) -> &Option<Vec<String>> {
        &self.encryptedby
    }
}

impl Display for Data {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value.to_string())
    }
}

/// `DataValue` contains the actual raw value of a piece of `Data`.
/// A `DataValue` should always be a leaf value, not an array or object.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(into = "String", from = "Value")]
pub enum DataValue {
    Bool(bool),
    U64(u64),
    I64(i64),
    F64(f64),
    String(String),
}

impl Default for DataValue {
    fn default() -> Self {
        Self::Bool(false)
    }
}

impl From<DataValue> for String {
    fn from(val: DataValue) -> Self {
        val.to_string()
    }
}

impl From<&str> for DataValue {
    fn from(s: &str) -> Self {
        if let Ok(b) = s.parse::<bool>() {
            DataValue::Bool(b)
        } else if let Ok(n) = s.parse::<u64>() {
            DataValue::U64(n)
        } else if let Ok(n) = s.parse::<i64>() {
            DataValue::I64(n)
        } else if let Ok(n) = s.parse::<f64>() {
            DataValue::F64(n)
        } else {
            DataValue::String(s.to_owned())
        }
    }
}

impl From<Value> for DataValue {
    fn from(v: Value) -> Self {
        match v {
            Value::Null => DataValue::String("".to_owned()),
            Value::Bool(b) => DataValue::Bool(b),
            Value::Number(n) => DataValue::from(n.to_string().as_ref()),
            Value::String(s) => DataValue::String(s),
            _ => DataValue::String(v.to_string()),
        }
    }
}

impl Display for DataValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            DataValue::Bool(ref b) => write!(f, "{}", b),
            DataValue::U64(ref n) => write!(f, "{}", n),
            DataValue::I64(ref n) => write!(f, "{}", n),
            DataValue::F64(ref n) => write!(f, "{}", n),
            DataValue::String(ref s) => write!(f, "{}", s),
        }
    }
}

/// `DataPath` represents a json-style path for the location of a `Data` object.
/// The path should always be formatted as `.my.json.path.`; note the beginning and
/// ending periods. `DataPath` will automatically handle path validation when
/// created or deserialized, just provide any valid json-path on creation.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(into = "String", from = "String")]
pub struct DataPath {
    path: String,
}

impl DataPath {
    /// Validates a given string and returns a new DataPath
    pub fn new(path: &str) -> Self {
        let path = Self::validate_path(path);
        Self { path }
    }

    // Ensures that a data entry path begins and ends with a period ('.')
    // Empty strings will return as "."
    // Strings of length 1 where the only char is a period will return as "."
    // All other strings will have periods added to the beginning or end if needed.
    // For now, string containing multiple periods in a row, or composed only of
    // multiple periods, will be accepted and returned as given, with the same
    // behavior as any other standard string of len > 1.
    // This function is implemented as a boolean circuit to avoid iterating through
    // the same string numerous times.
    fn validate_path(path: &str) -> String {
        // Short circuit if path is empty
        if path.is_empty() {
            return ".".to_owned();
        }

        // Collect the first and last characters of the path
        let mut path_chars = path.chars();
        let first_char = path_chars.next();
        let last_char = path_chars.last();

        // Match on the results of char extraction
        match (first_char, last_char) {
            // String length >= 2
            (Some(fc), Some(lc)) => {
                if fc != '.' && lc != '.' {
                    format!(".{}.", path)
                } else if fc == '.' && lc == '.' {
                    path.to_owned()
                } else if fc != '.' {
                    format!(".{}", path)
                } else {
                    format!("{}.", path)
                }
            }
            // String length == 1
            (Some(fc), None) => {
                if fc == '.' {
                    path.to_owned()
                } else {
                    format!(".{}.", path)
                }
            }
            // Impossible case: string length == 0, should never be here because
            // of the short-circuit implemented at the beginning of the function
            (None, None) => panic!(
                "this is an impossible situation; if you have gotten here, \\
	     a short-circuit earlier in the function has failed to function as \\
	     intended"
            ),
            // Impossible case: if this happens we should panic because something is
            // fundamentally wrong with the computing environment and someone should
            // know about it.
            // If the last char is != None, then it MUST BE that the
            // first char is != None, as the last char is collected after the
            // iterator has ticked over one spot to account for the first char,
            // therefore if the iterator finds something in the last() call, then
            // it must be after having collected something from the nth(0) call.
            (None, Some(_)) => panic!(
                "this is an impossible situation; if you have gotten here, \\
	     something has happened that should never happen according to the \\
	     laws of computing and/or the rust compiler. if you have gotten here, \\
	     some major memory or computing trickery has occurred and you should \\
	     be concerned for the integrity of your computing base"
            ),
        }
    }
}

impl Display for DataPath {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.path)
    }
}

impl<'a> From<&'a str> for DataPath {
    fn from(path: &'a str) -> Self {
        Self::new(path)
    }
}

/// We need this because of a requirement on Deserialize
/// Prefer not to use it, using the &str version instead
impl From<String> for DataPath {
    fn from(path: String) -> Self {
        Self::from(path.as_ref())
    }
}

impl From<DataPath> for String {
    fn from(dp: DataPath) -> Self {
        dp.to_string()
    }
}

#[cfg(test)]
mod tests {
    mod datavalue {
        use crate::data::DataValue;
        use serde_json::{json, Value};
        use std::convert::From;

        #[test]
        fn test_default_is_false_bool() {
            let dv = DataValue::default();
            match dv {
                DataValue::Bool(b) => {
                    assert!(!b, "default DataValue should be a DataValue::Bool(false)")
                }
                _ => {
                    panic!("default DataValue should be a DataValue::Bool(false)")
                }
            }
        }

        #[test]
        fn test_to_string_bool_true() {
            let dv = DataValue::from("true");
            assert_eq!(dv.to_string(), "true");
        }

        #[test]
        fn test_to_string_bool_false() {
            let dv = DataValue::from("false");
            assert_eq!(dv.to_string(), "false");
        }

        #[test]
        fn test_to_string_u64() {
            let dv = DataValue::from("24");
            assert_eq!(dv.to_string(), "24");
        }

        #[test]
        fn test_to_string_i64() {
            let dv = DataValue::from("-10");
            assert_eq!(dv.to_string(), "-10");
        }

        #[test]
        fn test_to_string_f64() {
            let dv = DataValue::from("10.3");
            assert_eq!(dv.to_string(), "10.3");
        }

        #[test]
        fn test_to_string_string() {
            let dv = DataValue::from("somestr");
            assert_eq!(dv.to_string(), "somestr");
        }

        #[test]
        fn test_from_datavalue_for_string() {
            let dv = DataValue::default();
            let s: String = From::<DataValue>::from(dv);
            assert_eq!(s, "false");
        }

        #[test]
        fn test_from_string_for_bool_true() {
            let dv = From::<&str>::from("true");
            match dv {
                DataValue::Bool(b) => assert!(b),
                _ => panic!("DataValue should have been a Bool variant"),
            }
        }

        #[test]
        fn test_from_string_for_bool_false() {
            let dv = From::<&str>::from("false");
            match dv {
                DataValue::Bool(b) => assert!(!b),
                _ => panic!("DataValue should have been a Bool variant"),
            }
        }

        #[test]
        fn test_from_string_for_zero() {
            let dv = From::<&str>::from("0");
            match dv {
                DataValue::U64(n) => assert_eq!(n, 0),
                _ => panic!("DataValue should have been a U64 variant"),
            }
        }

        #[test]
        fn test_from_string_for_positive_integer() {
            let dv = From::<&str>::from("100");
            match dv {
                DataValue::U64(n) => assert_eq!(n, 100),
                _ => panic!("DataValue should have been a U64 variant"),
            }
        }

        #[test]
        fn test_from_string_for_negative_integer() {
            let dv = From::<&str>::from("-1");
            match dv {
                DataValue::I64(n) => assert_eq!(n, -1),
                _ => panic!("DataValue should have been a I64 variant"),
            }
        }

        #[test]
        fn test_from_string_for_positive_decimal() {
            let dv = From::<&str>::from("10.52");
            match dv {
                // We have to do the f64::EPSILON comparison here as floating point
                // comparisons are inherently inexact; see:
                // https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp
                DataValue::F64(n) => assert!((n - 10.52f64).abs() < f64::EPSILON),
                _ => panic!("DataValue should have been a F64 variant"),
            }
        }

        #[test]
        fn test_from_string_for_negative_decimal() {
            let dv = From::<&str>::from("-4.38");
            match dv {
                // We have to do the f64::EPSILON comparison here as floating point
                // comparisons are inherently inexact; see:
                // https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp
                DataValue::F64(n) => assert!((n + 4.38f64).abs() < f64::EPSILON),
                _ => panic!("DataValue should have been a F64 variant"),
            }
        }

        #[test]
        fn test_from_string_for_string() {
            let dv = From::<&str>::from("somestr");
            match dv {
                DataValue::String(s) => assert_eq!(s, "somestr"),
                _ => panic!("DataValue should have been a String variant"),
            }
        }

        #[test]
        fn test_from_string_for_string_that_starts_with_a_number() {
            let dv = From::<&str>::from("10.52a");
            match dv {
                DataValue::String(s) => assert_eq!(s, "10.52a"),
                _ => panic!("DataValue should have been a String variant"),
            }
        }

        #[test]
        fn test_from_string_for_empty_string() {
            let dv = From::<&str>::from("");
            match dv {
                DataValue::String(s) => assert_eq!(s, ""),
                _ => panic!("DataValue should have been a String variant"),
            }
        }

        #[test]
        fn test_from_value_for_null_variant() {
            let dv = From::<Value>::from(Value::Null);
            match dv {
                DataValue::String(s) => assert_eq!(s, ""),
                _ => panic!("DataValue should have been a String variant"),
            }
        }

        #[test]
        fn test_from_value_for_bool_true_variant() {
            let dv = From::<Value>::from(Value::Bool(true));
            match dv {
                DataValue::Bool(b) => assert!(b),
                _ => panic!("DataValue should have been a Bool variant"),
            }
        }

        #[test]
        fn test_from_value_for_bool_false_variant() {
            let dv = From::<Value>::from(Value::Bool(false));
            match dv {
                DataValue::Bool(b) => assert!(!b),
                _ => panic!("DataValue should have been a Bool variant"),
            }
        }

        #[test]
        fn test_from_value_for_number_zero_variant() {
            let dv = From::<Value>::from(json!(0));
            match dv {
                DataValue::U64(n) => assert_eq!(n, 0),
                _ => panic!("DataValue should have been a U64 variant"),
            }
        }

        #[test]
        fn test_from_value_for_number_negative_variant() {
            let dv = From::<Value>::from(json!(-1240));
            match dv {
                DataValue::I64(n) => assert_eq!(n, -1240),
                _ => panic!("DataValue should have been an I64 variant"),
            }
        }

        #[test]
        fn test_from_value_for_number_negative_decimal_variant() {
            let dv = From::<Value>::from(json!(-300.434));
            match dv {
                DataValue::F64(n) => assert!((n + 300.434).abs() < f64::EPSILON),
                _ => panic!("DataValue should have been an F64 variant"),
            }
        }

        #[test]
        fn test_from_value_for_number_positive_decimal_variant() {
            let dv = From::<Value>::from(json!(0.001));
            match dv {
                DataValue::F64(n) => assert!((n - 0.001).abs() < f64::EPSILON),
                _ => panic!("DataValue should have been an F64 variant"),
            }
        }

        #[test]
        fn test_from_value_for_string_variant() {
            let dv = From::<Value>::from(Value::String("somestr".to_owned()));
            match dv {
                DataValue::String(s) => assert_eq!(s, "somestr"),
                _ => panic!("DataValue should have been a String variant"),
            }
        }

        #[test]
        fn test_from_value_for_object_variant() {
            let dv = From::<Value>::from(json!({ "key": "value" }));
            match dv {
                DataValue::String(s) => assert_eq!(s, "{\"key\":\"value\"}"),
                _ => panic!("DataValue should have been a String variant"),
            }
        }

        #[test]
        fn test_from_value_for_array_variant() {
            let dv = From::<Value>::from(json!([ 1, "str", { "key": "value" } ]));
            match dv {
                DataValue::String(s) => assert_eq!(s, "[1,\"str\",{\"key\":\"value\"}]"),
                _ => panic!("DataValue should have been a String variant"),
            }
        }
    }

    mod datapath {
        use crate::data::DataPath;
        use std::convert::From;

        #[test]
        fn test_new_with_valid_path() {
            let dp = DataPath::new(".my.path.");
            assert_eq!(dp.to_string(), ".my.path.");
        }

        #[test]
        fn test_new_with_path_missing_first_period() {
            let dp = DataPath::new("my.path.");
            assert_eq!(dp.to_string(), ".my.path.");
        }

        #[test]
        fn test_new_with_path_missing_last_period() {
            let dp = DataPath::new(".my.path");
            assert_eq!(dp.to_string(), ".my.path.");
        }

        #[test]
        fn test_new_with_path_missing_first_and_last_period() {
            let dp = DataPath::new("my.path");
            assert_eq!(dp.to_string(), ".my.path.");
        }

        #[test]
        fn test_new_with_path_with_no_periods() {
            let dp = DataPath::new("my");
            assert_eq!(dp.to_string(), ".my.");
        }

        #[test]
        fn test_new_with_empty_path() {
            let dp = DataPath::new("");
            assert_eq!(dp.to_string(), ".");
        }

        #[test]
        fn test_new_with_path_is_single_period() {
            let dp = DataPath::new(".");
            assert_eq!(dp.to_string(), ".");
        }

        #[test]
        fn test_new_with_path_is_double_period() {
            let dp = DataPath::new("..");
            assert_eq!(dp.to_string(), "..");
        }

        #[test]
        fn test_from_string() {
            let dp: DataPath = From::<String>::from("my.path".to_owned());
            assert_eq!(dp.to_string(), ".my.path.");
        }

        #[test]
        fn test_from_str() {
            let dp: DataPath = From::<&str>::from("my.path.");
            assert_eq!(dp.to_string(), ".my.path.");
        }

        #[test]
        fn test_from_datapath_for_string() {
            let dp = DataPath::new(".my.path");
            let s: String = From::<DataPath>::from(dp);
            assert_eq!(s, ".my.path.");
        }
    }
}