diff --git a/w3b-abi/src/decode.rs b/w3b-abi/src/decode/decode.rs similarity index 92% rename from w3b-abi/src/decode.rs rename to w3b-abi/src/decode/decode.rs index c441d7a..3e59473 100644 --- a/w3b-abi/src/decode.rs +++ b/w3b-abi/src/decode/decode.rs @@ -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, @@ -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, - }, - InvalidUtf8 { - valid_up_to: usize, - invalid_size: Option, - }, -} - -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 { - ::fmt(self, f) - } -} - -impl Error for DecodeError {} - -impl From for DecodeError { - #[inline] - fn from(inner: HexError) -> Self { - DecodeError::Hex { inner } - } -} +use super::error::DecodeError; #[inline] pub fn decode(input: &str, types: &[ParamType]) -> Result, DecodeError> { diff --git a/w3b-abi/src/decode/error.rs b/w3b-abi/src/decode/error.rs new file mode 100644 index 0000000..79436bb --- /dev/null +++ b/w3b-abi/src/decode/error.rs @@ -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, + }, + InvalidUtf8 { + valid_up_to: usize, + invalid_size: Option, + }, +} + +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 { + ::fmt(self, f) + } +} + +impl Error for DecodeError {} + +impl From for DecodeError { + #[inline] + fn from(inner: HexError) -> Self { + DecodeError::Hex { inner } + } +} diff --git a/w3b-abi/src/decode/mod.rs b/w3b-abi/src/decode/mod.rs new file mode 100644 index 0000000..1ab5716 --- /dev/null +++ b/w3b-abi/src/decode/mod.rs @@ -0,0 +1,5 @@ +mod decode; +mod error; + +pub use decode::*; +pub use error::*;