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

/**
 * If an integer's range is too small to store an offset, then
 * this error is generated
 */
#[derive(Debug)]
pub struct IntegerDeltaError(pub(crate) IntegerDeltaErrorImpl);

/// All types of errors, this is internal and so protected
/// behind a wrapper struct
#[derive(Debug)]
pub(crate) enum IntegerDeltaErrorImpl {
    /// Failed to convert isize to given integer type
    Conversion(isize),
    
    /// Failed to subtract the two usizes (overflowed isize)
    Sub(usize, usize),

    /// Got a zero when a non-zero value was expected (for `NonZero*`)
    #[cfg(feature = "nightly")]
    InvalidNonZero
}

#[cfg(not(feature = "no_std"))]
impl std::error::Error for IntegerDeltaError {}

mod fmt {
    use super::*;
    use std::fmt;

    impl fmt::Display for IntegerDeltaError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self.0 {
                IntegerDeltaErrorImpl::Conversion(del) => write!(
                    f,
                    "Offset could not be stored (offset of {} is too large)",
                    del
                ),
                IntegerDeltaErrorImpl::Sub(a, b) => {
                    write!(f, "Difference is beween {} and {} overflows `isize`", a, b)
                },
                
                #[cfg(feature = "nightly")]
                IntegerDeltaErrorImpl::InvalidNonZero => {
                    write!(f, "Difference was zero when a `NonZero*` type was specified")
                }
            }
        }
    }
}