-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Trung Nguyen
committed
Jan 17, 2020
1 parent
453fd02
commit 8573b35
Showing
3 changed files
with
71 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::{error::Error, fmt}; | ||
|
||
use w3b_types_core::hex::HexError; | ||
|
||
#[derive(PartialEq)] | ||
pub enum DecodeError { | ||
Hex { | ||
inner: HexError, | ||
}, | ||
UnexpectedChar { | ||
char: char, | ||
index: usize, | ||
expected: Vec<char>, | ||
}, | ||
InvalidUtf8 { | ||
valid_up_to: usize, | ||
invalid_size: Option<usize>, | ||
}, | ||
} | ||
|
||
impl fmt::Debug for DecodeError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
DecodeError::Hex { inner } => inner.fmt(f), | ||
|
||
DecodeError::UnexpectedChar { | ||
char, | ||
index, | ||
expected, | ||
} => write!( | ||
f, | ||
"unexpected character {} at index {}, expected {:?}", | ||
char, index, expected, | ||
), | ||
|
||
DecodeError::InvalidUtf8 { | ||
valid_up_to, | ||
invalid_size, | ||
} => write!( | ||
f, | ||
"invalid UTF-8 bytes (valid up to {}{})", | ||
valid_up_to, | ||
invalid_size | ||
.map(|invalid_size| format!(", invalid size {}", invalid_size)) | ||
.unwrap_or_default(), | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for DecodeError { | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
<Self as fmt::Debug>::fmt(self, f) | ||
} | ||
} | ||
|
||
impl Error for DecodeError {} | ||
|
||
impl From<HexError> for DecodeError { | ||
#[inline] | ||
fn from(inner: HexError) -> Self { | ||
DecodeError::Hex { inner } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod decode; | ||
mod error; | ||
|
||
pub use decode::*; | ||
pub use error::*; |