From 5e6632ca47d5937cf8a88d09b928f9f428c312f1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 21:58:55 +0100 Subject: [PATCH] Remove cruft --- src/json_aid.rs | 69 +++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 54 deletions(-) diff --git a/src/json_aid.rs b/src/json_aid.rs index 570f466bc..6c9925ffc 100644 --- a/src/json_aid.rs +++ b/src/json_aid.rs @@ -8,7 +8,7 @@ pub fn clean(s: &str) -> &str { } } -pub fn u256_from_str(s: &str) -> U256 { +fn u256_from_str(s: &str) -> U256 { if s.len() >= 2 && &s[0..2] == "0x" { U256::from_str(&s[2..]).unwrap_or(U256::from(0)) } else { @@ -16,59 +16,26 @@ pub fn u256_from_str(s: &str) -> U256 { } } -pub fn bytes_from_json(json: &Json) -> Bytes { - let s = json.as_string().unwrap_or(""); - if s.len() % 2 == 1 { - FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![]) - } else { - FromHex::from_hex(clean(s)).unwrap_or(vec![]) +impl FromJson for Bytes { + fn from_json(json: &Json) -> Self { + let s = json.as_string().unwrap_or(""); + if s.len() % 2 == 1 { + FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![]) + } else { + FromHex::from_hex(clean(s)).unwrap_or(vec![]) + } } } -pub fn address_from_json(json: &Json) -> Address { - From::from(json.as_string().unwrap_or("0000000000000000000000000000000000000000")) -} - -pub fn h256_from_json(json: &Json) -> H256 { - let s = json.as_string().unwrap_or("0000000000000000000000000000000000000000000000000000000000000000"); - if s.len() % 2 == 1 { - h256_from_hex(&("0".to_string() + &(clean(s).to_string()))[..]) - } else { - h256_from_hex(clean(s)) +impl FromJson for BTreeMap { + fn from_json(json: &Json) -> Self { + json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut m, (key, value)| { + m.insert(x!(&u256_from_str(key)), x!(&U256::from_json(value))); + m + }) } } -pub fn vec_h256_from_json(json: &Json) -> Vec { - json.as_array().unwrap().iter().map(&h256_from_json).collect() -} - -pub fn map_h256_h256_from_json(json: &Json) -> BTreeMap { - json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut m, (key, value)| { - m.insert(H256::from(&u256_from_str(key)), H256::from(&U256::from_json(value))); - m - }) -} - -pub fn usize_from_json(json: &Json) -> usize { - U256::from_json(json).low_u64() as usize -} - -pub fn u64_from_json(json: &Json) -> u64 { - U256::from_json(json).low_u64() -} - -pub fn u32_from_json(json: &Json) -> u32 { - U256::from_json(json).low_u32() -} - -pub fn u16_from_json(json: &Json) -> u16 { - U256::from_json(json).low_u32() as u16 -} - -pub fn u8_from_json(json: &Json) -> u8 { - U256::from_json(json).low_u32() as u8 -} - impl FromJson for Vec where T: FromJson { fn from_json(json: &Json) -> Self { json.as_array().unwrap().iter().map(|x|T::from_json(x)).collect() @@ -93,12 +60,6 @@ impl FromJson for u16 { } } -impl FromJson for u8 { - fn from_json(json: &Json) -> Self { - U256::from_json(json).low_u64() as u8 - } -} - #[test] fn u256_from_json() { let j = Json::from_str("{ \"dec\": \"10\", \"hex\": \"0x0a\", \"int\": 10 }").unwrap();