wiwi/encoding/
generic_fn.rs1#![allow(unused_imports, reason = "wip")]
2use crate::prelude::*;
3use super::{ base16, base32, base64, hex, rfc1751, z85 };
4
5#[inline]
6pub fn encode<E>(value: impl Encodable<E>) -> E::EncodeOutput
7where
8 E: Encoding
9{
10 value.encode()
11}
12
13#[inline]
14pub fn decode<E>(value: impl Decodable<E>) -> E::DecodeOutput
15where
16 E: Encoding
17{
18 value.decode()
19}
20
21pub trait Encode {
22 fn encode<E>(self) -> E::EncodeOutput
23 where
24 Self: Encodable<E>,
25 E: Encoding;
26
27 fn decode<E>(self) -> E::DecodeOutput
28 where
29 Self: Decodable<E>,
30 E: Encoding;
31}
32
33impl<T> Encode for T {
34 #[inline]
35 fn encode<E>(self) -> E::EncodeOutput
36 where
37 Self: Encodable<E>,
38 E: Encoding
39 {
40 encode(self)
41 }
42
43 #[inline]
44 fn decode<E>(self) -> E::DecodeOutput
45 where
46 Self: Decodable<E>,
47 E: Encoding
48 {
49 decode(self)
50 }
51}
52
53pub trait Encodable<E>
54where
55 E: Encoding,
56 Self: Sized
57{
58 fn encode(self) -> E::EncodeOutput;
59}
60
61pub trait Decodable<E>
62where
63 E: Encoding,
64 Self: Sized
65{
66 fn decode(self) -> E::DecodeOutput;
67}
68
69pub trait Encoding
70where
71 Self: Sized + Sealed
72{
73 type EncodeOutput;
74 type DecodeOutput;
75
76 #[inline]
77 fn encode<T>(value: T) -> Self::EncodeOutput
78 where
79 T: Encodable<Self>
80 {
81 value.encode()
82 }
83
84 #[inline]
85 fn decode<T>(value: T) -> Self::DecodeOutput
86 where
87 T: Decodable<Self>
88 {
89 value.decode()
90 }
91}
92
93pub struct Base16 {
94 __private: ()
95}
96
97impl Sealed for Base16 {}
98
99impl Encoding for Base16 {
100 type EncodeOutput = String;
101 type DecodeOutput = Result<Vec<u8>, base16::DecodeError>;
102}
103
104impl Encodable<Base16> for &[u8] {
105 #[inline]
106 fn encode(self) -> String {
107 base16::encode_base16(self)
108 }
109}
110
111impl Encodable<Base16> for &str {
112 #[inline]
113 fn encode(self) -> String {
114 base16::encode_base16(self.as_bytes())
115 }
116}
117
118impl Decodable<Base16> for &[u8] {
119 #[inline]
120 fn decode(self) -> Result<Vec<u8>, base16::DecodeError> {
121 base16::decode_base16(self)
122 }
123}
124
125impl Decodable<Base16> for &str {
126 #[inline]
127 fn decode(self) -> Result<Vec<u8>, base16::DecodeError> {
128 base16::decode_base16(self.as_bytes())
129 }
130}
131
132pub struct Base32 {
133 __private: ()
134}
135
136impl Sealed for Base32 {}
137
138impl Encoding for Base32 {
139 type EncodeOutput = String;
140 type DecodeOutput = ();
141}
142
143impl Encodable<Base32> for &[u8] {
144 #[inline]
145 fn encode(self) -> String {
146 base32::encode_base32(self)
147 }
148}
149
150impl Encodable<Base32> for &str {
151 #[inline]
152 fn encode(self) -> String {
153 base32::encode_base32(self.as_bytes())
154 }
155}
156
157pub struct Base64 {
158 __private: ()
159}
160
161impl Sealed for Base64 {}
162
163impl Encoding for Base64 {
164 type EncodeOutput = ();
165 type DecodeOutput = ();
166}
167
168pub struct Hex {
169 __private: ()
170}
171
172impl Sealed for Hex {}
173
174impl Encoding for Hex {
175 type EncodeOutput = String;
176 type DecodeOutput = Result<Vec<u8>, hex::DecodeError>;
177}
178
179impl Encodable<Hex> for &[u8] {
180 #[inline]
181 fn encode(self) -> String {
182 hex::encode_hex(self)
183 }
184}
185
186impl Encodable<Hex> for &str {
187 #[inline]
188 fn encode(self) -> String {
189 hex::encode_hex(self.as_bytes())
190 }
191}
192
193impl Decodable<Hex> for &[u8] {
194 #[inline]
195 fn decode(self) -> Result<Vec<u8>, hex::DecodeError> {
196 hex::decode_hex(self)
197 }
198}
199
200impl Decodable<Hex> for &str {
201 #[inline]
202 fn decode(self) -> Result<Vec<u8>, hex::DecodeError> {
203 hex::decode_hex(self.as_bytes())
204 }
205}
206
207pub struct RFC1751 {
208 __private: ()
209}
210
211impl Sealed for RFC1751 {}
212
213impl Encoding for RFC1751 {
214 type EncodeOutput = ();
215 type DecodeOutput = ();
216}
217
218pub struct Z85 {
219 __private: ()
220}
221
222impl Sealed for Z85 {}
223
224impl Encoding for Z85 {
225 type EncodeOutput = String;
226 type DecodeOutput = Result<Vec<u8>, z85::DecodeError>;
227}
228
229impl Encodable<Z85> for &[u8] {
230 #[inline]
231 fn encode(self) -> String {
232 z85::encode_z85(self)
233 }
234}
235
236impl Encodable<Z85> for &str {
237 #[inline]
238 fn encode(self) -> String {
239 z85::encode_z85(self.as_bytes())
240 }
241}
242
243impl Decodable<Z85> for &[u8] {
244 #[inline]
245 fn decode(self) -> Result<Vec<u8>, z85::DecodeError> {
246 z85::decode_z85(self)
247 }
248}
249
250impl Decodable<Z85> for &str {
251 #[inline]
252 fn decode(self) -> Result<Vec<u8>, z85::DecodeError> {
253 z85::decode_z85(self.as_bytes())
254 }
255}
256
257pub trait Sealed {}