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
use crate::{Decoder, Encoder};
use base64::Engine;
use thiserror::Error;
/// Wraps a binary codec and make it a string codec by representing the binary data as a base64
/// string.
///
/// Only available with the **`base64` feature** enabled.
///
/// Example:
///
/// ```
/// # use codee::{Encoder, Decoder};
/// # use codee::string::Base64;
/// # use codee::binary::MsgpackSerdeCodec;
/// # use serde::{Serialize, Deserialize};
/// #
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct MyState {
/// chicken_count: u32,
/// egg_count: u32,
/// farm_name: String,
/// }
///
/// let original_value = MyState {
/// chicken_count: 10,
/// egg_count: 20,
/// farm_name: "My Farm".to_owned(),
/// };
///
/// let encoded: String = Base64::<MsgpackSerdeCodec>::encode(&original_value).unwrap();
/// let decoded: MyState = Base64::<MsgpackSerdeCodec>::decode(&encoded).unwrap();
///
/// assert_eq!(decoded, original_value);
/// ```
pub struct Base64<C>(C);
#[derive(Error, Debug, PartialEq)]
pub enum Base64DecodeError<Err> {
#[error("failed to decode base64: {0}")]
DecodeBase64(#[from] base64::DecodeError),
#[error("failed to decode: {0}")]
Decoder(Err),
}
impl<T, E> Encoder<T> for Base64<E>
where
E: Encoder<T, Encoded = Vec<u8>>,
{
type Error = E::Error;
type Encoded = String;
fn encode(val: &T) -> Result<Self::Encoded, Self::Error> {
Ok(base64::engine::general_purpose::STANDARD.encode(E::encode(val)?))
}
}
impl<T, D> Decoder<T> for Base64<D>
where
D: Decoder<T, Encoded = [u8]>,
{
type Error = Base64DecodeError<D::Error>;
type Encoded = str;
fn decode(val: &Self::Encoded) -> Result<T, Self::Error> {
let buf = base64::engine::general_purpose::STANDARD.decode(val)?;
D::decode(&buf).map_err(Base64DecodeError::Decoder)
}
}