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
use bitflags::bitflags;
bitflags! {
pub struct Type: u32 {
const PROFANE = 0b0_000_000_000_000_000_111;
const OFFENSIVE = 0b0_000_000_000_000_111_000;
const SEXUAL = 0b0_000_000_000_111_000_000;
const MEAN = 0b0_000_000_111_000_000_000;
const EVASIVE = 0b0_000_111_000_000_000_000;
const SPAM = 0b0_111_000_000_000_000_000;
const SAFE = 0b1_000_000_000_000_000_000;
const MILD = 0b0_001_001_001_001_001_001;
const MODERATE = 0b0_010_010_010_010_010_010;
const SEVERE = 0b0_100_100_100_100_100_100;
const MILD_OR_HIGHER = Self::MILD.bits | Self::MODERATE.bits | Self::SEVERE.bits;
const MODERATE_OR_HIGHER = Self::MODERATE.bits | Self::SEVERE.bits;
const INAPPROPRIATE = Self::PROFANE.bits | Self::OFFENSIVE.bits | Self::SEXUAL.bits | (Self::MEAN.bits & Self::SEVERE.bits);
const ANY = Self::PROFANE.bits | Self::OFFENSIVE.bits | Self::SEXUAL.bits | Self::MEAN.bits | Self::EVASIVE.bits | Self::SPAM.bits;
const NONE = 0;
}
}
const SEVERE_WEIGHT: i8 = 3;
const MODERATE_WEIGHT: i8 = 2;
const MILD_WEIGHT: i8 = 1;
impl Type {
pub(crate) const WEIGHT_COUNT: usize = 5;
const WEIGHT_BITS: usize = 3;
pub fn is(self, threshold: Self) -> bool {
self & threshold != Type::NONE
}
pub fn isnt(self, threshold: Self) -> bool {
self & threshold == Type::NONE
}
#[allow(dead_code)]
pub(crate) fn to_weights(self) -> [i8; Self::WEIGHT_COUNT] {
fn bits_to_weight(bits: u32) -> i8 {
if bits == 0 {
0
} else if bits & 0b1 != 0 {
MILD_WEIGHT
} else if bits & 0b10 != 0 {
MODERATE_WEIGHT
} else {
SEVERE_WEIGHT
}
}
let mut i = 0;
[0; Self::WEIGHT_COUNT].map(|_| {
let ret = bits_to_weight((self.bits >> i) & 0b111);
i += Self::WEIGHT_BITS;
ret
})
}
pub(crate) fn from_weights(weights: &[i8; Self::WEIGHT_COUNT]) -> Type {
let mut result = 0;
for (i, &weight) in weights.iter().enumerate() {
let severity: u32 = if weight >= SEVERE_WEIGHT {
0b100
} else if weight == MODERATE_WEIGHT {
0b010
} else if weight == MILD_WEIGHT {
0b001
} else {
0
};
result |= severity << (i * Self::WEIGHT_BITS)
}
Type { bits: result }
}
}
impl Default for Type {
fn default() -> Self {
Self::INAPPROPRIATE
}
}
#[cfg(test)]
impl std::fmt::Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn description(bits: u32) -> &'static str {
if bits & 0b100 != 0 {
"severely"
} else if bits & 0b010 != 0 {
"moderately"
} else if bits & 0b001 != 0 {
"mildly"
} else {
"not"
}
}
let mut count = 0;
if *self & Self::PROFANE != Type::empty() {
if count > 0 {
write!(f, ", ")?;
}
write!(f, "{} profane", description((*self & Self::PROFANE).bits()))?;
count += 1;
}
if *self & Self::OFFENSIVE != Type::empty() {
if count > 0 {
write!(f, ", ")?;
}
write!(
f,
"{} offensive",
description((*self & Self::OFFENSIVE).bits() >> 3)
)?;
count += 1;
}
if *self & Self::SEXUAL != Type::empty() {
if count > 0 {
write!(f, ", ")?;
}
write!(
f,
"{} sexual",
description((*self & Self::SEXUAL).bits() >> 6)
)?;
count += 1;
}
if *self & Self::MEAN != Type::empty() {
if count > 0 {
write!(f, ", ")?;
}
write!(f, "{} mean", description((*self & Self::MEAN).bits() >> 9))?;
count += 1;
}
if *self & Self::EVASIVE != Type::empty() {
if count > 0 {
write!(f, ", ")?;
}
write!(
f,
"{} evasive",
description((*self & Self::EVASIVE).bits() >> 12)
)?;
count += 1;
}
if *self & Self::SPAM != Type::empty() {
if count > 0 {
write!(f, ", ")?;
}
write!(f, "{} spam", description((*self & Self::SPAM).bits() >> 15))?;
count += 1;
}
if count == 0 {
write!(f, "no detections")
} else {
write!(f, "")
}
}
}