hoard 0.5.1

Hoard backups of files across your filesystem into one location.
Documentation
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! A `Combinator` provides a *newtype* to parse a two-dimensional list of items as a collection of
//! AND-ed and OR-ed elements. Every item in the outer list is OR-ed, while every item in an inner list
//! is AND-ed. That is, for
//!
//! ```ignore
//! [ foo, bar, [baz, quux]]
//! ```
//!
//! The list will be parsed as `foo OR bar OR (baz AND quux)`.
//!
//! All AND/OR combinations must be expressed in this fashion. For example, to get `foo AND bar`,
//! you want `[[foo, bar]]`.
//!
//! It also implements `serde::Serialize` and `serde::Deserialize` for innermost types that implement
//! `Into<bool>` and the appropriate serde impl. No optimizations are applied to make later
//! evaluations easier.

use core::fmt;
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::error::Error;
use std::fmt::Formatter;
use tap::TapFallible;

/// An internal container for the [`Combinator<T>`] type.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum Inner<T: TryInto<bool>> {
    /// A single item that can be converted to a boolean.
    Single(T),
    /// Multiple items that can be converted to booleans.
    ///
    /// When the [`Inner<T>`] is evaluated, all of the booleans are AND-ed together.
    Multiple(Vec<T>),
}

impl<T> Inner<T>
where
    T: TryInto<bool>,
{
    /// Whether this [`Inner<T>`] contains a single item.
    ///
    /// This can be either an [`Inner<T>::Single`] or an [`Inner<T>::Multiple`] containing
    /// only one item.
    pub fn is_singleton(&self) -> bool {
        match self {
            Inner::Single(_) => true,
            Inner::Multiple(list) => list.len() == 1,
        }
    }

    /// Whether this [`Inner<T>`] is empty.
    ///
    /// This only applies if it is an [`Inner<T>::Multiple`] containing an empty list.
    pub fn is_empty(&self) -> bool {
        match self {
            Inner::Single(_) => false,
            Inner::Multiple(list) => list.is_empty(),
        }
    }
}

impl<T, E> TryFrom<Inner<T>> for bool
where
    T: TryInto<bool, Error = E>,
    E: Error,
{
    type Error = E;

    fn try_from(combinator: Inner<T>) -> Result<bool, Self::Error> {
        match combinator {
            Inner::Single(item) => item.try_into(),
            Inner::Multiple(list) => list
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>, E>>()
                .map(|list| list.into_iter().all(|item| item)),
        }
    }
}

impl<T> fmt::Display for Inner<T>
where
    T: fmt::Display + TryInto<bool>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Inner::Single(item) => write!(f, "{}", item),
            Inner::Multiple(list) => {
                let s = list.iter().map(ToString::to_string).reduce(|mut a, s| {
                    a.push_str(" AND ");
                    a.push_str(&s);
                    a
                });

                match s {
                    None => write!(f, ""),
                    Some(s) => write!(f, "{}", s),
                }
            }
        }
    }
}

/// A combination of things that can evaluate to `true` or `false`.
///
/// If at least one of the [`Inner<T>`] items evaluates to `true`, then entire `Combinator<T>`
/// will evaluate to `true`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(transparent)]
pub struct Combinator<T: TryInto<bool>>(pub Vec<Inner<T>>);

impl<T> Combinator<T>
where
    T: Serialize + TryInto<bool>,
{
    /// Whether the [`Combinator<T>`] is empty.
    ///
    /// This can be if the list of [`Inner<T>`] is empty or if all of the contained
    /// [`Inner<T>`] are empty (think a list of empty lists).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty() || self.0.iter().all(Inner::is_empty)
    }

    /// Whether the [`Combinator<T>`] is a singleton.
    ///
    /// This can be any number of empty [`Inner<T>`] and exactly one that is a singleton.
    #[must_use]
    pub fn is_singleton(&self) -> bool {
        let mut iter = self.0.iter().filter(|inner| !inner.is_empty());
        match iter.next() {
            None => false,
            Some(inner) => inner.is_singleton() && iter.next().is_none(),
        }
    }

    /// Whether the [`Combinator<T>`] is only OR-ed items.
    ///
    /// This means the [`Combinator`] has at least two items that are OR-ed together; i.e.,
    /// it contains two or more [`Inner<T>`] that are all singletons.
    #[must_use]
    pub fn is_only_or(&self) -> bool {
        !self.is_empty() && !self.is_singleton() && self.0.iter().all(Inner::is_singleton)
    }

    /// Whether the [`Combinator<T>`] is only AND-ed items.
    ///
    /// This means the [`Combinator`] has at least two items that are AND-ed together; i.e.,
    /// it contains exactly one [`Inner<T>::Multiple`] with at least two items.
    #[must_use]
    pub fn is_only_and(&self) -> bool {
        self.0.len() == 1 && !self.is_empty() && !self.is_singleton()
    }

    /// Whether the [`Combinator<T>`] is complex.
    ///
    /// "Complex" here means that it contains at least three items with some combination of AND
    /// and OR.
    #[must_use]
    pub fn is_complex(&self) -> bool {
        !(self.is_empty() || self.is_singleton() || self.is_only_and() || self.is_only_or())
    }

    /// Convert this [`Combinator<T>`] to TOML.
    ///
    /// # Errors
    ///
    /// Any errors during the serialization process ([`toml::ser::Error`]).
    pub fn to_toml_string(&self) -> Result<String, toml::ser::Error> {
        toml::to_string(&self).tap_err(crate::tap_log_error_msg(
            "failed to serialize combinator as TOML",
        ))
    }
}

impl<T> fmt::Display for Combinator<T>
where
    T: fmt::Display + TryInto<bool>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let Combinator(list) = self;

        let is_one_item = list.len() == 1;
        let s = list
            .iter()
            .map(|item| {
                if item.is_singleton() || item.is_empty() || is_one_item {
                    item.to_string()
                } else {
                    format!("({})", item)
                }
            })
            .reduce(|mut a, s| {
                a.push_str(" OR ");
                a.push_str(&s);
                a
            });

        match s {
            None => write!(f, ""),
            Some(s) => write!(f, "{}", s),
        }
    }
}

impl<T, E> TryFrom<Combinator<T>> for bool
where
    T: TryInto<bool, Error = E>,
    E: Error,
{
    type Error = E;

    fn try_from(Combinator(combinator): Combinator<T>) -> Result<bool, Self::Error> {
        if combinator.is_empty() {
            return Ok(true);
        }

        combinator
            .into_iter()
            .map(TryInto::try_into)
            .collect::<Result<Vec<_>, E>>()
            .map(|list| list.into_iter().any(|item| item))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use once_cell::sync::Lazy;
    use serde_test::{assert_tokens, Token};
    use std::convert::Infallible;

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    #[serde(transparent)]
    struct Tester(bool);

    impl TryFrom<Tester> for bool {
        type Error = Infallible;

        fn try_from(Tester(b): Tester) -> Result<bool, Self::Error> {
            Ok(b)
        }
    }

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

    enum CombinatorType {
        Empty,
        Singleton,
        OnlyOr,
        OnlyAnd,
        Complex,
    }

    #[test]
    fn test_inner() {
        let test_params = vec![
            (true, Inner::Single(Tester(true))),
            (false, Inner::Single(Tester(false))),
            // CombinatorInner::Multiple should AND all items
            (
                true,
                Inner::Multiple(vec![Tester(true), Tester(true), Tester(true)]),
            ),
            (
                false,
                Inner::Multiple(vec![Tester(true), Tester(false), Tester(true)]),
            ),
        ];

        for (expected, input) in test_params {
            let result: bool = input.clone().try_into().unwrap();
            assert_eq!(
                result, expected,
                "CombinatorInner did not AND items correctly: {:?}",
                input
            );
        }
    }

    #[test]
    fn test_inner_serde_single() {
        let test_params_bool = vec![
            ([Token::Bool(true)], Inner::Single(true)),
            ([Token::Bool(false)], Inner::Single(false)),
        ];

        for (expected, input) in test_params_bool {
            assert_tokens(&input, &expected);
        }

        let test_params_tester = vec![
            ([Token::Bool(true)], Inner::Single(Tester(true))),
            ([Token::Bool(false)], Inner::Single(Tester(false))),
        ];

        for (expected, input) in test_params_tester {
            assert_tokens(&input, &expected);
        }
    }

    #[test]
    fn test_inner_serde_multiple() {
        let test_params = vec![
            (
                &[
                    Token::Seq { len: Some(3) },
                    Token::Bool(true),
                    Token::Bool(false),
                    Token::Bool(false),
                    Token::SeqEnd,
                ][..],
                Inner::Multiple(vec![Tester(true), Tester(false), Tester(false)]),
            ),
            (
                &[
                    Token::Seq { len: Some(3) },
                    Token::Bool(false),
                    Token::Bool(true),
                    Token::Bool(false),
                    Token::SeqEnd,
                ][..],
                Inner::Multiple(vec![Tester(false), Tester(true), Tester(false)]),
            ),
            (
                &[
                    Token::Seq { len: Some(1) },
                    Token::Bool(true),
                    Token::SeqEnd,
                ][..],
                Inner::Multiple(vec![Tester(true)]),
            ),
        ];

        for (expected, input) in test_params {
            assert_tokens(&input, expected);
        }
    }

    struct TestItem {
        combinator: Combinator<Tester>,
        evaluates_to: bool,
        expected_bool_str: &'static str,
        typ: CombinatorType,
    }

    #[allow(clippy::too_many_lines)]
    static CASES: Lazy<Vec<TestItem>> = Lazy::new(|| {
        vec![
            // BEGIN: Empty combinators
            TestItem {
                combinator: Combinator(vec![]),
                // Empty should evaluate to true
                evaluates_to: true,
                expected_bool_str: "",
                typ: CombinatorType::Empty,
            },
            // Containing only an empty Inner::Multiple counts as empty
            TestItem {
                combinator: Combinator(vec![Inner::Multiple(vec![])]),
                // Empty should evaluate to true
                evaluates_to: true,
                expected_bool_str: "",
                typ: CombinatorType::Empty,
            },
            // BEGIN: Singleton
            TestItem {
                combinator: Combinator(vec![Inner::Single(Tester(true))]),
                evaluates_to: true,
                expected_bool_str: "true",
                typ: CombinatorType::Singleton,
            },
            TestItem {
                combinator: Combinator(vec![Inner::Single(Tester(false))]),
                evaluates_to: false,
                expected_bool_str: "false",
                typ: CombinatorType::Singleton,
            },
            // Containing a single Inner::Multiple with a single item counts as singleton
            TestItem {
                combinator: Combinator(vec![Inner::Multiple(vec![Tester(true)])]),
                evaluates_to: true,
                expected_bool_str: "true",
                typ: CombinatorType::Singleton,
            },
            TestItem {
                combinator: Combinator(vec![Inner::Multiple(vec![Tester(false)])]),
                evaluates_to: false,
                expected_bool_str: "false",
                typ: CombinatorType::Singleton,
            },
            // BEGIN: OnlyOr
            TestItem {
                combinator: Combinator(vec![
                    Inner::Single(Tester(true)),
                    Inner::Single(Tester(false)),
                ]),
                evaluates_to: true,
                expected_bool_str: "true OR false",
                typ: CombinatorType::OnlyOr,
            },
            TestItem {
                combinator: Combinator(vec![
                    Inner::Single(Tester(false)),
                    Inner::Single(Tester(false)),
                ]),
                evaluates_to: false,
                expected_bool_str: "false OR false",
                typ: CombinatorType::OnlyOr,
            },
            // Containing Inner::Multiples with only one item in them counts like a Single
            TestItem {
                combinator: Combinator(vec![
                    Inner::Single(Tester(false)),
                    Inner::Multiple(vec![Tester(true)]),
                ]),
                evaluates_to: true,
                expected_bool_str: "false OR true",
                typ: CombinatorType::OnlyOr,
            },
            TestItem {
                combinator: Combinator(vec![
                    Inner::Single(Tester(true)),
                    Inner::Multiple(vec![Tester(false)]),
                ]),
                evaluates_to: true,
                expected_bool_str: "true OR false",
                typ: CombinatorType::OnlyOr,
            },
            // BEGIN: OnlyAnd
            TestItem {
                combinator: Combinator(vec![Inner::Multiple(vec![
                    Tester(true),
                    Tester(true),
                    Tester(true),
                ])]),
                evaluates_to: true,
                expected_bool_str: "true AND true AND true",
                typ: CombinatorType::OnlyAnd,
            },
            TestItem {
                combinator: Combinator(vec![Inner::Multiple(vec![
                    Tester(true),
                    Tester(false),
                    Tester(true),
                ])]),
                evaluates_to: false,
                expected_bool_str: "true AND false AND true",
                typ: CombinatorType::OnlyAnd,
            },
            // BEGIN: Complex
            TestItem {
                combinator: Combinator(vec![
                    Inner::Single(Tester(true)),
                    Inner::Multiple(vec![Tester(true), Tester(false)]),
                ]),
                evaluates_to: true,
                expected_bool_str: "true OR (true AND false)",
                typ: CombinatorType::Complex,
            },
            TestItem {
                combinator: Combinator(vec![
                    Inner::Single(Tester(false)),
                    Inner::Multiple(vec![Tester(true), Tester(false)]),
                ]),
                evaluates_to: false,
                expected_bool_str: "false OR (true AND false)",
                typ: CombinatorType::Complex,
            },
            TestItem {
                combinator: Combinator(vec![
                    Inner::Single(Tester(false)),
                    Inner::Multiple(vec![Tester(true), Tester(false), Tester(false)]),
                    Inner::Single(Tester(false)),
                ]),
                evaluates_to: false,
                expected_bool_str: "false OR (true AND false AND false) OR false",
                typ: CombinatorType::Complex,
            },
        ]
    });

    #[test]
    fn test_combinator() {
        for case in CASES.iter() {
            let result: bool = case.combinator.clone().try_into().unwrap();
            assert_eq!(
                result, case.evaluates_to,
                "Combinator did not OR items correctly: {:?}",
                case.combinator
            );
        }
    }

    #[test]
    fn test_combinator_to_boolean_string() {
        for case in CASES.iter() {
            assert_eq!(
                case.expected_bool_str,
                &case.combinator.to_string(),
                "failed to create boolean string for {:?}",
                case.combinator
            );
        }
    }

    #[test]
    fn test_combinator_to_toml_string() {
        for case in CASES.iter() {
            let toml_str = toml::to_string(&case.combinator);
            assert_eq!(toml_str, case.combinator.to_toml_string());
        }
    }

    #[test]
    fn test_combinator_is_empty() {
        for case in CASES.iter() {
            assert_eq!(
                matches!(case.typ, CombinatorType::Empty),
                case.combinator.is_empty(),
                "left value indicates whether {:?} should be empty",
                case.combinator
            );
        }
    }

    #[test]
    fn test_is_singleton() {
        for case in CASES.iter() {
            assert_eq!(
                matches!(case.typ, CombinatorType::Singleton),
                case.combinator.is_singleton()
            );
        }
    }

    #[test]
    fn test_is_only_or() {
        for case in CASES.iter() {
            assert_eq!(
                matches!(case.typ, CombinatorType::OnlyOr),
                case.combinator.is_only_or(),
                "left value indicates whether {:?} should be only OR",
                case.combinator
            );
        }
    }

    #[test]
    fn test_is_only_and() {
        for case in CASES.iter() {
            assert_eq!(
                matches!(case.typ, CombinatorType::OnlyAnd),
                case.combinator.is_only_and(),
                "left value indicates whether {:?} should be only AND",
                case.combinator
            );
        }
    }

    #[test]
    fn test_is_complex() {
        for case in CASES.iter() {
            assert_eq!(
                matches!(case.typ, CombinatorType::Complex),
                case.combinator.is_complex(),
                "left value indicates whether {:?} should be complex",
                case.combinator
            );
        }
    }
}