Remove unnecessary unwraps in json_aid.

This commit is contained in:
Gav Wood 2016-01-14 22:16:41 +01:00
parent 5e6632ca47
commit d2cc3b9b5b

View File

@ -18,27 +18,31 @@ fn u256_from_str(s: &str) -> U256 {
impl FromJson for Bytes { impl FromJson for Bytes {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
let s = json.as_string().unwrap_or(""); match json {
if s.len() % 2 == 1 { &Json::String(ref s) => match s.len() % 2 {
FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![]) 0 => FromHex::from_hex(clean(s)).unwrap_or(vec![]),
} else { _ => FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![]),
FromHex::from_hex(clean(s)).unwrap_or(vec![]) },
_ => vec![],
} }
} }
} }
impl FromJson for BTreeMap<H256, H256> { impl FromJson for BTreeMap<H256, H256> {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut m, (key, value)| { match json {
m.insert(x!(&u256_from_str(key)), x!(&U256::from_json(value))); &Json::Object(ref o) => o.iter().map(|(key, value)| (x!(&u256_from_str(key)), x!(&U256::from_json(value)))).collect(),
m _ => BTreeMap::new(),
}) }
} }
} }
impl<T> FromJson for Vec<T> where T: FromJson { impl<T> FromJson for Vec<T> where T: FromJson {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
json.as_array().unwrap().iter().map(|x|T::from_json(x)).collect() match json {
&Json::Array(ref o) => o.iter().map(|x|T::from_json(x)).collect(),
_ => Vec::new(),
}
} }
} }