diff --git a/ethers-contract/README.md b/ethers-contract/README.md index b91644f21..61a8a2f23 100644 --- a/ethers-contract/README.md +++ b/ethers-contract/README.md @@ -1,4 +1,4 @@ -# ethers-core +# ethers-contract Type-safe abstractions for interacting with Ethereum smart contracts. diff --git a/ethers-contract/src/base.rs b/ethers-contract/src/base.rs index be648b523..920c2c6fb 100644 --- a/ethers-contract/src/base.rs +++ b/ethers-contract/src/base.rs @@ -1,12 +1,10 @@ -use crate::contract::ContractInstance; - +use crate::ContractInstance; pub use ethers_core::abi::AbiError; use ethers_core::{ abi::{Abi, Detokenize, Error, Event, Function, FunctionExt, RawLog, Token, Tokenize}, types::{Address, Bytes, Selector, H256}, }; use ethers_providers::Middleware; - use std::{ borrow::Borrow, collections::{BTreeMap, HashMap}, diff --git a/ethers-contract/src/lib.rs b/ethers-contract/src/lib.rs index 4d4058d9f..07fb72b2d 100644 --- a/ethers-contract/src/lib.rs +++ b/ethers-contract/src/lib.rs @@ -3,8 +3,9 @@ #![deny(unsafe_code)] #![warn(missing_docs)] -mod contract; -pub use contract::{Contract, ContractInstance}; +#[path = "contract.rs"] +mod _contract; +pub use _contract::{Contract, ContractInstance}; mod base; pub use base::{decode_function_data, encode_function_data, AbiError, BaseContract}; @@ -67,3 +68,32 @@ pub use once_cell::sync::Lazy; #[cfg(feature = "eip712")] pub use ethers_derive_eip712::*; + +// For Abigen expansions in docs.rs builds. + +#[doc(hidden)] +#[allow(unused_extern_crates)] +extern crate self as ethers_contract; + +#[doc(hidden)] +#[allow(unused_extern_crates)] +#[cfg(docsrs)] +extern crate self as ethers; + +#[doc(hidden)] +#[cfg(docsrs)] +pub mod core { + pub use ethers_core::*; +} + +#[doc(hidden)] +#[cfg(docsrs)] +pub mod contract { + pub use crate::*; +} + +#[doc(hidden)] +#[cfg(docsrs)] +pub mod providers { + pub use ethers_providers::*; +} diff --git a/ethers-core/src/macros/ethers_crate.rs b/ethers-core/src/macros/ethers_crate.rs index 5e984c23f..d1f8b2637 100644 --- a/ethers-core/src/macros/ethers_crate.rs +++ b/ethers-core/src/macros/ethers_crate.rs @@ -56,10 +56,13 @@ pub struct ProjectEnvironment { } impl ProjectEnvironment { + /// Creates a new instance using the given manifest dir and crate name. pub fn new, U: Into>(manifest_dir: T, crate_name: U) -> Self { Self { manifest_dir: manifest_dir.into(), crate_name: Some(crate_name.into()) } } + /// Creates a new instance using the the `CARGO_MANIFEST_DIR` and `CARGO_CRATE_NAME` environment + /// variables. pub fn new_from_env() -> Option { Some(Self { manifest_dir: env::var_os("CARGO_MANIFEST_DIR")?.into(), @@ -90,29 +93,15 @@ impl ProjectEnvironment { } #[inline] - pub fn crate_names_from_metadata(&self) -> Option { + fn crate_names_from_metadata(&self) -> Option { let metadata = MetadataCommand::new().current_dir(&self.manifest_dir).exec().ok()?; let pkg = metadata.root_package()?; // return ethers_* if the root package is an internal ethers crate since `ethers` is not // available - let crate_is_root = self.is_crate_root(); - if let Ok(current_pkg) = pkg.name.parse::() { - // replace `current_pkg`'s name with "crate" - let names = - EthersCrate::path_names() - .map(|(pkg, name)| { - if crate_is_root && pkg == current_pkg { - (pkg, "crate") - } else { - (pkg, name) - } - }) - .collect(); - return Some(names) - } /* else if pkg.name == "ethers" { - // should not happen (the root package the `ethers` workspace package itself) - } */ + if pkg.name.parse::().is_ok() || pkg.name == "ethers" { + return Some(EthersCrate::path_names().collect()) + } let mut names: CrateNames = EthersCrate::ethers_path_names().collect(); for dep in pkg.dependencies.iter() { @@ -293,7 +282,7 @@ impl EthersCrate { pub const fn ethers_path_name(self) -> &'static str { match self { // re-exported in ethers::contract - Self::EthersContractAbigen => "::ethers::contract", // partly + Self::EthersContractAbigen => "::ethers::contract", // partially Self::EthersContractDerive => "::ethers::contract", Self::EthersDeriveEip712 => "::ethers::contract", @@ -386,7 +375,7 @@ mod tests { let krate = s.crate_name.as_ref().and_then(|x| x.parse::().ok()); let is_internal = krate.is_some(); - let mut expected: CrateNames = match (is_internal, ethers) { + let expected: CrateNames = match (is_internal, ethers) { // internal (true, _) => EthersCrate::path_names().collect(), @@ -403,10 +392,6 @@ mod tests { } }; - if is_internal { - expected.insert(krate.unwrap(), "crate"); - } - // don't use assert for a better custom message if names != expected { // BTreeMap sorts the keys diff --git a/src/lib.rs b/src/lib.rs index 5fd61c2b1..59fc95199 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,11 @@ #![deny(rustdoc::broken_intra_doc_links)] #![doc(test(no_crate_inject, attr(deny(rust_2018_idioms), allow(dead_code, unused_variables))))] +// For macro expansion +#[doc(hidden)] +#[allow(unused_extern_crates)] +extern crate self as ethers; + #[doc(inline)] pub use ethers_addressbook as addressbook; #[doc(inline)]