use crate::json::JsonError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}
#[derive(Debug)]
pub enum ErrorKind {
JsonError(JsonError),
InvalidNumberOfArguments {
found: usize,
},
MalformattedArgument {
index: usize,
},
}
impl From<JsonError> for Error {
fn from(json_err: JsonError) -> Self {
Error::from_kind(ErrorKind::JsonError(json_err))
}
}
impl Error {
fn from_kind(kind: ErrorKind) -> Self {
Error { kind }
}
fn kind(&self) -> &ErrorKind {
&self.kind
}
pub fn invalid_number_of_arguments(found: usize) -> Self {
assert!(found != 1 && found != 2);
Error::from_kind(ErrorKind::InvalidNumberOfArguments { found })
}
pub fn malformatted_argument(index: usize) -> Self {
assert!(index <= 1);
Error::from_kind(ErrorKind::MalformattedArgument { index })
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
match self.kind() {
ErrorKind::JsonError(err) => write!(f, "{}", err),
ErrorKind::InvalidNumberOfArguments { found } => write!(
f,
"found {} arguments passed to eth_abi but expected 1 or 2",
found
),
ErrorKind::MalformattedArgument { index } => write!(
f,
"found non-identifier argument at index {} passed to eth_abi",
index
),
}
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
match self.kind() {
ErrorKind::JsonError(err) => err.description(),
ErrorKind::InvalidNumberOfArguments{ .. } => {
"encountered an invalid number of arguments passed to eth_abi: expected 1 or 2"
},
ErrorKind::MalformattedArgument{ .. } => {
"encountered malformatted argument passed to eth_abi: expected identifier (e.g. `Foo`))"
}
}
}
}