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
use ckb_types::{packed, prelude::*};
use faster_hex::{hex_decode, hex_encode};
use std::fmt;
#[derive(Clone, Default, PartialEq, Eq, Hash, Debug)]
pub struct ProposalShortId(pub [u8; 10]);
impl ProposalShortId {
pub fn new(inner: [u8; 10]) -> ProposalShortId {
ProposalShortId(inner)
}
pub fn into_inner(self) -> [u8; 10] {
self.0
}
}
impl From<packed::ProposalShortId> for ProposalShortId {
fn from(core: packed::ProposalShortId) -> ProposalShortId {
ProposalShortId::new(core.unpack())
}
}
impl From<ProposalShortId> for packed::ProposalShortId {
fn from(json: ProposalShortId) -> Self {
json.into_inner().pack()
}
}
struct ProposalShortIdVisitor;
impl<'b> serde::de::Visitor<'b> for ProposalShortIdVisitor {
type Value = ProposalShortId;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a 0x-prefixed hex string")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v.len() < 2 || &v.as_bytes()[0..2] != b"0x" || v.len() != 22 {
return Err(E::invalid_value(serde::de::Unexpected::Str(v), &self));
}
let mut buffer = [0u8; 10];
hex_decode(&v.as_bytes()[2..], &mut buffer)
.map_err(|e| E::custom(format_args!("{:?}", e)))?;
Ok(ProposalShortId::new(buffer))
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(&v)
}
}
impl serde::Serialize for ProposalShortId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut buffer = [0u8; 22];
buffer[0] = b'0';
buffer[1] = b'x';
hex_encode(&self.0, &mut buffer[2..])
.map_err(|e| serde::ser::Error::custom(&format!("{}", e)))?;
serializer.serialize_str(unsafe { ::std::str::from_utf8_unchecked(&buffer) })
}
}
impl<'de> serde::Deserialize<'de> for ProposalShortId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_str(ProposalShortIdVisitor)
}
}