use std::fmt::{self, Display, Formatter};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as B64;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Base64Bytes(pub Vec<u8>);
impl Base64Bytes {
pub fn decode(&self) -> Result<Vec<u8>, base64::DecodeError> {
B64.decode(&self.0)
}
#[must_use]
pub fn encode<T: AsRef<[u8]>>(input: T) -> Self {
Self(B64.encode(input.as_ref()).into_bytes())
}
}
impl AsRef<[u8]> for Base64Bytes {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl From<&[u8]> for Base64Bytes {
fn from(slice: &[u8]) -> Self {
Self(slice.to_vec())
}
}
impl Display for Base64Bytes {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(&String::from_utf8_lossy(&self.0))
}
}