Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #48 from gavofyork/gav
Browse files Browse the repository at this point in the history
from_existing works for an empty DB if passed empty RLP SHA3; Option types in JSON.
  • Loading branch information
debris committed Jan 14, 2016
2 parents ca3413b + 6a7c823 commit 1ebd14b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ macro_rules! impl_hash {
fn from_json(json: &Json) -> Self {
match json {
&Json::String(ref s) => {
println!("s: {}", s);
match s.len() % 2 {
0 => FromStr::from_str(clean_0x(s)).unwrap(),
_ => FromStr::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap()
Expand Down
38 changes: 36 additions & 2 deletions src/json_aid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ impl<T> FromJson for Vec<T> where T: FromJson {
}
}

impl<T> FromJson for Option<T> where T: FromJson {
fn from_json(json: &Json) -> Self {
match json {
&Json::String(ref o) if o.is_empty() => None,
&Json::Null => None,
_ => Some(FromJson::from_json(json)),
}
}
}

impl FromJson for u64 {
fn from_json(json: &Json) -> Self {
U256::from_json(json).low_u64()
Expand Down Expand Up @@ -77,7 +87,7 @@ fn u256_from_json() {
}

#[test]
fn h256_from_json_() {
fn h256_from_json() {
let j = Json::from_str("{ \"with\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"without\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\" }").unwrap();

let v: H256 = xjson!(&j["with"]);
Expand All @@ -95,9 +105,33 @@ fn vec_u256_from_json() {
}

#[test]
fn vec_h256_from_json_() {
fn vec_h256_from_json() {
let j = Json::from_str("{ \"array\": [ \"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\"] }").unwrap();

let v: Vec<H256> = xjson!(&j["array"]);
assert_eq!(vec![H256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap(); 2], v);
}

#[test]
fn simple_types() {
let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap();
let v: u16 = xjson!(&j["int"]);
assert_eq!(42u16, v);
let v: u32 = xjson!(&j["dec"]);
assert_eq!(42u32, v);
let v: u64 = xjson!(&j["hex"]);
assert_eq!(42u64, v);
}

#[test]
fn option_types() {
let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap();
let v: Option<u16> = xjson!(&j["int"]);
assert_eq!(Some(42u16), v);
let v: Option<u16> = xjson!(&j["dec"]);
assert_eq!(Some(42u16), v);
let v: Option<u16> = xjson!(&j["null"]);
assert_eq!(None, v);
let v: Option<u16> = xjson!(&j["empty"]);
assert_eq!(None, v);
}
3 changes: 3 additions & 0 deletions src/trie/triedbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ impl<'db> TrieDBMut<'db> {
/// Create a new trie with the backing database `db` and `root`
/// Panics, if `root` does not exist
pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self {
if !db.exists(root) && root == &SHA3_NULL_RLP {
*root = db.insert(&NULL_RLP);
}
assert!(db.exists(root));
TrieDBMut {
db: db,
Expand Down

0 comments on commit 1ebd14b

Please sign in to comment.