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
use std::ops::{BitAnd, BitOr, BitXor};

use crate::style::Attribute;

/// a bitset for all possible attributes
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Attributes(u32);

impl From<Attribute> for Attributes {
    fn from(attribute: Attribute) -> Self {
        Self(attribute.bytes())
    }
}

impl From<&[Attribute]> for Attributes {
    fn from(arr: &[Attribute]) -> Self {
        let mut attributes = Attributes::default();
        for &attr in arr {
            attributes.set(attr);
        }
        attributes
    }
}

impl BitAnd<Attribute> for Attributes {
    type Output = Self;
    fn bitand(self, rhs: Attribute) -> Self {
        Self(self.0 & rhs.bytes())
    }
}
impl BitAnd for Attributes {
    type Output = Self;
    fn bitand(self, rhs: Self) -> Self {
        Self(self.0 & rhs.0)
    }
}

impl BitOr<Attribute> for Attributes {
    type Output = Self;
    fn bitor(self, rhs: Attribute) -> Self {
        Self(self.0 | rhs.bytes())
    }
}
impl BitOr for Attributes {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}

impl BitXor<Attribute> for Attributes {
    type Output = Self;
    fn bitxor(self, rhs: Attribute) -> Self {
        Self(self.0 ^ rhs.bytes())
    }
}
impl BitXor for Attributes {
    type Output = Self;
    fn bitxor(self, rhs: Self) -> Self {
        Self(self.0 ^ rhs.0)
    }
}

impl Attributes {
    /// Returns the empty bitset.
    #[inline(always)]
    pub const fn none() -> Self {
        Self(0)
    }

    /// Returns a copy of the bitset with the given attribute set.
    /// If it's already set, this returns the bitset unmodified.
    #[inline(always)]
    pub const fn with(self, attribute: Attribute) -> Self {
        Self(self.0 | attribute.bytes())
    }

    /// Returns a copy of the bitset with the given attribute unset.
    /// If it's not set, this returns the bitset unmodified.
    #[inline(always)]
    pub const fn without(self, attribute: Attribute) -> Self {
        Self(self.0 & !attribute.bytes())
    }

    /// Sets the attribute.
    /// If it's already set, this does nothing.
    #[inline(always)]
    pub fn set(&mut self, attribute: Attribute) {
        self.0 |= attribute.bytes();
    }

    /// Unsets the attribute.
    /// If it's not set, this changes nothing.
    #[inline(always)]
    pub fn unset(&mut self, attribute: Attribute) {
        self.0 &= !attribute.bytes();
    }

    /// Sets the attribute if it's unset, unset it
    /// if it is set.
    #[inline(always)]
    pub fn toggle(&mut self, attribute: Attribute) {
        self.0 ^= attribute.bytes();
    }

    /// Returns whether the attribute is set.
    #[inline(always)]
    pub const fn has(self, attribute: Attribute) -> bool {
        self.0 & attribute.bytes() != 0
    }

    /// Sets all the passed attributes. Removes none.
    #[inline(always)]
    pub fn extend(&mut self, attributes: Attributes) {
        self.0 |= attributes.0;
    }

    /// Returns whether there is no attribute set.
    #[inline(always)]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }
}

#[cfg(test)]
mod tests {
    use super::{Attribute, Attributes};

    #[test]
    fn test_attributes() {
        let mut attributes: Attributes = Attribute::Bold.into();
        assert!(attributes.has(Attribute::Bold));
        attributes.set(Attribute::Italic);
        assert!(attributes.has(Attribute::Italic));
        attributes.unset(Attribute::Italic);
        assert!(!attributes.has(Attribute::Italic));
        attributes.toggle(Attribute::Bold);
        assert!(attributes.is_empty());
    }

    #[test]
    fn test_attributes_const() {
        const ATTRIBUTES: Attributes = Attributes::none()
            .with(Attribute::Bold)
            .with(Attribute::Italic)
            .without(Attribute::Bold);
        assert!(!ATTRIBUTES.has(Attribute::Bold));
        assert!(ATTRIBUTES.has(Attribute::Italic));
    }
}