fedimint_core/encoding/
as_base64.rs

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
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine as _;
use serde::Deserialize as _;

use super::{Decodable, Encodable};
use crate::module::registry::ModuleRegistry;

pub fn serialize<T, S>(t: &T, ser: S) -> Result<S::Ok, S::Error>
where
    T: Encodable,
    S: serde::Serializer,
{
    ser.serialize_str(&URL_SAFE_NO_PAD.encode(t.consensus_encode_to_vec()))
}

pub fn deserialize<'de, T: Decodable, D>(de: D) -> Result<T, D::Error>
where
    D: serde::de::Deserializer<'de>,
{
    Decodable::consensus_decode_whole(
        &URL_SAFE_NO_PAD
            .decode(&String::deserialize(de)?)
            .map_err(|e| {
                serde::de::Error::custom(format!("decodable deserialization failed: {e:#}"))
            })?,
        &ModuleRegistry::default(),
    )
    .map_err(|e| serde::de::Error::custom(format!("decodable deserialization failed: {e:#}")))
}