qubit_codec_misc/
base64_codec.rs1use ::base64::Engine;
11use ::base64::engine::GeneralPurpose;
12use ::base64::engine::general_purpose::STANDARD;
13use ::base64::engine::general_purpose::STANDARD_NO_PAD;
14use ::base64::engine::general_purpose::URL_SAFE;
15use ::base64::engine::general_purpose::URL_SAFE_NO_PAD;
16use qubit_codec::ValueDecoder;
17use qubit_codec::ValueEncoder;
18
19use crate::MiscCodecError;
20use crate::MiscCodecResult;
21
22#[must_use]
28#[derive(Debug, Clone, Copy)]
29pub struct Base64Codec {
30 engine: &'static GeneralPurpose,
31}
32
33impl PartialEq for Base64Codec {
34 #[inline]
35 fn eq(&self, other: &Self) -> bool {
36 core::ptr::eq(self.engine, other.engine)
37 }
38}
39
40impl Eq for Base64Codec {}
41
42impl Base64Codec {
43 #[inline]
48 pub fn standard() -> Self {
49 Self { engine: &STANDARD }
50 }
51
52 #[inline]
57 pub fn standard_no_pad() -> Self {
58 Self {
59 engine: &STANDARD_NO_PAD,
60 }
61 }
62
63 #[inline]
68 pub fn url_safe() -> Self {
69 Self { engine: &URL_SAFE }
70 }
71
72 #[inline]
77 pub fn url_safe_no_pad() -> Self {
78 Self {
79 engine: &URL_SAFE_NO_PAD,
80 }
81 }
82
83 #[inline]
91 #[must_use]
92 pub fn encode(&self, bytes: &[u8]) -> String {
93 self.engine.encode(bytes)
94 }
95
96 #[inline]
107 pub fn decode(&self, text: &str) -> MiscCodecResult<Vec<u8>> {
108 self.engine.decode(text).map_err(MiscCodecError::from)
109 }
110}
111
112impl Default for Base64Codec {
113 #[inline]
115 fn default() -> Self {
116 Self::standard()
117 }
118}
119
120impl ValueEncoder<[u8]> for Base64Codec {
121 type Error = MiscCodecError;
122 type Output = String;
123
124 #[inline]
126 fn encode(&mut self, input: &[u8]) -> Result<Self::Output, Self::Error> {
127 Ok(Base64Codec::encode(self, input))
128 }
129}
130
131impl ValueDecoder<str> for Base64Codec {
132 type Error = MiscCodecError;
133 type Output = Vec<u8>;
134
135 #[inline]
137 fn decode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
138 Base64Codec::decode(self, input)
139 }
140}