Update serde to 1.0
This commit is contained in:
parent
e69fedb35a
commit
76d160344b
|
@ -31,6 +31,6 @@ clippy = {version = "0.0", optional = true}
|
||||||
rand = "0.3"
|
rand = "0.3"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
rustc-serialize = "0.3"
|
rustc-serialize = "0.3"
|
||||||
serde = "0.6"
|
serde = "1.0"
|
||||||
serde_json = "0.6"
|
serde_json = "1.0"
|
||||||
|
|
||||||
|
|
67
src/key.rs
67
src/key.rs
|
@ -274,20 +274,20 @@ impl Encodable for PublicKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for PublicKey {
|
impl<'de> Deserialize<'de> for PublicKey {
|
||||||
fn deserialize<D>(d: &mut D) -> Result<PublicKey, D::Error>
|
fn deserialize<D>(d: D) -> Result<PublicKey, D::Error>
|
||||||
where D: Deserializer
|
where D: Deserializer<'de>
|
||||||
{
|
{
|
||||||
use serde::de;
|
use serde::de;
|
||||||
struct Visitor {
|
struct Visitor {
|
||||||
marker: marker::PhantomData<PublicKey>,
|
marker: marker::PhantomData<PublicKey>,
|
||||||
}
|
}
|
||||||
impl de::Visitor for Visitor {
|
impl<'de> de::Visitor<'de> for Visitor {
|
||||||
type Value = PublicKey;
|
type Value = PublicKey;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seq<V>(&mut self, mut v: V) -> Result<PublicKey, V::Error>
|
fn visit_seq<A>(self, mut a: A) -> Result<PublicKey, A::Error>
|
||||||
where V: de::SeqVisitor
|
where A: de::SeqAccess<'de>
|
||||||
{
|
{
|
||||||
debug_assert!(constants::UNCOMPRESSED_PUBLIC_KEY_SIZE >= constants::COMPRESSED_PUBLIC_KEY_SIZE);
|
debug_assert!(constants::UNCOMPRESSED_PUBLIC_KEY_SIZE >= constants::COMPRESSED_PUBLIC_KEY_SIZE);
|
||||||
|
|
||||||
|
@ -298,27 +298,44 @@ impl Deserialize for PublicKey {
|
||||||
|
|
||||||
let mut read_len = 0;
|
let mut read_len = 0;
|
||||||
while read_len < constants::UNCOMPRESSED_PUBLIC_KEY_SIZE {
|
while read_len < constants::UNCOMPRESSED_PUBLIC_KEY_SIZE {
|
||||||
let read_ch = match try!(v.visit()) {
|
let read_ch = match try!(a.next_element()) {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => break
|
None => break
|
||||||
};
|
};
|
||||||
ret[read_len] = read_ch;
|
ret[read_len] = read_ch;
|
||||||
read_len += 1;
|
read_len += 1;
|
||||||
}
|
}
|
||||||
try!(v.end());
|
let one_after_last : Option<u8> = try!(a.next_element());
|
||||||
|
if one_after_last.is_some() {
|
||||||
PublicKey::from_slice(&s, &ret[..read_len]).map_err(|e| de::Error::syntax(&e.to_string()))
|
return Err(de::Error::invalid_length(read_len + 1, &self));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match read_len {
|
||||||
|
constants::UNCOMPRESSED_PUBLIC_KEY_SIZE | constants::COMPRESSED_PUBLIC_KEY_SIZE
|
||||||
|
=> PublicKey::from_slice(&s, &ret[..read_len]).map_err(
|
||||||
|
|e| match e {
|
||||||
|
InvalidPublicKey => de::Error::invalid_value(de::Unexpected::Seq, &self),
|
||||||
|
_ => de::Error::custom(&e.to_string()),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
_ => Err(de::Error::invalid_length(read_len, &self)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
|
write!(f, "a sequence of {} or {} bytes representing a valid compressed or uncompressed public key",
|
||||||
|
constants::COMPRESSED_PUBLIC_KEY_SIZE, constants::UNCOMPRESSED_PUBLIC_KEY_SIZE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin actual function
|
// Begin actual function
|
||||||
d.visit(Visitor { marker: ::std::marker::PhantomData })
|
d.deserialize_seq(Visitor { marker: ::std::marker::PhantomData })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for PublicKey {
|
impl Serialize for PublicKey {
|
||||||
fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer
|
where S: Serializer
|
||||||
{
|
{
|
||||||
let secp = Secp256k1::with_caps(::ContextFlag::None);
|
let secp = Secp256k1::with_caps(::ContextFlag::None);
|
||||||
|
@ -491,30 +508,30 @@ mod test {
|
||||||
use json;
|
use json;
|
||||||
|
|
||||||
// Invalid length
|
// Invalid length
|
||||||
let zero31 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]".as_bytes();
|
let zero31 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
|
||||||
let mut json = json::de::Deserializer::new(zero31.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(zero31);
|
||||||
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
||||||
let mut json = json::de::Deserializer::new(zero31.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(zero31);
|
||||||
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_err());
|
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_err());
|
||||||
|
|
||||||
let zero32 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]".as_bytes();
|
let zero32 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
|
||||||
let mut json = json::de::Deserializer::new(zero32.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(zero32);
|
||||||
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
||||||
let mut json = json::de::Deserializer::new(zero32.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(zero32);
|
||||||
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_ok());
|
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_ok());
|
||||||
|
|
||||||
// All zeroes pk is invalid
|
// All zeroes pk is invalid
|
||||||
let zero65 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]".as_bytes();
|
let zero65 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
|
||||||
let mut json = json::de::Deserializer::new(zero65.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(zero65);
|
||||||
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
||||||
let mut json = json::de::Deserializer::new(zero65.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(zero65);
|
||||||
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_err());
|
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_err());
|
||||||
|
|
||||||
// Syntax error
|
// Syntax error
|
||||||
let string = "\"my key\"".as_bytes();
|
let string = "\"my key\"";
|
||||||
let mut json = json::de::Deserializer::new(string.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(string);
|
||||||
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
assert!(<PublicKey as Deserialize>::deserialize(&mut json).is_err());
|
||||||
let mut json = json::de::Deserializer::new(string.iter().map(|c| Ok(*c)));
|
let mut json = json::de::Deserializer::from_str(string);
|
||||||
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_err());
|
assert!(<SecretKey as Deserialize>::deserialize(&mut json).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -532,7 +549,7 @@ mod test {
|
||||||
let mut serializer = json::ser::Serializer::new(&mut encoded);
|
let mut serializer = json::ser::Serializer::new(&mut encoded);
|
||||||
start.serialize(&mut serializer).unwrap();
|
start.serialize(&mut serializer).unwrap();
|
||||||
}
|
}
|
||||||
let mut deserializer = json::de::Deserializer::new(encoded.iter().map(|c| Ok(*c)));
|
let mut deserializer = json::de::Deserializer::from_slice(&encoded);
|
||||||
let decoded = Deserialize::deserialize(&mut deserializer);
|
let decoded = Deserialize::deserialize(&mut deserializer);
|
||||||
assert_eq!(Some(start), decoded.ok());
|
assert_eq!(Some(start), decoded.ok());
|
||||||
})
|
})
|
||||||
|
|
|
@ -144,9 +144,9 @@ macro_rules! impl_array_newtype {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::serde::Deserialize for $thing {
|
impl<'de> ::serde::Deserialize<'de> for $thing {
|
||||||
fn deserialize<D>(d: &mut D) -> Result<$thing, D::Error>
|
fn deserialize<D>(d: D) -> Result<$thing, D::Error>
|
||||||
where D: ::serde::Deserializer
|
where D: ::serde::Deserializer<'de>
|
||||||
{
|
{
|
||||||
// We have to define the Visitor struct inside the function
|
// We have to define the Visitor struct inside the function
|
||||||
// to make it local ... all we really need is that it's
|
// to make it local ... all we really need is that it's
|
||||||
|
@ -154,35 +154,42 @@ macro_rules! impl_array_newtype {
|
||||||
struct Visitor {
|
struct Visitor {
|
||||||
marker: ::std::marker::PhantomData<$thing>,
|
marker: ::std::marker::PhantomData<$thing>,
|
||||||
}
|
}
|
||||||
impl ::serde::de::Visitor for Visitor {
|
impl<'de> ::serde::de::Visitor<'de> for Visitor {
|
||||||
type Value = $thing;
|
type Value = $thing;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seq<V>(&mut self, mut v: V) -> Result<$thing, V::Error>
|
fn visit_seq<A>(self, mut a: A) -> Result<$thing, A::Error>
|
||||||
where V: ::serde::de::SeqVisitor
|
where A: ::serde::de::SeqAccess<'de>
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
use std::mem;
|
use std::mem;
|
||||||
let mut ret: [$ty; $len] = mem::uninitialized();
|
let mut ret: [$ty; $len] = mem::uninitialized();
|
||||||
for i in 0..$len {
|
for i in 0..$len {
|
||||||
ret[i] = match try!(v.visit()) {
|
ret[i] = match try!(a.next_element()) {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => return Err(::serde::de::Error::end_of_stream())
|
None => return Err(::serde::de::Error::invalid_length(i, &self))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
try!(v.end());
|
let one_after_last : Option<u8> = try!(a.next_element());
|
||||||
|
if one_after_last.is_some() {
|
||||||
|
return Err(::serde::de::Error::invalid_length($len + 1, &self));
|
||||||
|
}
|
||||||
Ok($thing(ret))
|
Ok($thing(ret))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
|
write!(f, "a sequence of {} elements", $len)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin actual function
|
// Begin actual function
|
||||||
d.visit(Visitor { marker: ::std::marker::PhantomData })
|
d.deserialize_seq(Visitor { marker: ::std::marker::PhantomData })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::serde::Serialize for $thing {
|
impl ::serde::Serialize for $thing {
|
||||||
fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
|
||||||
where S: ::serde::Serializer
|
where S: ::serde::Serializer
|
||||||
{
|
{
|
||||||
(&self.0[..]).serialize(s)
|
(&self.0[..]).serialize(s)
|
||||||
|
|
Loading…
Reference in New Issue