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
use crate::types::{H256, U256};
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{self, Value};
#[derive(Debug, PartialEq, Eq)]
pub struct Work {
pub pow_hash: H256,
pub seed_hash: H256,
pub target: H256,
pub number: Option<u64>,
}
impl<'a> Deserialize<'a> for Work {
fn deserialize<D>(deserializer: D) -> Result<Work, D::Error>
where
D: Deserializer<'a>,
{
let v: Value = Deserialize::deserialize(deserializer)?;
let (pow_hash, seed_hash, target, number) = serde_json::from_value::<(H256, H256, H256, u64)>(v.clone())
.map(|(pow_hash, seed_hash, target, number)| (pow_hash, seed_hash, target, Some(number)))
.or_else(|_| {
serde_json::from_value::<(H256, H256, H256)>(v)
.map(|(pow_hash, seed_hash, target)| (pow_hash, seed_hash, target, None))
})
.map_err(|e| D::Error::custom(format!("Cannot deserialize Work: {:?}", e)))?;
Ok(Work {
pow_hash,
seed_hash,
target,
number,
})
}
}
impl Serialize for Work {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self.number.as_ref() {
Some(num) => (&self.pow_hash, &self.seed_hash, &self.target, U256::from(*num)).serialize(s),
None => (&self.pow_hash, &self.seed_hash, &self.target).serialize(s),
}
}
}