util cleanup (#1474)
* removed old json-tests * simplify folds in triehash.rs * removed unused json_aid * removed unused squeeze.rs * json branching tests for trie * loading trie consensus tests
This commit is contained in:
@@ -16,15 +16,23 @@
|
||||
|
||||
//! Lenient bytes json deserialization for test json files.
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::ops::Deref;
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use serde::{Deserialize, Deserializer, Error};
|
||||
use serde::de::Visitor;
|
||||
use std::ops::Deref;
|
||||
|
||||
/// Lenient bytes json deserialization for test json files.
|
||||
#[derive(Default, Debug, PartialEq, Clone)]
|
||||
#[derive(Default, Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
|
||||
pub struct Bytes(Vec<u8>);
|
||||
|
||||
impl Bytes {
|
||||
/// Creates bytes struct.
|
||||
pub fn new(v: Vec<u8>) -> Self {
|
||||
Bytes(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Vec<u8>> for Bytes {
|
||||
fn into(self) -> Vec<u8> {
|
||||
self.0
|
||||
@@ -39,6 +47,25 @@ impl Deref for Bytes {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Bytes {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
let v = match value.len() {
|
||||
0 => vec![],
|
||||
2 if value.starts_with("0x") => vec![],
|
||||
_ if value.starts_with("0x") && value.len() % 2 == 1 => {
|
||||
let v = "0".to_owned() + &value[2..];
|
||||
FromHex::from_hex(v.as_ref() as &str).unwrap_or(vec![]),
|
||||
},
|
||||
_ if value.starts_with("0x") => FromHex::from_hex(&value[2..]).unwrap_or(vec![]),
|
||||
_ => FromHex::from_hex(value).unwrap_or(vec![]),
|
||||
};
|
||||
|
||||
Ok(Bytes(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for Bytes {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer {
|
||||
@@ -52,17 +79,7 @@ impl Visitor for BytesVisitor {
|
||||
type Value = Bytes;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
let v = match value.len() {
|
||||
0 => vec![],
|
||||
2 if value.starts_with("0x") => vec![],
|
||||
_ if value.starts_with("0x") && value.len() % 2 == 1 => {
|
||||
let v = "0".to_owned() + &value[2..];
|
||||
FromHex::from_hex(v.as_ref() as &str).unwrap_or(vec![]),
|
||||
},
|
||||
_ if value.starts_with("0x") => FromHex::from_hex(&value[2..]).unwrap_or(vec![]),
|
||||
_ => FromHex::from_hex(value).unwrap_or(vec![]),
|
||||
};
|
||||
Ok(Bytes(v))
|
||||
Bytes::from_str(value).map_err(Error::custom)
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
|
||||
Reference in New Issue
Block a user