tenderdash_proto/serializers/
txs.rs

1//! Serialize/deserialize `Vec<Vec<u8>>` type from and into transactions
2//! (Base64String array).
3use serde::{Deserialize, Deserializer, Serialize, Serializer};
4use subtle_encoding::base64;
5
6use crate::prelude::*;
7
8/// Deserialize transactions into `Vec<Vec<u8>>`
9pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Vec<u8>>, D::Error>
10where
11    D: Deserializer<'de>,
12{
13    let value_vec_base64string = Option::<Vec<String>>::deserialize(deserializer)?;
14    if value_vec_base64string.is_none() {
15        return Ok(Vec::new());
16    }
17    let value_vec_base64string = value_vec_base64string.unwrap();
18    if value_vec_base64string.is_empty() {
19        return Ok(Vec::new());
20    }
21    value_vec_base64string
22        .into_iter()
23        .map(|s| base64::decode(s).map_err(serde::de::Error::custom))
24        .collect()
25}
26
27/// Serialize from `Vec<Vec<u8>>` into transactions
28pub fn serialize<S>(value: &[Vec<u8>], serializer: S) -> Result<S::Ok, S::Error>
29where
30    S: Serializer,
31{
32    if value.is_empty() {
33        let whatevs: Option<Vec<u8>> = None;
34        return whatevs.serialize(serializer);
35    }
36    let value_base64string: Result<Vec<String>, S::Error> = value
37        .iter()
38        .map(|v| String::from_utf8(base64::encode(v)).map_err(serde::ser::Error::custom))
39        .collect();
40    value_base64string?.serialize(serializer)
41}