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
#![allow(non_upper_case_globals)]
use bitflags::*;
use serde::{Deserialize, Deserializer, Serializer};
use std::str::FromStr;
bitflags! {
pub struct Filters: u32 {
const None = 0;
const Status = 0b_0000_0001;
const Cycle = 0b_0000_0010;
const Mold = 0b_0000_0100;
const Actions = 0b_0000_1000;
const Alarms = 0b_0001_0000;
const Audit = 0b_0010_0000;
const All = 0b_1111_1111;
const JobCards = 0b_0001_0000_0000_0000;
const Operators = 0b_0010_0000_0000_0000;
const OPCUA = 0b_0001_0000_0000_0000_0000_0000_0000_0000;
}
}
static ALL: &str = "Status, Cycle, Mold, Actions, Alarms, Audit, All";
impl Filters {
pub fn has(self, other: Self) -> bool {
self.contains(other)
}
}
impl FromStr for Filters {
type Err = ();
fn from_str(text: &str) -> Result<Self, Self::Err> {
let text = text.trim();
if text == "None" || text.is_empty() {
return Ok(Filters::None);
}
let filters = text
.split(',')
.map(|t| match t.trim() {
"Status" => Filters::Status,
"Cycle" => Filters::Cycle,
"Mold" => Filters::Mold,
"Actions" => Filters::Actions,
"Alarms" => Filters::Alarms,
"Audit" => Filters::Audit,
"All" => Filters::All,
"JobCards" => Filters::JobCards,
"Operators" => Filters::Operators,
"OPCUA" => Filters::OPCUA,
_ => Filters::None,
})
.fold(Filters::None, |f, x| f | x);
Ok(filters)
}
}
impl std::ops::Add for Filters {
type Output = Self;
#[allow(clippy::suspicious_arithmetic_impl)]
fn add(self, rhs: Self) -> Self::Output {
self | rhs
}
}
impl std::ops::AddAssign for Filters {
fn add_assign(&mut self, other: Self) {
*self |= other;
}
}
impl std::fmt::Display for Filters {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let text = format!("{:?}", self).replace(" | ", ", ");
let mut slice = text.trim();
if slice.starts_with(ALL) {
slice = text[ALL.len() - 3..].trim();
}
if text.is_empty() {
write!(f, "None")
} else {
write!(f, "{}", slice)
}
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize_to_flatten_array<S>(x: &Filters, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(&x.to_string())
}
pub fn deserialize_flattened_array<'de, D>(d: D) -> Result<Filters, D::Error>
where
D: Deserializer<'de>,
{
let text = String::deserialize(d)?;
Ok(Filters::from_str(&text).unwrap())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_serialize_filters() {
let f = Filters::All + Filters::Cycle + Filters::OPCUA;
assert_eq!("All, OPCUA", format!("{}", f));
}
}