1use core::fmt;
6
7use internals::write_err;
8
9use crate::prelude::*;
10
11#[rustfmt::skip] #[doc(inline)]
13pub use crate::parse::ParseIntError;
14
15#[derive(Debug, Clone, Eq, PartialEq)]
18pub enum PrefixedHexError {
19 MissingPrefix(MissingPrefixError),
21 ParseInt(ParseIntError),
23}
24
25impl fmt::Display for PrefixedHexError {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 use PrefixedHexError::*;
28
29 match *self {
30 MissingPrefix(ref e) => write_err!(f, "hex string is missing prefix"; e),
31 ParseInt(ref e) => write_err!(f, "prefixed hex string invalid int"; e),
32 }
33 }
34}
35
36#[cfg(feature = "std")]
37impl std::error::Error for PrefixedHexError {
38 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39 use PrefixedHexError::*;
40
41 match *self {
42 MissingPrefix(ref e) => Some(e),
43 ParseInt(ref e) => Some(e),
44 }
45 }
46}
47
48impl From<MissingPrefixError> for PrefixedHexError {
49 fn from(e: MissingPrefixError) -> Self { Self::MissingPrefix(e) }
50}
51
52impl From<ParseIntError> for PrefixedHexError {
53 fn from(e: ParseIntError) -> Self { Self::ParseInt(e) }
54}
55
56#[derive(Debug, Clone, Eq, PartialEq)]
58pub enum UnprefixedHexError {
59 ContainsPrefix(ContainsPrefixError),
61 ParseInt(ParseIntError),
63}
64
65impl fmt::Display for UnprefixedHexError {
66 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67 use UnprefixedHexError::*;
68
69 match *self {
70 ContainsPrefix(ref e) => write_err!(f, "hex string is contains prefix"; e),
71 ParseInt(ref e) => write_err!(f, "hex string parse int"; e),
72 }
73 }
74}
75
76#[cfg(feature = "std")]
77impl std::error::Error for UnprefixedHexError {
78 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
79 use UnprefixedHexError::*;
80
81 match *self {
82 ContainsPrefix(ref e) => Some(e),
83 ParseInt(ref e) => Some(e),
84 }
85 }
86}
87
88impl From<ContainsPrefixError> for UnprefixedHexError {
89 fn from(e: ContainsPrefixError) -> Self { Self::ContainsPrefix(e) }
90}
91
92impl From<ParseIntError> for UnprefixedHexError {
93 fn from(e: ParseIntError) -> Self { Self::ParseInt(e) }
94}
95
96#[derive(Debug, Clone, Eq, PartialEq)]
98pub struct MissingPrefixError {
99 hex: String,
100}
101
102impl MissingPrefixError {
103 pub(crate) fn new(s: &str) -> Self { Self { hex: s.into() } }
104}
105
106impl fmt::Display for MissingPrefixError {
107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108 write!(f, "hex string is missing a prefix (e.g. 0x): {}", self.hex)
109 }
110}
111
112#[cfg(feature = "std")]
113impl std::error::Error for MissingPrefixError {}
114
115#[derive(Debug, Clone, Eq, PartialEq)]
117pub struct ContainsPrefixError {
118 hex: String,
119}
120
121impl ContainsPrefixError {
122 pub(crate) fn new(s: &str) -> Self { Self { hex: s.into() } }
123}
124
125impl fmt::Display for ContainsPrefixError {
126 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127 write!(f, "hex string contains a prefix: {}", self.hex)
128 }
129}
130
131#[cfg(feature = "std")]
132impl std::error::Error for ContainsPrefixError {}