spin_serde/
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
30
//! Base64 (de)serialization

use std::borrow::Cow;

use base64::{engine::GeneralPurpose, prelude::BASE64_STANDARD_NO_PAD, Engine};
use serde::{de, Deserialize, Deserializer, Serializer};

const BASE64: GeneralPurpose = BASE64_STANDARD_NO_PAD;

/// Serializes to base64.
pub fn serialize<S>(bytes: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match bytes {
        Some(bytes) => serializer.serialize_str(&BASE64.encode(bytes)),
        None => serializer.serialize_none(),
    }
}

/// Deserializes from base64.
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error>
where
    D: Deserializer<'de>,
{
    match Option::<Cow<str>>::deserialize(deserializer)? {
        Some(s) => Ok(Some(BASE64.decode(s.as_ref()).map_err(de::Error::custom)?)),
        None => Ok(None),
    }
}