modular_bitfield/
error.rs

1//! Errors that can occure while operating on modular bitfields.
2
3use core::fmt::Debug;
4
5/// The given value was out of range for the bitfield.
6#[derive(Debug, PartialEq, Eq)]
7pub struct OutOfBounds;
8
9impl core::fmt::Display for OutOfBounds {
10    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
11        write!(f, "encountered an out of bounds value")
12    }
13}
14
15/// The bitfield contained an invalid bit pattern.
16#[derive(Debug, PartialEq, Eq)]
17pub struct InvalidBitPattern<Bytes> {
18    pub invalid_bytes: Bytes,
19}
20
21impl<Bytes> core::fmt::Display for InvalidBitPattern<Bytes>
22where
23    Bytes: Debug,
24{
25    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
26        write!(
27            f,
28            "encountered an invalid bit pattern: {:X?}",
29            self.invalid_bytes
30        )
31    }
32}
33
34impl<Bytes> InvalidBitPattern<Bytes> {
35    /// Creates a new invalid bit pattern error.
36    #[inline]
37    pub fn new(invalid_bytes: Bytes) -> Self {
38        Self { invalid_bytes }
39    }
40
41    /// Returns the invalid bit pattern.
42    #[inline]
43    pub fn invalid_bytes(self) -> Bytes {
44        self.invalid_bytes
45    }
46}