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
//! Bitflags handling and storage.
//!
//! This crate allows you to define flag values using an enum and derive
//! `BitFlags` to add convenience methods.
//!
//! This implementation was heavily inspired by
//! [enumflags2](https://crates.io/crates/enumflags2) and
//! [bitflags](https://crates.io/crates/bitflags) and customized for use in a
//! sawp parser. Consider using those two open source projects before resorting
//! to this one. One key feature is that we are automatically generating ffi
//! accessors using the [sawp-ffi](https://crates.io/crates/sawp-ffi) crate.
//!
//! This crate works as follows:
//! - `enum YourEnum` with a numeric representation (e.g. `#[repr(u8)]`) is used
//!   to define bit fields.
//! - deriving `BitFlags` on this enum will add convenience methods for bitwise
//!   operations and implement the `Flag` trait.
//! - Flag values are transparently stored as `Flags<YourEnum>` so you can perform
//!   more operations on this type.
//!
//! # Example
//! See `example` module for a generated example as well.
//! ```
//! use sawp_flags::{BitFlags, Flags, Flag};
//!
//! /// Example enum
//! #[derive(Debug, Clone, Copy, PartialEq, BitFlags)]
//! #[repr(u8)]
//! pub enum Test {
//!     A = 0b0001,
//!     B = 0b0010,
//!     C = 0b0100,
//!     D = 0b1000,
//!     /// Variants can be a bitmask of the other fields like so
//!     E = Test::A as u8 | Test::B as u8 | Test::C as u8 | Test::D as u8,
//! }
//!
//! // `flags` will be of transparent type `Flags<Test>`
//! let flags : Flags<Test> = Test::A | Test::C;
//!
//! // convert a number to flags using `from_bits()`
//! assert_eq!(flags, Flags::<Test>::from_bits(0b101));
//!
//! // convert flags to a number using `bits()`
//! assert_eq!(0b101, flags.bits());
//!
//! // perform bitwise operations
//! assert_eq!(Test::A | Test::B | Test::C, flags | Test::B);
//! assert_eq!(Test::A, flags & Test::A);
//! assert_eq!(Test::C, flags ^ Test::A);
//!
//! // check which flags are set
//! assert!(flags.contains(Test::A));
//! assert!(!flags.contains(Test::A | Test::B));
//! assert!(flags.intersects(Test::A));
//! assert!(flags.intersects(Test::A | Test::B));
//! ```

use std::ops::*;

/// The `BitFlags` derive macro will implement the `Flags` Trait on your enum and
/// provide convenience methods for bit operations and type conversions.
///
// Re-export derive macro for convenience.
pub use sawp_flags_derive::BitFlags;

/// A primitive numeric type to be used for flag storage.
pub trait Primitive:
    Default
    + BitOr<Self, Output = Self>
    + BitAnd<Self, Output = Self>
    + BitXor<Self, Output = Self>
    + Not<Output = Self>
    + PartialOrd<Self>
    + std::fmt::Debug
    + std::fmt::Binary
    + Copy
    + Clone
{
}

impl Primitive for u8 {}
impl Primitive for u16 {}
impl Primitive for u32 {}
impl Primitive for u64 {}
impl Primitive for u128 {}

/// A trait implemented by all flag enums.
pub trait Flag: Copy + Clone + std::fmt::Debug + std::fmt::Display + 'static {
    /// Associated primitive numeric type
    type Primitive: Primitive;

    /// A list of all flag variants in the enum
    const ITEMS: &'static [Self];

    /// Numeric representation of the variant
    fn bits(self) -> Self::Primitive;

    /// Flag value when no variants are set
    fn none() -> Flags<Self>;

    /// Flag value when all variants are set
    fn all() -> Flags<Self>;
}

/// Storage type for handling flags
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct Flags<Enum, Primitive = <Enum as Flag>::Primitive> {
    val: Primitive,
    marker: std::marker::PhantomData<Enum>,
}

impl<Enum> std::fmt::Debug for Flags<Enum>
where
    Enum: Flag,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.val.fmt(f)
    }
}

impl<Enum> Default for Flags<Enum>
where
    Enum: Flag,
{
    fn default() -> Self {
        Self {
            val: <Enum as Flag>::Primitive::default(),
            marker: std::marker::PhantomData,
        }
    }
}

impl<Enum> Flags<Enum>
where
    Enum: Flag,
{
    /// Get a flag from a single enum value
    pub fn from_flag(flag: Enum) -> Self {
        Self {
            val: flag.bits(),
            marker: std::marker::PhantomData,
        }
    }

    /// Get a flag from a numeric value
    ///
    /// Note: the value is unchecked so any bit may be set. Be
    /// careful because `PartialEq` is a direct comparison of
    /// underlying bits.
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
        Self {
            val: bits,
            marker: std::marker::PhantomData,
        }
    }

    /// Numeric representation of the variant
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
        self.val
    }

    /// Reference to numeric representation of the variant
    pub fn bits_ref(&self) -> &<Enum as Flag>::Primitive {
        &self.val
    }

    /// Check if at least one flag in common is set
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
        (self & rhs.into()).bits() != Enum::none().bits()
    }

    /// Check if all flags provided in `rhs` are set
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
        let rhs = rhs.into();
        (self & rhs).bits() == rhs.bits()
    }

    pub fn is_empty(&self) -> bool {
        self.bits() == <Enum as Flag>::none().bits()
    }

    pub fn is_all(&self) -> bool {
        self.bits() == <Enum as Flag>::all().bits()
    }
}

impl<Enum: Flag> From<Enum> for Flags<Enum> {
    fn from(flag: Enum) -> Self {
        Self::from_flag(flag)
    }
}

impl<Enum: Flag> PartialEq<Enum> for Flags<Enum> {
    fn eq(&self, other: &Enum) -> bool {
        self.bits() == other.bits()
    }
}

impl<T, B> std::ops::BitOr<B> for Flags<T>
where
    T: Flag,
    B: Into<Flags<T>>,
{
    type Output = Flags<T>;
    fn bitor(self, other: B) -> Flags<T> {
        Flags::from_bits(self.bits() | other.into().bits())
    }
}

impl<T, B> std::ops::BitOrAssign<B> for Flags<T>
where
    T: Flag,
    B: Into<Flags<T>>,
{
    fn bitor_assign(&mut self, rhs: B) {
        *self = Flags::from_bits(self.bits() | rhs.into().bits())
    }
}

impl<T, B> std::ops::BitAnd<B> for Flags<T>
where
    T: Flag,
    B: Into<Flags<T>>,
{
    type Output = Flags<T>;
    fn bitand(self, other: B) -> Flags<T> {
        Flags::from_bits(self.bits() & other.into().bits())
    }
}

impl<T, B> std::ops::BitAndAssign<B> for Flags<T>
where
    T: Flag,
    B: Into<Flags<T>>,
{
    fn bitand_assign(&mut self, rhs: B) {
        *self = Flags::from_bits(self.bits() & rhs.into().bits())
    }
}

impl<T, B> std::ops::BitXor<B> for Flags<T>
where
    T: Flag,
    B: Into<Flags<T>>,
{
    type Output = Flags<T>;
    fn bitxor(self, other: B) -> Flags<T> {
        Flags::from_bits(self.bits() ^ other.into().bits())
    }
}

impl<T, B> std::ops::BitXorAssign<B> for Flags<T>
where
    T: Flag,
    B: Into<Flags<T>>,
{
    fn bitxor_assign(&mut self, rhs: B) {
        *self = Flags::from_bits(self.bits() ^ rhs.into().bits())
    }
}

impl<T: Flag> std::ops::Not for Flags<T> {
    type Output = Flags<T>;

    fn not(self) -> Self::Output {
        Flags::from_bits(!self.bits())
    }
}

impl<T: Flag> std::fmt::Display for Flags<T> {
    /// A pipe-separated list of set flags.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let none = self.bits() == T::none().bits();
        let mut first = true;
        for val in <T as Flag>::ITEMS
            .iter()
            .cloned()
            .filter(move |&flag| self.contains(flag))
        {
            write!(f, "{}{:?}", if first { "" } else { " | " }, val)?;
            first = false;

            if none {
                return Ok(());
            }
        }

        if none {
            write!(f, "NONE")?;
        }

        Ok(())
    }
}

impl<T: Flag> std::fmt::Binary for Flags<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Binary::fmt(&self.bits(), f)
    }
}

/// Example enum deriving `BitFlags`
pub mod example {
    use super::*;

    /// Example enum
    #[derive(Debug, Clone, Copy, PartialEq, Eq, BitFlags)]
    #[repr(u8)]
    pub enum Test {
        A = 0b0001,
        B = 0b0010,
        C = 0b0100,
        D = 0b1000,
        /// Variants can be bitmask of other fields
        E = Test::A as u8 | Test::B as u8 | Test::C as u8 | Test::D as u8,
    }
}

#[cfg(test)]
mod test {
    use super::{example::*, *};

    #[test]
    fn test_enum_bits() {
        let bits = 0b1010_1010;
        let flags = Flags::<Test>::from_bits(bits);
        assert_eq!(bits, flags.bits());
        assert_eq!(&bits, flags.bits_ref());
    }

    #[test]
    fn test_enum_or() {
        let mut flags = Test::A | Test::B;
        assert_eq!(0b0011, flags.bits());

        flags |= Test::C;
        assert_eq!(0b0111, flags.bits());

        flags |= Test::C | Test::D;
        assert_eq!(0b1111, flags.bits());
    }

    #[test]
    fn test_enum_and() {
        let mut flags = Test::E & Test::B;
        assert_eq!(0b0010, flags.bits());

        flags &= Test::B;
        assert_eq!(0b0010, flags.bits());

        flags &= Test::E & Test::B;
        assert_eq!(0b0010, flags.bits());
    }

    #[test]
    fn test_enum_xor() {
        let mut flags = Test::A ^ Test::B;
        assert_eq!(0b0011, flags.bits());

        flags ^= Test::C;
        assert_eq!(0b0111, flags.bits());

        flags ^= Test::D ^ Test::B;
        assert_eq!(0b1101, flags.bits());
    }

    #[test]
    fn test_enum_not() {
        let flags = !Test::A;
        assert_eq!(0b1111_1110, flags.bits());
        let flags = !(Test::A ^ Test::B);
        assert_eq!(0b1111_1100, flags.bits());
    }

    #[test]
    fn test_contains() {
        let flags = Test::A | Test::C;
        assert!(flags.contains(Test::A));
        assert!(!flags.contains(Test::B));
        assert!(!flags.contains(Test::E));
        assert!(!flags.contains(Test::B | Test::D));
        assert!(!flags.contains(Test::A | Test::B));
        assert!(flags.contains(Test::A | Test::C));
    }

    #[test]
    fn test_intersects() {
        let flags = Test::A | Test::C;
        assert!(flags.intersects(Test::A));
        assert!(flags.intersects(Test::E));
        assert!(flags.intersects(Test::A | Test::B));
        assert!(flags.intersects(Test::A | Test::C));
        assert!(flags.intersects(Test::A | Test::B | Test::C));
        assert!(!flags.intersects(Test::B | Test::D));
    }

    #[test]
    fn test_eq() {
        let flags = Test::A;
        assert_eq!(flags, Test::A);
        assert_eq!(Test::A, flags);

        let flags = Test::A | Test::C;
        assert_ne!(flags, Test::A);
        assert_ne!(flags, Test::C);
        assert_ne!(Test::A, flags);
        assert_eq!(flags, Test::A | Test::C);
        assert_ne!(flags, Test::A | Test::C | Test::E);

        let flags = Flags::<Test>::from_bits(0b1000_0001);
        assert_ne!(flags, Test::A);
    }

    #[test]
    fn test_enum_string() {
        assert_eq!("NONE", Test::none().to_string());
        assert_eq!("A", Test::A.to_string());
        assert_eq!("A | B", (Test::A | Test::B).to_string());
        assert_eq!("A | B | C | D | E", Test::E.to_string());
        assert_eq!("A | B | C | D | E", Flags::from_flag(Test::E).to_string());
    }

    #[test]
    fn test_enum_string_none() {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, BitFlags)]
        #[repr(u8)]
        pub enum Test {
            Zero = 0b0000,
            A = 0b0001,
            B = 0b0010,
            C = 0b0100,
            D = 0b1000,
            /// Variants can be bitmask of other fields
            E = Test::A as u8 | Test::B as u8 | Test::C as u8 | Test::D as u8,
        }
        assert_eq!("Zero", Test::Zero.to_string());
        assert_eq!("Zero", Test::none().to_string());
        assert_eq!("Zero", Flags::from_flag(Test::Zero).to_string());
    }

    #[test]
    fn test_enum_format() {
        assert_eq!("A", format!("{:?}", Test::A));
        assert_eq!("E", format!("{:?}", Test::E));
        assert_eq!("0", format!("{:?}", Test::none()));

        assert_eq!("0", format!("{:b}", Test::none()));
        assert_eq!("1", format!("{:b}", Test::A));
        assert_eq!("1111", format!("{:b}", Test::E));
    }

    #[test]
    fn test_enum_from_str() {
        use std::str::FromStr;
        assert_eq!(Err(()), Test::from_str(""));
        assert_eq!(Ok(Test::A), Test::from_str("a"));
        assert_eq!(Ok(Test::A), Test::from_str("A"));
    }

    #[test]
    fn test_all() {
        assert_eq!(Test::E, Test::all());
        assert!(!Flags::from_flag(Test::A).is_all());
        assert!(Flags::from_flag(Test::E).is_all());
    }

    #[test]
    fn test_none() {
        assert_eq!(Flags::from_bits(0), Test::none());
        assert!(Flags::<Test>::from_bits(0).is_empty());
        assert!(!Flags::from_flag(Test::A).is_empty());
        assert!(!Flags::from_flag(Test::E).is_empty());
    }
}