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
use bitvec::prelude::*;
use std::fmt;
use std::ops::{Deref, DerefMut};

/// A collection of bits representing either device capability or state.
///
/// This can be used to iterate across all keys supported by a keyboard, or all buttons supported
/// by a joystick. You can also query directly whether a specific bit is set (corresponding to
/// whether a key or button is depressed).
#[repr(transparent)]
pub struct AttributeSetRef<T> {
    _indexer: std::marker::PhantomData<T>,
    bitslice: BitSlice<u8>,
}

impl<T: EvdevEnum> AttributeSetRef<T> {
    #[inline]
    fn new(bitslice: &BitSlice<u8>) -> &Self {
        // SAFETY: for<T> AttributeSet<T> is repr(transparent) over BitSlice<u8>
        unsafe { &*(bitslice as *const BitSlice<u8> as *const Self) }
    }

    #[inline]
    fn new_mut(bitslice: &mut BitSlice<u8>) -> &mut Self {
        // SAFETY: for<T> AttributeSet<T> is repr(transparent) over BitSlice<u8>
        unsafe { &mut *(bitslice as *mut BitSlice<u8> as *mut Self) }
    }

    /// Returns `true` if this AttributeSet contains the passed T.
    #[inline]
    pub fn contains(&self, attr: T) -> bool {
        self.bitslice.get(attr.to_index()).map_or(false, |b| *b)
    }

    /// Provides an iterator over all "set" bits in the collection.
    #[inline]
    pub fn iter(&self) -> AttributeSetRefIter<'_, T> {
        self.into_iter()
    }

    #[inline]
    pub(crate) fn slice(&self, start: T) -> &Self {
        Self::new(&self.bitslice[start.to_index()..])
    }

    pub fn insert(&mut self, attr: T) {
        self.set(attr, true)
    }

    pub fn remove(&mut self, attr: T) {
        self.set(attr, false)
    }

    // TODO: figure out a good name for this if we make it public
    #[inline]
    pub(crate) fn set(&mut self, attr: T, on: bool) {
        self.bitslice.set(attr.to_index(), on)
    }
}

impl<T: EvdevEnum + fmt::Debug> fmt::Debug for AttributeSetRef<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_set().entries(self.iter()).finish()
    }
}

impl<T: EvdevEnum> Default for &AttributeSetRef<T> {
    fn default() -> Self {
        AttributeSetRef::new(BitSlice::empty())
    }
}
impl<T: EvdevEnum> Default for &mut AttributeSetRef<T> {
    fn default() -> Self {
        AttributeSetRef::new_mut(BitSlice::empty_mut())
    }
}

impl<'a, T: EvdevEnum> IntoIterator for &'a AttributeSetRef<T> {
    type Item = T;
    type IntoIter = AttributeSetRefIter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        AttributeSetRefIter {
            _indexer: std::marker::PhantomData,
            inner: self.bitslice.iter_ones(),
        }
    }
}

pub struct AttributeSetRefIter<'a, T> {
    _indexer: std::marker::PhantomData<&'a T>,
    inner: bitvec::slice::IterOnes<'a, u8, Lsb0>,
}

impl<T: EvdevEnum> Iterator for AttributeSetRefIter<'_, T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(T::from_index)
    }
}

impl<T: EvdevEnum> ExactSizeIterator for AttributeSetRefIter<'_, T> {
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<T: EvdevEnum> DoubleEndedIterator for AttributeSetRefIter<'_, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back().map(T::from_index)
    }
}

pub struct AttributeSet<T: ArrayedEvdevEnum> {
    container: T::Array,
}

impl<T: ArrayedEvdevEnum> AttributeSet<T> {
    pub fn new() -> Self {
        Self {
            container: T::zeroed_array(),
        }
    }

    fn as_bitslice(&self) -> &BitSlice<u8> {
        T::array_as_slice(&self.container)
    }

    fn as_mut_bitslice(&mut self) -> &mut BitSlice<u8> {
        T::array_as_slice_mut(&mut self.container)
    }

    #[inline]
    pub(crate) fn as_mut_raw_slice(&mut self) -> &mut [u8] {
        T::array_as_buf(&mut self.container)
    }
}

impl<T: ArrayedEvdevEnum> Default for AttributeSet<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: ArrayedEvdevEnum> FromIterator<T> for AttributeSet<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut set = AttributeSet::default();
        iter.into_iter().for_each(|el| set.insert(el));
        set
    }
}

impl<'a, T: ArrayedEvdevEnum> FromIterator<&'a T> for AttributeSet<T> {
    fn from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Self {
        Self::from_iter(iter.into_iter().copied())
    }
}

impl<T: ArrayedEvdevEnum> Deref for AttributeSet<T> {
    type Target = AttributeSetRef<T>;
    fn deref(&self) -> &AttributeSetRef<T> {
        AttributeSetRef::new(self.as_bitslice())
    }
}

impl<T: ArrayedEvdevEnum> DerefMut for AttributeSet<T> {
    fn deref_mut(&mut self) -> &mut AttributeSetRef<T> {
        AttributeSetRef::new_mut(self.as_mut_bitslice())
    }
}

impl<'a, T: ArrayedEvdevEnum> IntoIterator for &'a AttributeSet<T> {
    type Item = T;
    type IntoIter = AttributeSetRefIter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        (**self).into_iter()
    }
}

impl<T: ArrayedEvdevEnum> Clone for AttributeSet<T>
where
    T::Array: Clone,
{
    fn clone(&self) -> Self {
        Self {
            container: self.container.clone(),
        }
    }
    fn clone_from(&mut self, other: &Self) {
        self.container.clone_from(&other.container)
    }
}

impl<T: ArrayedEvdevEnum + fmt::Debug> fmt::Debug for AttributeSet<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (**self).fmt(f)
    }
}

pub trait EvdevEnum: Copy + 'static {
    fn from_index(i: usize) -> Self;
    fn to_index(self) -> usize;
}

pub trait ArrayedEvdevEnum: EvdevEnum {
    type Array;
    fn array_as_slice(arr: &Self::Array) -> &BitSlice<u8>;
    fn array_as_slice_mut(arr: &mut Self::Array) -> &mut BitSlice<u8>;
    fn array_as_buf(arr: &mut Self::Array) -> &mut [u8];
    fn zeroed_array() -> Self::Array;
}

macro_rules! evdev_enum {
    ($t:ty, Array, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => {
        evdev_enum!(
            $t,
            Array: bitvec::BitArr!(for <$t>::COUNT, in u8),
            bitvec::array::BitArray::as_raw_mut_slice,
            bitvec::array::BitArray::ZERO,
            $($(#[$attr])* $c = $val,)*
        );
    };
    ($t:ty, box Array, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => {
        evdev_enum!(
            $t,
            Array: Box<bitvec::BitArr!(for <$t>::COUNT, in u8)>,
            bitvec::array::BitArray::as_raw_mut_slice,
            Box::new(bitvec::array::BitArray::ZERO),
            $($(#[$attr])* $c = $val,)*
        );
    };
    (
        $t:ty,
        Array: $Array:ty, $arr_as_buf:expr, $zero:expr,
        $($(#[$attr:meta])* $c:ident = $val:expr,)*
    ) => {
        impl $crate::attribute_set::ArrayedEvdevEnum for $t {
            type Array = $Array;
            fn array_as_slice(arr: &Self::Array) -> &bitvec::slice::BitSlice<u8> {
                arr
            }
            fn array_as_slice_mut(arr: &mut Self::Array) -> &mut bitvec::slice::BitSlice<u8> {
                arr
            }
            fn array_as_buf(arr: &mut Self::Array) -> &mut [u8] {
                $arr_as_buf(arr)
            }
            fn zeroed_array() -> Self::Array {
                $zero
            }
        }
        evdev_enum!($t, $($(#[$attr])* $c = $val,)*);
    };
    ($t:ty, $($(#[$attr:meta])* $c:ident = $val:expr,)*) => {
        impl $t {
            $($(#[$attr])* pub const $c: Self = Self($val);)*
        }
        impl std::str::FromStr for $t {
            type Err = crate::EnumParseError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                let map: &[(&'static str, $t)] = &[
                    $((stringify!($c), Self::$c),)*
                ];

                match map.iter().find(|e| e.0 == s) {
                    Some(e) => Ok(e.1),
                    None => Err(crate::EnumParseError(())),
                }
            }
        }
        impl std::fmt::Debug for $t {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                #[allow(unreachable_patterns)]
                match *self {
                    $(Self::$c => f.pad(stringify!($c)),)*
                    _ => write!(f, "unknown key: {}", self.0),
                }
            }
        }
        impl $crate::attribute_set::EvdevEnum for $t {
            #[inline]
            fn from_index(i: usize) -> Self {
                Self(i as _)
            }
            #[inline]
            fn to_index(self) -> usize {
                self.0 as _
            }
        }
        #[cfg(feature = "serde")]
        #[allow(unreachable_patterns)]
        impl serde_1::Serialize for $t {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde_1::ser::Serializer,
            {
                let value = match *self {
                    $(Self::$c => stringify!($c),)*
                    _ => unreachable!(),
                };

                serializer.serialize_str(value)
            }
        }
        #[cfg(feature = "serde")]
        paste::paste! {
            struct [<$t Visitor>];
            impl<'de> serde_1::de::Visitor<'de> for [<$t Visitor>] {
                type Value = $t;

                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    write!(formatter, "a string with any of the constants in {}", stringify!($t))
                }

                fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
                where
                    E: serde_1::de::Error,
                {
                    match s.to_lowercase().as_str() {
                        $(stringify!([<$c:lower>]) => Ok($t::$c),)*
                        _ => Err(serde_1::de::Error::invalid_value(serde_1::de::Unexpected::Str(s), &self)),
                    }
                }
            }
            impl<'de> serde_1::Deserialize<'de> for $t {
                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
                where
                    D: serde_1::de::Deserializer<'de>,
                {
                    deserializer.deserialize_str([<$t Visitor>])
                }
            }
        }
    }
}