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
use bitflags::bitflags;
use std::fmt::Debug;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};

bitflags! {
    struct TypeRepr: 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;
    }
}

/// Type is represents a type or severity of inappropriateness.
/// They can be combined with bitwise AND and OR operators, and are **not** mutually exclusive.
///
/// For example, the following means profane or at-least moderately mean:
/// `Type::PROFANE | (Type::MEAN & Type::MODERATE_OR_HIGHER)`
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct Type(TypeRepr);

const SEVERE_WEIGHT: i8 = 3;
const MODERATE_WEIGHT: i8 = 2;
const MILD_WEIGHT: i8 = 1;

impl Type {
    /// Bad words.
    pub const PROFANE: Self = Self(TypeRepr::PROFANE);

    /// Offensive words.
    pub const OFFENSIVE: Self = Self(TypeRepr::OFFENSIVE);

    /// Sexual words.
    pub const SEXUAL: Self = Self(TypeRepr::SEXUAL);

    /// Mean words.
    pub const MEAN: Self = Self(TypeRepr::MEAN);

    /// Words intended to evade detection.
    pub const EVASIVE: Self = Self(TypeRepr::EVASIVE);

    /// Spam/gibberish/SHOUTING.
    pub const SPAM: Self = Self(TypeRepr::SPAM);

    /// One of a very small number of safe phases.
    /// Recommended to enforce this on users who repeatedly evade the filter.
    pub const SAFE: Self = Self(TypeRepr::SAFE);

    /// Not that bad.
    pub const MILD: Self = Self(TypeRepr::MILD);

    /// Bad.
    pub const MODERATE: Self = Self(TypeRepr::MODERATE);

    /// Cover your eyes!
    pub const SEVERE: Self = Self(TypeRepr::SEVERE);

    /// Any level; `Type::MILD`, `Type::MODERATE`, or `Type::SEVERE`.
    pub const MILD_OR_HIGHER: Self = Self(TypeRepr::MILD_OR_HIGHER);

    /// Any level in excess of `Type::MILD`.
    pub const MODERATE_OR_HIGHER: Self = Self(TypeRepr::MODERATE_OR_HIGHER);

    /// The default `Type`, meaning profane, offensive, sexual, or severely mean.
    pub const INAPPROPRIATE: Self = Self(TypeRepr::INAPPROPRIATE);

    /// Any type of detection (except SAFE). This will be expanded to cover all future types.
    pub const ANY: Self = Self(TypeRepr::ANY);

    /// No type of detection.
    pub const NONE: Self = Self(TypeRepr::NONE);

    /// Number of weights.
    pub(crate) const WEIGHT_COUNT: usize = 5;
    /// Bits per weight;
    const WEIGHT_BITS: usize = 3;

    /// Returns `true` if and only if self, the analysis result, meets the given threshold.
    pub fn is(self, threshold: Self) -> bool {
        self & threshold != Type::NONE
    }

    /// Logical opposite of `Self::is`.
    pub fn isnt(self, threshold: Self) -> bool {
        self & threshold == Type::NONE
    }

    #[deprecated(note = "this is for backwards-compatibility, use Type::NONE instead")]
    pub fn empty() -> Self {
        Self::NONE
    }

    #[deprecated(note = "this is for backwards-compatibility, compare with Type::NONE instead")]
    pub fn is_empty(self) -> bool {
        self.0.is_empty()
    }

    #[deprecated(note = "this is for backwards-compatibility, there is no replacement")]
    pub fn bits(self) -> u32 {
        self.0.bits
    }

    #[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.0.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 // none
            };

            result |= severity << (i * Self::WEIGHT_BITS)
        }
        Self(TypeRepr { bits: result })
    }
}

impl Default for Type {
    /// Returns a reasonable default for censoring or blocking.
    fn default() -> Self {
        Self::INAPPROPRIATE
    }
}

impl BitAnd for Type {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        // Delegate to bitflags
        Self(self.0.bitand(rhs.0))
    }
}

impl BitOr for Type {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        // Delegate to bitflags
        Self(self.0.bitor(rhs.0))
    }
}

impl BitAndAssign for Type {
    fn bitand_assign(&mut self, rhs: Self) {
        // Delegate to bitflags
        self.0.bitand_assign(rhs.0)
    }
}

impl BitOrAssign for Type {
    fn bitor_assign(&mut self, rhs: Self) {
        // Delegate to bitflags
        self.0.bitor_assign(rhs.0)
    }
}

impl Not for Type {
    type Output = Self;

    fn not(self) -> Self::Output {
        Self(self.0.not())
    }
}

/// Note: Can't impl directly on TypeRepr due to https://github.com/bitflags/bitflags/issues/218
impl Debug 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 != Self::NONE {
            if count > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{} profane",
                description((*self & Self::PROFANE).0.bits())
            )?;
            count += 1;
        }
        if *self & Self::OFFENSIVE != Self::NONE {
            if count > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{} offensive",
                description((*self & Self::OFFENSIVE).0.bits() >> 3)
            )?;
            count += 1;
        }
        if *self & Self::SEXUAL != Self::NONE {
            if count > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{} sexual",
                description((*self & Self::SEXUAL).0.bits() >> 6)
            )?;
            count += 1;
        }
        if *self & Self::MEAN != Self::NONE {
            if count > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{} mean",
                description((*self & Self::MEAN).0.bits() >> 9)
            )?;
            count += 1;
        }
        if *self & Self::EVASIVE != Self::NONE {
            if count > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{} evasive",
                description((*self & Self::EVASIVE).0.bits() >> 12)
            )?;
            count += 1;
        }
        if *self & Self::SPAM != Self::NONE {
            if count > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{} spam",
                description((*self & Self::SPAM).0.bits() >> 15)
            )?;
            count += 1;
        }
        if *self & Self::SAFE != Self::NONE {
            if count > 0 {
                write!(f, ", ")?;
            }
            write!(f, "safe")
        } else if count == 0 {
            write!(f, "no detections")
        } else {
            write!(f, "")
        }
    }
}