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
94
95
96
97
98
99
100
101
102
103
104
105
#[macro_export]
macro_rules! from_bytes {
($name:ident, $data: ident, $body:block) => {
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[wasm_bindgen]
impl $name {
pub fn from_bytes($data: Vec<u8>) -> Result<$name, JsError> {
Ok($body?)
}
}
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
impl $name {
pub fn from_bytes($data: Vec<u8>) -> Result<$name, DeserializeError> $body
}
};
($name:ident) => {
from_bytes!($name, bytes, {
let mut raw = Deserializer::from(std::io::Cursor::new(bytes));
Self::deserialize(&mut raw)
});
};
}
#[macro_export]
macro_rules! to_bytes {
($name:ident) => {
#[wasm_bindgen]
impl $name {
pub fn to_bytes(&self) -> Vec<u8> {
let mut buf = Serializer::new_vec();
self.serialize(&mut buf).unwrap();
buf.finalize()
}
}
};
}
#[macro_export]
macro_rules! from_hex {
($name:ident, $data: ident, $body:block) => {
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[wasm_bindgen]
impl $name {
pub fn from_hex($data: &str) -> Result<$name, JsError> {
match hex::decode($data) {
Ok(_) => Ok($body?),
Err(e) => Err(JsError::from_str(&e.to_string()))
}
}
}
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
impl $name {
pub fn from_hex($data: &str) -> Result<$name, DeserializeError> $body
}
};
($name:ident) => {
from_hex!($name, hex_str, {
let mut raw = Deserializer::from(std::io::Cursor::new(hex::decode(hex_str).unwrap()));
Self::deserialize(&mut raw)
});
};
}
#[macro_export]
macro_rules! to_hex {
($name:ident) => {
#[wasm_bindgen]
impl $name {
pub fn to_hex(&self) -> String {
let mut buf = Serializer::new_vec();
self.serialize(&mut buf).unwrap();
hex::encode(buf.finalize())
}
}
};
}
#[macro_export]
macro_rules! to_from_bytes {
($name:ident) => {
to_bytes!($name);
from_bytes!($name);
to_hex!($name);
from_hex!($name);
};
}