Skip to main content

snapbox/data/
filters.rs

1use crate::data::DataFormat;
2
3#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
4pub(crate) struct FilterSet {
5    flags: usize,
6    against: Option<DataFormat>,
7}
8
9impl FilterSet {
10    pub(crate) fn new() -> Self {
11        Self::empty().redactions().newlines().paths()
12    }
13
14    pub(crate) const fn empty() -> Self {
15        Self {
16            flags: 0,
17            against: None,
18        }
19    }
20
21    pub(crate) fn redactions(mut self) -> Self {
22        self.set(Self::REDACTIONS);
23        self
24    }
25
26    pub(crate) fn newlines(mut self) -> Self {
27        self.set(Self::NEWLINES);
28        self
29    }
30
31    pub(crate) fn paths(mut self) -> Self {
32        self.set(Self::PATHS);
33        self
34    }
35
36    pub(crate) fn unordered(mut self) -> Self {
37        self.set(Self::UNORDERED);
38        self
39    }
40
41    pub(crate) fn against(mut self, format: DataFormat) -> Self {
42        self.against = Some(format);
43        self
44    }
45
46    pub(crate) const fn is_redaction_set(&self) -> bool {
47        self.is_set(Self::REDACTIONS)
48    }
49
50    pub(crate) const fn is_newlines_set(&self) -> bool {
51        self.is_set(Self::NEWLINES)
52    }
53
54    pub(crate) const fn is_paths_set(&self) -> bool {
55        self.is_set(Self::PATHS)
56    }
57
58    pub(crate) const fn is_unordered_set(&self) -> bool {
59        self.is_set(Self::UNORDERED)
60    }
61
62    pub(crate) const fn get_against(&self) -> Option<DataFormat> {
63        self.against
64    }
65}
66
67impl FilterSet {
68    const REDACTIONS: usize = 1 << 0;
69    const NEWLINES: usize = 1 << 1;
70    const PATHS: usize = 1 << 2;
71    const UNORDERED: usize = 1 << 3;
72
73    fn set(&mut self, flag: usize) -> &mut Self {
74        self.flags |= flag;
75        self
76    }
77
78    const fn is_set(&self, flag: usize) -> bool {
79        self.flags & flag != 0
80    }
81}