use ::base64::Engine;
use qubit_codec::ValueDecoder;
use qubit_codec::ValueEncoder;
use crate::Base64Decoder;
use crate::Base64Encoder;
use crate::MiscCodecError;
use crate::MiscCodecResult;
use crate::internal::Base64Config;
#[must_use]
#[derive(Debug, Clone, Copy)]
pub struct Base64Codec {
config: Base64Config,
}
impl PartialEq for Base64Codec {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.config == other.config
}
}
impl Eq for Base64Codec {}
impl Base64Codec {
#[inline]
pub fn standard() -> Self {
Self {
config: Base64Config::Standard,
}
}
#[inline]
pub fn standard_no_pad() -> Self {
Self {
config: Base64Config::StandardNoPad,
}
}
#[inline]
pub fn url_safe() -> Self {
Self {
config: Base64Config::UrlSafe,
}
}
#[inline]
pub fn url_safe_no_pad() -> Self {
Self {
config: Base64Config::UrlSafeNoPad,
}
}
#[inline]
#[must_use]
pub fn encode(&self, bytes: &[u8]) -> String {
self.config.engine().encode(bytes)
}
#[inline]
pub fn decode(&self, text: &str) -> MiscCodecResult<Vec<u8>> {
self.config.engine().decode(text).map_err(MiscCodecError::from)
}
#[inline]
pub fn encoder(&self) -> Base64Encoder {
Base64Encoder::from_config(self.config)
}
#[inline]
pub fn decoder(&self) -> Base64Decoder {
Base64Decoder::from_config(self.config)
}
}
impl Default for Base64Codec {
#[inline]
fn default() -> Self {
Self::standard()
}
}
impl ValueEncoder<[u8]> for Base64Codec {
type Error = MiscCodecError;
type Output = String;
#[inline]
fn encode(&mut self, input: &[u8]) -> Result<Self::Output, Self::Error> {
Ok(Base64Codec::encode(self, input))
}
}
impl ValueDecoder<str> for Base64Codec {
type Error = MiscCodecError;
type Output = Vec<u8>;
#[inline]
fn decode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
Base64Codec::decode(self, input)
}
}