From fca45496f42f248113a6c522336603dc5a70bb9b Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 25 Nov 2015 19:26:40 +0100 Subject: [PATCH] Add hash and error. --- Cargo.lock | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/error.rs | 13 +++++++ src/hash.rs | 36 +++++++++++++++++ src/lib.rs | 7 ++-- 5 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 src/error.rs create mode 100644 src/hash.rs diff --git a/Cargo.lock b/Cargo.lock index 8f6ccf6a8..223c676f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,4 +1,111 @@ [root] name = "ethcore-util" version = "0.1.0" +dependencies = [ + "arrayvec 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "arrayvec" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "odds 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "env_logger" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nodrop" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "odds 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "odds" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unreachable 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rustc-serialize" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unreachable" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "void 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "void" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index f6b0e38a4..0b7032bbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,5 @@ authors = ["Ethcore "] [dependencies] log = "0.3" env_logger = "0.3" +rustc-serialize = "0.3" +arrayvec = "0.3" \ No newline at end of file diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 000000000..9dc471f67 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,13 @@ +use rustc_serialize::hex::*; + +#[derive(Debug)] +pub enum EthcoreError { + FromHex(FromHexError), + BadSize +} + +impl From for EthcoreError { + fn from(err: FromHexError) -> EthcoreError { + EthcoreError::FromHex(err) + } +} diff --git a/src/hash.rs b/src/hash.rs new file mode 100644 index 000000000..295da3900 --- /dev/null +++ b/src/hash.rs @@ -0,0 +1,36 @@ +use rustc_serialize::hex::*; +use error::EthcoreError; +use std::str::FromStr; + +macro_rules! impl_hash { + ($from: ident, $size: expr) => { + #[derive(PartialEq, Debug)] + struct $from ([u8; $size]); + + impl FromStr for $from { +// type Output = $from; + type Err = EthcoreError; + + fn from_str(s: &str) -> Result<$from, EthcoreError> { + let a = try!(s.from_hex()); + if a.len() != $size { return Err(EthcoreError::BadSize); } + let mut ret = $from([0;$size]); + for i in 0..$size { + ret.0[i] = a[i]; + } + Ok(ret) + } + } + } +} + +impl_hash!(Hash64, 8); +impl_hash!(Hash128, 16); +impl_hash!(Address, 20); +impl_hash!(Hash256, 32); +//impl_hash!(Hash512, 64); + +#[test] +fn it_works() { + assert_eq!(Hash64::from_str("0123456789abcdef").unwrap(), Hash64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])); +} diff --git a/src/lib.rs b/src/lib.rs index a93251b65..9aae65008 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ -#[test] -fn it_works() { -} +extern crate rustc_serialize; + +pub mod error; +pub mod hash; \ No newline at end of file