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
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::string::FromUtf8Error;
use core::fmt;
#[cfg(feature = "std")]
use std::{io, string::FromUtf8Error};
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Error {
ChecksumInvalid,
EncodingInvalid,
IoError,
LengthInvalid,
PaddingInvalid,
TrailingWhitespace,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let description = match self {
Error::ChecksumInvalid => "checksum mismatch",
Error::EncodingInvalid => "bad encoding",
Error::IoError => "I/O error",
Error::LengthInvalid => "invalid length",
Error::PaddingInvalid => "padding invalid",
Error::TrailingWhitespace => "trailing whitespace",
};
write!(f, "{}", description)
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
macro_rules! ensure {
($condition:expr, $err:path) => {
if !($condition) {
Err($err)?;
}
};
}
#[cfg(feature = "std")]
impl From<io::Error> for Error {
fn from(_err: io::Error) -> Error {
Error::IoError
}
}
#[cfg(feature = "alloc")]
impl From<FromUtf8Error> for Error {
fn from(_err: FromUtf8Error) -> Error {
Error::EncodingInvalid
}
}