cardano_serialization_lib/serialization/
serialization_macros.rs1#[macro_export]
8macro_rules! from_bytes {
9    ($name:ident, $data: ident, $body:block) => {
11        #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
13        #[wasm_bindgen]
14        impl $name {
15            pub fn from_bytes($data: Vec<u8>) -> Result<$name, JsError> {
16                Ok($body?)
17            }
18        }
19        #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
21        impl $name {
22            pub fn from_bytes($data: Vec<u8>) -> Result<$name, DeserializeError> $body
23        }
24    };
25    ($name:ident) => {
27        from_bytes!($name, bytes, {
28            let mut raw = Deserializer::from(std::io::Cursor::new(bytes));
29            Self::deserialize(&mut raw)
30        });
31    };
32}
33
34#[macro_export]
39macro_rules! to_bytes {
40    ($name:ident) => {
41        #[wasm_bindgen]
42        impl $name {
43            pub fn to_bytes(&self) -> Vec<u8> {
44                let mut buf = Serializer::new_vec();
45                self.serialize(&mut buf).unwrap();
46                buf.finalize()
47            }
48        }
49    };
50}
51
52#[macro_export]
53macro_rules! from_hex {
54    ($name:ident, $data: ident, $body:block) => {
56        #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
58        #[wasm_bindgen]
59        impl $name {
60            pub fn from_hex($data: &str) -> Result<$name, JsError> {
61                match hex::decode($data) {
62                    Ok(_) => Ok($body?),
63                    Err(e) => Err(JsError::from_str(&e.to_string()))
64                }
65
66            }
67        }
68        #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
70        impl $name {
71            pub fn from_hex($data: &str) -> Result<$name, DeserializeError> $body
72        }
73    };
74    ($name:ident) => {
76        from_hex!($name, hex_str, {
77            let mut raw = Deserializer::from(std::io::Cursor::new(hex::decode(hex_str).unwrap()));
78            Self::deserialize(&mut raw)
79        });
80    };
81}
82
83#[macro_export]
84macro_rules! to_hex {
85    ($name:ident) => {
86        #[wasm_bindgen]
87        impl $name {
88            pub fn to_hex(&self) -> String {
89                let mut buf = Serializer::new_vec();
90                self.serialize(&mut buf).unwrap();
91                hex::encode(buf.finalize())
92            }
93        }
94    };
95}
96
97#[macro_export]
98macro_rules! to_from_bytes {
99    ($name:ident) => {
100        to_bytes!($name);
101        from_bytes!($name);
102        to_hex!($name);
103        from_hex!($name);
104    };
105}
106
107#[macro_export]
108macro_rules! to_from_json {
109    ($name:ident) => {
110        #[wasm_bindgen]
111        impl $name {
112            pub fn to_json(&self) -> Result<String, JsError> {
113                serde_json::to_string_pretty(&self)
114                    .map_err(|e| JsError::from_str(&format!("to_json: {}", e)))
115            }
116
117            #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
118            pub fn to_js_value(&self) -> Result<JsValue, JsError> {
119                serde_wasm_bindgen::to_value(&self)
120                    .map_err(|e| JsError::from_str(&format!("to_js_value: {}", e)))
121            }
122
123            pub fn from_json(json: &str) -> Result<$name, JsError> {
124                serde_json::from_str(json)
125                    .map_err(|e| JsError::from_str(&format!("from_json: {}", e)))
126            }
127        }
128    };
129}
130
131#[macro_export]
132macro_rules! impl_to_from {
133    ($name:ident) => {
134        to_from_bytes!($name);
135        to_from_json!($name);
136    };
137}
138
139#[macro_export]
140macro_rules! impl_deserialize_for_wrapped_tuple {
141    ($type:ty) => {
142        impl Deserialize for $type {
143            fn deserialize<R: BufRead + Seek>(
144                raw: &mut Deserializer<R>,
145            ) -> Result<Self, DeserializeError> {
146                (|| -> Result<_, DeserializeError> {
147                    use crate::serialization::utils::check_len_indefinite;
148                    let len = raw.array()?;
149
150                    let inner_struct = Self::deserialize_as_embedded_group(raw, len)?;
151
152                    check_len_indefinite(raw, len)?;
153
154                    Ok(inner_struct)
155                })()
156                .map_err(|e| e.annotate(stringify!($type)))
157            }
158        }
159    };
160}