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
#![cfg(feature = "alloc")]
use alloc::{format, string::String, string::ToString};
#[derive(Debug, PartialEq)]
pub enum DekuError {
Parse(String),
InvalidParam(String),
}
impl From<core::num::TryFromIntError> for DekuError {
fn from(e: core::num::TryFromIntError) -> DekuError {
DekuError::Parse(format!("error parsing int: {}", e.to_string()))
}
}
impl From<core::array::TryFromSliceError> for DekuError {
fn from(e: core::array::TryFromSliceError) -> DekuError {
DekuError::Parse(format!("error parsing from slice: {}", e.to_string()))
}
}
impl From<core::convert::Infallible> for DekuError {
fn from(_e: core::convert::Infallible) -> DekuError {
unreachable!();
}
}
impl core::fmt::Display for DekuError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match *self {
DekuError::Parse(ref err) => write!(f, "Parse error: {}", err),
DekuError::InvalidParam(ref err) => write!(f, "Invalid param error: {}", err),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DekuError {
fn cause(&self) -> Option<&dyn std::error::Error> {
Some(self)
}
}