Skip to content

Commit

Permalink
Move DecodeError to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Trung Nguyen committed Jan 17, 2020
1 parent 453fd02 commit 8573b35
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 51 deletions.
52 changes: 1 addition & 51 deletions w3b-abi/src/decode.rs → w3b-abi/src/decode/decode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{error::Error, fmt};

use w3b_types_abi::{Address, Bytes, Int256, Uint256};
use w3b_types_core::{
hex as hex_general,
Expand All @@ -8,55 +6,7 @@ use w3b_types_core::{

use crate::{param_type::ParamType, token::Token};

#[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::UnexpectedChar {
char,
index,
expected,
} => write!(
f,
"unexpected character {} at index {}, expected {:?}",
char, index, expected,
),

_ => panic!(),
}
}
}

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 }
}
}
use super::error::DecodeError;

#[inline]
pub fn decode(input: &str, types: &[ParamType]) -> Result<Vec<Token>, DecodeError> {
Expand Down
65 changes: 65 additions & 0 deletions w3b-abi/src/decode/error.rs
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 }
}
}
5 changes: 5 additions & 0 deletions w3b-abi/src/decode/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod decode;
mod error;

pub use decode::*;
pub use error::*;

0 comments on commit 8573b35

Please sign in to comment.