Serde 0.9 (#4508)
* Porting json * Dapps * Rpc & Ethstore * New ethabi * Last bunch of fixes * Fixing last test * Removing build script * Adding ethcore-ipc-tests back * Fixing grumbles * Fixing blockchain tests (inference regression?)
This commit is contained in:
committed by
Nikolay Volf
parent
a2c6cd8f7b
commit
f1e99ea2e4
@@ -16,11 +16,12 @@
|
||||
|
||||
//! Lenient bytes json deserialization for test json files.
|
||||
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
use std::ops::Deref;
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use serde::{Deserialize, Deserializer, Error};
|
||||
use serde::de::Visitor;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde::de::{Error, Visitor};
|
||||
|
||||
/// Lenient bytes json deserialization for test json files.
|
||||
#[derive(Default, Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
|
||||
@@ -67,7 +68,7 @@ impl FromStr for Bytes {
|
||||
}
|
||||
|
||||
impl Deserialize for Bytes {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer {
|
||||
deserializer.deserialize(BytesVisitor)
|
||||
}
|
||||
@@ -78,11 +79,15 @@ struct BytesVisitor;
|
||||
impl Visitor for BytesVisitor {
|
||||
type Value = Bytes;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a hex encoded string of bytes")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
Bytes::from_str(value).map_err(Error::custom)
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
self.visit_str(value.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,9 @@
|
||||
//! Lenient hash json deserialization for test json files.
|
||||
|
||||
use std::str::FromStr;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer, Error};
|
||||
use serde::de::Visitor;
|
||||
use std::fmt;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde::de::{Error, Visitor};
|
||||
use rustc_serialize::hex::ToHex;
|
||||
use util::hash::{H64 as Hash64, H160 as Hash160, H256 as Hash256, H520 as Hash520, H2048 as Hash2048};
|
||||
|
||||
@@ -42,7 +43,7 @@ macro_rules! impl_hash {
|
||||
}
|
||||
|
||||
impl Deserialize for $name {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer {
|
||||
|
||||
struct HashVisitor;
|
||||
@@ -50,7 +51,11 @@ macro_rules! impl_hash {
|
||||
impl Visitor for HashVisitor {
|
||||
type Value = $name;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a 0x-prefixed hex-encoded hash")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
let value = match value.len() {
|
||||
0 => $inner::from(0),
|
||||
2 if value == "0x" => $inner::from(0),
|
||||
@@ -65,7 +70,7 @@ macro_rules! impl_hash {
|
||||
Ok($name(value))
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
self.visit_str(value.as_ref())
|
||||
}
|
||||
}
|
||||
@@ -75,7 +80,7 @@ macro_rules! impl_hash {
|
||||
}
|
||||
|
||||
impl Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
let mut hex = "0x".to_owned();
|
||||
hex.push_str(&self.0.to_hex());
|
||||
serializer.serialize_str(&hex)
|
||||
|
||||
@@ -14,15 +14,20 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Json deserialization module.
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![cfg_attr(feature="nightly", feature(custom_derive, custom_attribute, plugin))]
|
||||
#![cfg_attr(feature="nightly", plugin(serde_macros, clippy))]
|
||||
|
||||
#[cfg(feature = "serde_macros")]
|
||||
include!("lib.rs.in");
|
||||
|
||||
#[cfg(not(feature = "serde_macros"))]
|
||||
include!(concat!(env!("OUT_DIR"), "/lib.rs"));
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate ethcore_util as util;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
pub mod hash;
|
||||
pub mod uint;
|
||||
pub mod bytes;
|
||||
pub mod blockchain;
|
||||
pub mod spec;
|
||||
pub mod trie;
|
||||
pub mod vm;
|
||||
pub mod maybe;
|
||||
pub mod state;
|
||||
pub mod transaction;
|
||||
pub mod misc;
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate ethcore_util as util;
|
||||
|
||||
pub mod hash;
|
||||
pub mod uint;
|
||||
pub mod bytes;
|
||||
pub mod blockchain;
|
||||
pub mod spec;
|
||||
pub mod trie;
|
||||
pub mod vm;
|
||||
pub mod maybe;
|
||||
pub mod state;
|
||||
pub mod transaction;
|
||||
pub mod misc;
|
||||
@@ -1,11 +1,11 @@
|
||||
|
||||
//! Deserializer of empty string values into optionals.
|
||||
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use serde::{Deserialize, Deserializer, Error};
|
||||
use serde::de::Visitor;
|
||||
use serde_json::Value;
|
||||
use serde_json::value;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde::de::{Error, Visitor};
|
||||
use serde::de::value::ValueDeserializer;
|
||||
|
||||
/// Deserializer of empty string values into optionals.
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -17,7 +17,7 @@ pub enum MaybeEmpty<T> {
|
||||
}
|
||||
|
||||
impl<T> Deserialize for MaybeEmpty<T> where T: Deserialize {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer {
|
||||
deserializer.deserialize(MaybeEmptyVisitor::new())
|
||||
}
|
||||
@@ -38,16 +38,19 @@ impl<T> MaybeEmptyVisitor<T> {
|
||||
impl<T> Visitor for MaybeEmptyVisitor<T> where T: Deserialize {
|
||||
type Value = MaybeEmpty<T>;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "an empty string or string-encoded type")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
self.visit_string(value.to_owned())
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
match value.is_empty() {
|
||||
true => Ok(MaybeEmpty::None),
|
||||
false => {
|
||||
let value = Value::String(value);
|
||||
T::deserialize(&mut value::Deserializer::new(value)).map(MaybeEmpty::Some).map_err(|_| Error::custom("failed"))
|
||||
T::deserialize(value.into_deserializer()).map(MaybeEmpty::Some)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,12 @@
|
||||
|
||||
//! Trie test input deserialization.
|
||||
|
||||
use std::fmt;
|
||||
use std::collections::BTreeMap;
|
||||
use std::str::FromStr;
|
||||
use bytes::Bytes;
|
||||
use serde::{Deserialize, Deserializer, Error};
|
||||
use serde::de::{Visitor, MapVisitor, SeqVisitor};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde::de::{Error as ErrorTrait, Visitor, MapVisitor, SeqVisitor};
|
||||
|
||||
/// Trie test input.
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -30,7 +31,7 @@ pub struct Input {
|
||||
}
|
||||
|
||||
impl Deserialize for Input {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
deserializer.deserialize(InputVisitor)
|
||||
@@ -42,20 +43,24 @@ struct InputVisitor;
|
||||
impl Visitor for InputVisitor {
|
||||
type Value = Input;
|
||||
|
||||
fn visit_map<V>(&mut self, mut visitor: V) -> Result<Self::Value, V::Error> where V: MapVisitor {
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a map of bytes into bytes")
|
||||
}
|
||||
|
||||
fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where V: MapVisitor {
|
||||
let mut result = BTreeMap::new();
|
||||
|
||||
loop {
|
||||
let key_str: Option<String> = visitor.visit_key()?;
|
||||
let key = match key_str {
|
||||
Some(ref k) if k.starts_with("0x") => Bytes::from_str(k).map_err(Error::custom)?,
|
||||
Some(ref k) if k.starts_with("0x") => Bytes::from_str(k).map_err(V::Error::custom)?,
|
||||
Some(k) => Bytes::new(k.into_bytes()),
|
||||
None => { break; }
|
||||
};
|
||||
|
||||
let val_str: Option<String> = visitor.visit_value()?;
|
||||
let val = match val_str {
|
||||
Some(ref v) if v.starts_with("0x") => Some(Bytes::from_str(v).map_err(Error::custom)?),
|
||||
Some(ref v) if v.starts_with("0x") => Some(Bytes::from_str(v).map_err(V::Error::custom)?),
|
||||
Some(v) => Some(Bytes::new(v.into_bytes())),
|
||||
None => None,
|
||||
};
|
||||
@@ -63,8 +68,6 @@ impl Visitor for InputVisitor {
|
||||
result.insert(key, val);
|
||||
}
|
||||
|
||||
visitor.end()?;
|
||||
|
||||
let input = Input {
|
||||
data: result
|
||||
};
|
||||
@@ -72,7 +75,7 @@ impl Visitor for InputVisitor {
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<Self::Value, V::Error> where V: SeqVisitor {
|
||||
fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where V: SeqVisitor {
|
||||
let mut result = BTreeMap::new();
|
||||
|
||||
loop {
|
||||
@@ -83,20 +86,20 @@ impl Visitor for InputVisitor {
|
||||
};
|
||||
|
||||
if keyval.len() != 2 {
|
||||
return Err(Error::custom("Invalid key value pair."));
|
||||
return Err(V::Error::custom("Invalid key value pair."));
|
||||
}
|
||||
|
||||
let ref key_str: Option<String> = keyval[0];
|
||||
let ref val_str: Option<String> = keyval[1];
|
||||
|
||||
let key = match *key_str {
|
||||
Some(ref k) if k.starts_with("0x") => Bytes::from_str(k).map_err(Error::custom)?,
|
||||
Some(ref k) if k.starts_with("0x") => Bytes::from_str(k).map_err(V::Error::custom)?,
|
||||
Some(ref k) => Bytes::new(k.clone().into_bytes()),
|
||||
None => { break; }
|
||||
};
|
||||
|
||||
let val = match *val_str {
|
||||
Some(ref v) if v.starts_with("0x") => Some(Bytes::from_str(v).map_err(Error::custom)?),
|
||||
Some(ref v) if v.starts_with("0x") => Some(Bytes::from_str(v).map_err(V::Error::custom)?),
|
||||
Some(ref v) => Some(Bytes::new(v.clone().into_bytes())),
|
||||
None => None,
|
||||
};
|
||||
@@ -104,8 +107,6 @@ impl Visitor for InputVisitor {
|
||||
result.insert(key, val);
|
||||
}
|
||||
|
||||
visitor.end()?;
|
||||
|
||||
let input = Input {
|
||||
data: result
|
||||
};
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
|
||||
//! Lenient uint json deserialization for test json files.
|
||||
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
use serde::{Deserialize, Deserializer, Error};
|
||||
use serde::de::Visitor;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde::de::{Error, Visitor};
|
||||
use util::{U256, Uint as U};
|
||||
|
||||
/// Lenient uint json deserialization for test json files.
|
||||
@@ -50,7 +51,7 @@ impl Into<u8> for Uint {
|
||||
}
|
||||
|
||||
impl Deserialize for Uint {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer {
|
||||
deserializer.deserialize(UintVisitor)
|
||||
}
|
||||
@@ -61,11 +62,15 @@ struct UintVisitor;
|
||||
impl Visitor for UintVisitor {
|
||||
type Value = Uint;
|
||||
|
||||
fn visit_u64<E>(&mut self, value: u64) -> Result<Self::Value, E> where E: Error {
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a hex encoded uint")
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> where E: Error {
|
||||
Ok(Uint(U256::from(value)))
|
||||
}
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
let value = match value.len() {
|
||||
0 => U256::from(0),
|
||||
2 if value.starts_with("0x") => U256::from(0),
|
||||
@@ -80,7 +85,7 @@ impl Visitor for UintVisitor {
|
||||
Ok(Uint(value))
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
self.visit_str(value.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user