1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::fmt;
use std::str::FromStr;
use serde;
use serde::de::Unexpected;
use primitives::bigint::{U256 as GlobalU256, Uint};
macro_rules! impl_uint {
($name: ident, $other: ident, $size: expr) => {
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash)]
pub struct $name($other);
impl Eq for $name { }
impl<T> From<T> for $name where $other: From<T> {
fn from(o: T) -> Self {
$name($other::from(o))
}
}
impl FromStr for $name {
type Err = <$other as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
$other::from_str(s).map($name)
}
}
impl Into<$other> for $name {
fn into(self) -> $other {
self.0
}
}
impl serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
let as_hex = format!("{}", self.0.to_hex());
serializer.serialize_str(&as_hex)
}
}
impl<'a> serde::Deserialize<'a> for $name {
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error> where D: serde::Deserializer<'a> {
struct UintVisitor;
impl<'b> serde::de::Visitor<'b> for UintVisitor {
type Value = $name;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an integer represented in hex string")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error {
if value.len() > $size * 16 {
return Err(E::invalid_value(Unexpected::Str(value), &self))
}
$other::from_str(value).map($name).map_err(|_| E::invalid_value(Unexpected::Str(value), &self))
}
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: serde::de::Error {
self.visit_str(&value)
}
}
deserializer.deserialize_identifier(UintVisitor)
}
}
}
}
impl_uint!(U256, GlobalU256, 4);
#[cfg(test)]
mod tests {
use super::U256;
use serde_json;
#[test]
fn u256_serialize() {
let u256 = U256::from(256);
let serialized = serde_json::to_string(&u256).unwrap();
assert_eq!(serialized, r#""100""#);
}
#[test]
fn u256_deserialize() {
let u256 = U256::from(256);
let deserialized = serde_json::from_str::<U256>(r#""100""#).unwrap();
assert_eq!(deserialized, u256);
}
}