Merge branch 'master' of https://github.com/gavofyork/ethcore-util
This commit is contained in:
13
src/error.rs
Normal file
13
src/error.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use rustc_serialize::hex::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum EthcoreError {
|
||||
FromHex(FromHexError),
|
||||
BadSize
|
||||
}
|
||||
|
||||
impl From<FromHexError> for EthcoreError {
|
||||
fn from(err: FromHexError) -> EthcoreError {
|
||||
EthcoreError::FromHex(err)
|
||||
}
|
||||
}
|
||||
36
src/hash.rs
Normal file
36
src/hash.rs
Normal file
@@ -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]));
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
extern crate rustc_serialize;
|
||||
|
||||
pub mod error;
|
||||
pub mod hash;
|
||||
pub mod bytes;
|
||||
pub mod rlp;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user