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
//! Error type

#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::string::FromUtf8Error;
#[cfg(feature = "std")]
use std::{io, string::FromUtf8Error};

/// Error type
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum Error {
    /// Checksum fdoes not match expected value
    #[fail(display = "checksum mismatch")]
    ChecksumInvalid,

    /// Data is not encoded correctly
    #[fail(display = "bad encoding")]
    EncodingInvalid,

    /// Error performing I/O operation
    #[cfg(feature = "std")]
    #[fail(display = "I/O error")]
    IoError,

    /// Input or output buffer is an incorrect length
    #[fail(display = "invalid length")]
    LengthInvalid,

    /// Padding missing/invalid
    #[fail(display = "padding invalid")]
    PaddingInvalid,

    /// Trailing whitespace detected
    // TODO: handle trailing whitespace?
    #[fail(display = "trailing whitespace")]
    TrailingWhitespace,
}

/// Assert that the provided condition is true, or else return the given error
macro_rules! ensure {
    ($condition:expr, $err:ident) => {
        if !($condition) {
            Err($err)?;
        }
    };
}

#[cfg(feature = "std")]
impl From<io::Error> for Error {
    fn from(_err: io::Error) -> Error {
        // TODO: preserve cause or error message?
        Error::IoError
    }
}

#[cfg(feature = "alloc")]
impl From<FromUtf8Error> for Error {
    fn from(_err: FromUtf8Error) -> Error {
        // TODO: preserve cause or error message?
        Error::EncodingInvalid
    }
}