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
//! Errors that can occure while operating on modular bitfields.

use core::fmt::Debug;

/// The given value was out of range for the bitfield.
#[derive(Debug, PartialEq, Eq)]
pub struct OutOfBounds;

impl core::fmt::Display for OutOfBounds {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "encountered an out of bounds value")
    }
}

/// The bitfield contained an invalid bit pattern.
#[derive(Debug, PartialEq, Eq)]
pub struct InvalidBitPattern<Bytes> {
    pub invalid_bytes: Bytes,
}

impl<Bytes> core::fmt::Display for InvalidBitPattern<Bytes>
where
    Bytes: Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(
            f,
            "encountered an invalid bit pattern: {:X?}",
            self.invalid_bytes
        )
    }
}

impl<Bytes> InvalidBitPattern<Bytes> {
    /// Creates a new invalid bit pattern error.
    #[inline]
    pub fn new(invalid_bytes: Bytes) -> Self {
        Self { invalid_bytes }
    }

    /// Returns the invalid bit pattern.
    #[inline]
    pub fn invalid_bytes(self) -> Bytes {
        self.invalid_bytes
    }
}