Skip to content

Commit

Permalink
Remove anyhow in crates intended to be libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r authored and vkgnosis committed Oct 4, 2021
1 parent 959f0f3 commit d862110
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 29 deletions.
1 change: 0 additions & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ edition = "2018"
proc-macro = true

[dependencies]
anyhow = "1"
ethabi = { path = "../ethabi", version = "15.0.0" }
heck = "0.3.1"
syn = { version = "1.0.13", default-features = false, features = ["derive", "parsing", "printing", "proc-macro"] }
Expand Down
19 changes: 10 additions & 9 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ mod contract;
mod event;
mod function;

use anyhow::anyhow;
use ethabi::{Contract, Param, ParamType, Result};
use ethabi::{Contract, Error, Param, ParamType, Result};
use heck::SnakeCase;
use proc_macro2::Span;
use quote::quote;
use std::{env, fs, path::PathBuf};
use std::{borrow::Cow, env, fs, path::PathBuf};

const ERROR_MSG: &str = "`derive(EthabiContract)` failed";

Expand All @@ -35,8 +34,9 @@ fn impl_ethabi_derive(ast: &syn::DeriveInput) -> Result<proc_macro2::TokenStream
let options = get_options(&ast.attrs, "ethabi_contract_options")?;
let path = get_option(&options, "path")?;
let normalized_path = normalize_path(&path)?;
let source_file = fs::File::open(&normalized_path)
.map_err(|_| anyhow!("Cannot load contract abi from `{}`", normalized_path.display()))?;
let source_file = fs::File::open(&normalized_path).map_err(|_| {
Error::Other(Cow::Owned(format!("Cannot load contract abi from `{}`", normalized_path.display())))
})?;
let contract = Contract::load(source_file)?;
let c = contract::Contract::from(&contract);
Ok(c.generate())
Expand All @@ -47,7 +47,7 @@ fn get_options(attrs: &[syn::Attribute], name: &str) -> Result<Vec<syn::NestedMe

match options {
Some(syn::Meta::List(list)) => Ok(list.nested.into_iter().collect()),
_ => Err(anyhow!("Unexpected meta item").into()),
_ => Err(Error::Other(Cow::Borrowed("Unexpected meta item"))),
}
}

Expand All @@ -59,7 +59,7 @@ fn get_option(options: &[syn::NestedMeta], name: &str) -> Result<String> {
_ => None,
})
.find(|meta| meta.path().is_ident(name))
.ok_or_else(|| anyhow!("Expected to find option {}", name))?;
.ok_or_else(|| Error::Other(Cow::Owned(format!("Expected to find option {}", name))))?;

str_value_of_meta_item(item, name)
}
Expand All @@ -71,12 +71,13 @@ fn str_value_of_meta_item(item: &syn::Meta, name: &str) -> Result<String> {
}
}

Err(anyhow!(r#"`{}` must be in the form `#[{}="something"]`"#, name, name).into())
Err(Error::Other(Cow::Owned(format!(r#"`{}` must be in the form `#[{}="something"]`"#, name, name))))
}

fn normalize_path(relative_path: &str) -> Result<PathBuf> {
// workaround for https://github.com/rust-lang/rust/issues/43860
let cargo_toml_directory = env::var("CARGO_MANIFEST_DIR").map_err(|_| anyhow!("Cannot find manifest file"))?;
let cargo_toml_directory =
env::var("CARGO_MANIFEST_DIR").map_err(|_| Error::Other(Cow::Borrowed("Cannot find manifest file")))?;
let mut path: PathBuf = cargo_toml_directory.into();
path.push(relative_path);
Ok(path)
Expand Down
3 changes: 0 additions & 3 deletions ethabi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ description = "Easy to use conversion of ethereum contract calls to bytecode."
edition = "2018"

[dependencies]
anyhow = { version = "1", optional = true }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = { version = "1.0", optional = true }
Expand All @@ -33,7 +32,6 @@ default = [
"rlp",
]
std = [
"anyhow/std",
"hex/std",
"sha3/std",
"ethereum-types/std",
Expand All @@ -44,7 +42,6 @@ std = [
# To enable custom `Reader`/`Tokenizer` and `serde` features support
full-serde = [
"std",
"anyhow",
"serde",
"serde_json",
"uint",
Expand Down
19 changes: 7 additions & 12 deletions ethabi/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::no_std_prelude::Cow;
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
#[cfg(feature = "full-serde")]
use core::num;

#[cfg(feature = "full-serde")]
use anyhow::anyhow;
#[cfg(feature = "std")]
use thiserror::Error;

#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;

/// Ethabi result type
pub type Result<T> = core::result::Result<T, Error>;

Expand All @@ -43,19 +40,17 @@ pub enum Error {
#[error("Hex parsing error: {0}")]
Hex(#[from] hex::FromHexError),
/// Other errors.
#[cfg(feature = "full-serde")]
#[error("{0}")]
Other(#[from] anyhow::Error),
#[cfg_attr(feature = "std", error("{0}"))]
Other(Cow<'static, str>),
}

#[cfg(feature = "full-serde")]
impl From<uint::FromDecStrErr> for Error {
fn from(err: uint::FromDecStrErr) -> Self {
use uint::FromDecStrErr::*;
match err {
InvalidCharacter => anyhow!("Uint parse error: InvalidCharacter"),
InvalidLength => anyhow!("Uint parse error: InvalidLength"),
InvalidCharacter => Self::Other(Cow::Borrowed("Uint parse error: InvalidCharacter")),
InvalidLength => Self::Other(Cow::Borrowed("Uint parse error: InvalidLength")),
}
.into()
}
}
6 changes: 5 additions & 1 deletion ethabi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ extern crate alloc;
#[cfg(not(feature = "std"))]
mod no_std_prelude {
pub use alloc::{
borrow::ToOwned,
borrow::{Cow, ToOwned},
boxed::Box,
string::{self, String, ToString},
vec::Vec,
};
}
#[cfg(feature = "std")]
mod no_std_prelude {
pub use std::borrow::Cow;
}
#[cfg(not(feature = "std"))]
use no_std_prelude::*;

Expand Down
6 changes: 3 additions & 3 deletions ethabi/src/token/lenient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
token::{StrictTokenizer, Tokenizer},
Uint,
};
use anyhow::anyhow;
use std::borrow::Cow;

/// Tries to parse string as a token. Does not require string to clearly represent the value.
pub struct LenientTokenizer;
Expand Down Expand Up @@ -62,12 +62,12 @@ impl Tokenizer for LenientTokenizer {
if abs.is_zero() {
return Ok(abs.into());
} else if abs > max + 1 {
return Err(anyhow!("int256 parse error: Underflow").into());
return Err(Error::Other(Cow::Borrowed("int256 parse error: Underflow")));
}
!abs + 1 // two's complement
} else {
if abs > max {
return Err(anyhow!("int256 parse error: Overflow").into());
return Err(Error::Other(Cow::Borrowed("int256 parse error: Overflow")));
}
abs
};
Expand Down

0 comments on commit d862110

Please sign in to comment.