docs(core): improve struct level docs

This commit is contained in:
Georgios Konstantopoulos 2020-06-10 10:33:51 +03:00
parent f93b8b6919
commit 6f5bc95543
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
4 changed files with 42 additions and 27 deletions

View File

@ -1,9 +1,11 @@
use std::process::{Child, Command}; use std::process::{Child, Command};
/// A ganache CLI instance. Will close the instance when dropped. /// A ganache CLI instance. Will close the instance when dropped.
pub struct Ganache(Child); ///
/// Construct this using [`Ganache`](./struct.Ganache.html)
pub struct GanacheInstance(Child);
impl Drop for Ganache { impl Drop for GanacheInstance {
fn drop(&mut self) { fn drop(&mut self) {
self.0.kill().expect("could not kill ganache"); self.0.kill().expect("could not kill ganache");
} }
@ -17,13 +19,13 @@ impl Drop for Ganache {
/// ///
/// # Example /// # Example
/// ///
/// ```rust,ignore /// ```no_run
/// use ethers_types::utils::ganache::GanacheBuilder; /// use ethers_core::utils::Ganache;
/// ///
/// let port = 8545u64; /// let port = 8545u64;
/// let url = format!("http://localhost:{}", port).to_string(); /// let url = format!("http://localhost:{}", port).to_string();
/// ///
/// let ganache = GanacheBuilder::new() /// let ganache = Ganache::new()
/// .port(port) /// .port(port)
/// .mnemonic("abstract vacuum mammal awkward pudding scene penalty purchase dinner depart evoke puzzle") /// .mnemonic("abstract vacuum mammal awkward pudding scene penalty purchase dinner depart evoke puzzle")
/// .spawn(); /// .spawn();
@ -31,12 +33,12 @@ impl Drop for Ganache {
/// drop(ganache); // this will kill the instance /// drop(ganache); // this will kill the instance
/// ``` /// ```
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct GanacheBuilder { pub struct Ganache {
port: Option<u64>, port: Option<u64>,
mnemonic: Option<String>, mnemonic: Option<String>,
} }
impl GanacheBuilder { impl Ganache {
/// Creates an empty Ganache builder. /// Creates an empty Ganache builder.
/// The default port is 8545. The mnemonic is chosen randomly. /// The default port is 8545. The mnemonic is chosen randomly.
pub fn new() -> Self { pub fn new() -> Self {
@ -58,7 +60,7 @@ impl GanacheBuilder {
/// Consumes the builder and spawns `ganache-cli` with stdout redirected /// Consumes the builder and spawns `ganache-cli` with stdout redirected
/// to /dev/null. This takes ~2 seconds to execute as it blocks while /// to /dev/null. This takes ~2 seconds to execute as it blocks while
/// waiting for `ganache-cli` to launch. /// waiting for `ganache-cli` to launch.
pub fn spawn(self) -> Ganache { pub fn spawn(self) -> GanacheInstance {
let mut cmd = Command::new("ganache-cli"); let mut cmd = Command::new("ganache-cli");
cmd.stdout(std::process::Stdio::null()); cmd.stdout(std::process::Stdio::null());
if let Some(port) = self.port { if let Some(port) = self.port {
@ -75,6 +77,6 @@ impl GanacheBuilder {
// TODO: Change this to poll for `port` // TODO: Change this to poll for `port`
let sleep_time = std::time::Duration::from_secs(2); let sleep_time = std::time::Duration::from_secs(2);
std::thread::sleep(sleep_time); std::thread::sleep(sleep_time);
Ganache(ganache_pid) GanacheInstance(ganache_pid)
} }
} }

View File

@ -44,7 +44,11 @@ pub fn id<S: AsRef<str>>(signature: S) -> [u8; 4] {
output output
} }
/// Serialize a type. Panics if the type is returns error during serialization. /// Serialize a type.
///
/// # Panics
///
/// If the type returns an error during serialization.
pub fn serialize<T: serde::Serialize>(t: &T) -> serde_json::Value { pub fn serialize<T: serde::Serialize>(t: &T) -> serde_json::Value {
serde_json::to_value(t).expect("Types never fail to serialize.") serde_json::to_value(t).expect("Types never fail to serialize.")
} }

View File

@ -1,6 +1,6 @@
/// Utilities for launching a ganache-cli testnet instance /// Utilities for launching a ganache-cli testnet instance
mod ganache; mod ganache;
pub use ganache::{Ganache, GanacheBuilder}; pub use ganache::Ganache;
/// Solidity compiler bindings /// Solidity compiler bindings
mod solc; mod solc;

View File

@ -1,17 +1,3 @@
//! Solidity Compiler Bindings
//!
//! Assumes that `solc` is installed and available in the caller's $PATH. Any calls
//! will fail otherwise.
//!
//! # Examples
//!
//! ```rust,ignore
//! // Give it a glob
//! let contracts = Solc::new("./contracts/*")
//! .optimizer(200)
//! .build();
//! let contract = contracts.get("SimpleStorage").unwrap();
//! ```
use crate::{abi::Abi, types::Bytes}; use crate::{abi::Abi, types::Bytes};
use glob::glob; use glob::glob;
use rustc_hex::FromHex; use rustc_hex::FromHex;
@ -43,7 +29,30 @@ pub struct CompiledContract {
pub bytecode: Bytes, pub bytecode: Bytes,
} }
/// Solc builder /// Solidity Compiler Bindings
///
/// Assumes that `solc` is installed and available in the caller's $PATH. Any calls
/// will **panic** otherwise.
///
/// By default, it uses 200 optimizer runs and Istanbul as the EVM version
///
/// # Examples
///
/// ```no_run
/// use ethers_core::utils::Solc;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Give it a glob
/// let contracts = Solc::new("./contracts/*")
/// .optimizer(200)
/// .build()?;
///
/// // this will return None if the specified contract did not exist in the compiled
/// // files
/// let contract = contracts.get("SimpleStorage").expect("contract not found");
/// # Ok(())
/// # }
/// ```
pub struct Solc { pub struct Solc {
/// The path where contracts will be read from /// The path where contracts will be read from
pub paths: Vec<String>, pub paths: Vec<String>,
@ -156,7 +165,7 @@ impl Solc {
} }
/// Sets the optimizer runs (default = 200) /// Sets the optimizer runs (default = 200)
pub fn optimizer_runs(mut self, runs: usize) -> Self { pub fn optimizer(mut self, runs: usize) -> Self {
self.optimizer = runs; self.optimizer = runs;
self self
} }