const_base/config.rs
1use crate::{B32CharSet, B64CharSet, Encoding, HexCharSet};
2
3/// For configuring how a string is encoded/decoded.
4///
5/// `Config` has these values by default:
6///
7/// - `end_padding = true`
8///
9#[derive(Debug, Copy, Clone)]
10pub struct Config {
11 pub(crate) encoding: Encoding,
12 pub(crate) end_padding: bool,
13}
14
15impl Config {
16 /// Constructs a `Config` from an [Encoding]
17 ///
18 /// You can use [the associated constants](#associated-consts) for a more
19 /// concise way to get a `Config`
20 ///
21 /// # Example
22 ///
23 /// ```rust
24 /// use const_base::{B64CharSet, Config, Encoding, encode};
25 ///
26 /// // The same as `Config::B64`
27 /// const CFG: Config = Config::new(Encoding::Base64(B64CharSet::Standard));
28 ///
29 /// assert_eq!(encode!("Rust", CFG), "UnVzdA==")
30 ///
31 /// ```
32 pub const fn new(encoding: Encoding) -> Self {
33 Self {
34 encoding,
35 end_padding: true,
36 }
37 }
38
39 /// Determines whether the string has padding at the end.
40 /// This is `true` by default.
41 ///
42 /// For each encoding, the strings are padded to a multiple:
43 ///
44 /// - Base64: pads to be a multiple of 4 long, with `=`.
45 /// - Base32: pads to be a multiple of 8 long, with `=`.
46 /// - Hexadecimal: requires no padding
47 ///
48 /// # Examples
49 ///
50 /// ### Base 64
51 ///
52 /// ```rust
53 /// use const_base::{Config, encode};
54 ///
55 /// assert_eq!(encode!("Rust", Config::B64), "UnVzdA==");
56 ///
57 /// assert_eq!(encode!("Rust", Config::B64.end_padding(true)), "UnVzdA==");
58 ///
59 /// assert_eq!(encode!("Rust", Config::B64.end_padding(false)), "UnVzdA");
60 ///
61 /// ```
62 ///
63 /// ### Base 32
64 ///
65 /// ```rust
66 /// use const_base::{Config, encode};
67 ///
68 /// assert_eq!(encode!("Rustic", Config::B32), "KJ2XG5DJMM======");
69 ///
70 /// assert_eq!(encode!("Rustic", Config::B32.end_padding(true)), "KJ2XG5DJMM======");
71 ///
72 /// assert_eq!(encode!("Rustic", Config::B32.end_padding(false)), "KJ2XG5DJMM");
73 ///
74 /// ```
75 pub const fn end_padding(mut self, have: bool) -> Self {
76 self.end_padding = have;
77 self
78 }
79
80 /// A different way to call [`encode`](crate::encode()).
81 ///
82 /// # Example
83 ///
84 /// ```rust
85 /// use const_base::{Config, encode};
86 ///
87 /// assert_eq!(Config::B64.encode::<8>(b"Rust").unwrap(), "UnVzdA==");
88 /// ```
89 #[inline(always)]
90 pub const fn encode<const OUT: usize>(
91 self,
92 input: &[u8],
93 ) -> Result<crate::ArrayStr<OUT>, crate::WrongOutputLength> {
94 crate::encode(input, self)
95 }
96
97 /// A different way to call [`decode`](crate::decode()).
98 ///
99 /// # Example
100 ///
101 /// ```rust
102 /// use const_base::{Config, decode};
103 ///
104 /// assert_eq!(Config::B64.decode::<4>(b"UnVzdA=="), Ok(*b"Rust"));
105 /// ```
106 #[inline(always)]
107 pub const fn decode<const OUT: usize>(
108 self,
109 input: &[u8],
110 ) -> Result<[u8; OUT], crate::DecodeError> {
111 crate::decode(input, self)
112 }
113}
114
115/// <div id = "associated-consts"></div>
116impl Config {
117 /// Configuration with the [`Base64`](crate::Encoding::Base64) encoding,
118 /// using the [`Standard`](crate::B64CharSet::Standard) character set.
119 ///
120 /// # Example
121 ///
122 /// ```rust
123 /// use const_base::{Config, encode};
124 ///
125 /// assert_eq!(encode!(&[23, 239, 192], Config::B64), "F+/A");
126 /// ```
127 ///
128 pub const B64: Self = Self::new(Encoding::Base64(B64CharSet::Standard));
129
130 /// Configuration with the [`Base64`](crate::Encoding::Base64) encoding,
131 /// using the [`UrlSafe`](crate::B64CharSet::UrlSafe) character set.
132 ///
133 /// # Example
134 ///
135 /// ```rust
136 /// use const_base::{Config, encode};
137 ///
138 /// assert_eq!(encode!(&[23, 239, 192], Config::B64_URL_SAFE), "F-_A");
139 /// ```
140 ///
141 pub const B64_URL_SAFE: Self = Self::new(Encoding::Base64(B64CharSet::UrlSafe));
142
143 /// Configuration with the [`Base32`](crate::Encoding::Base32) encoding,
144 /// using the [`Standard`](crate::B32CharSet::Standard) character set.
145 ///
146 ///
147 /// # Example
148 ///
149 /// ```rust
150 /// use const_base::{Config, encode};
151 ///
152 /// assert_eq!(encode!(b"neat", Config::B32), "NZSWC5A=");
153 /// ```
154 ///
155 pub const B32: Self = Self::new(Encoding::Base32(B32CharSet::Standard));
156
157 /// Configuration with the [`Hex`](crate::Encoding::Hex) (hexadecimal) encoding,
158 /// using the [`Uppercase`](crate::HexCharSet::Uppercase) character set.
159 ///
160 ///
161 /// # Example
162 ///
163 /// ```rust
164 /// use const_base::{Config, decode, encode};
165 ///
166 /// assert_eq!(encode!(&[0xF1, 0x00, 0x0f], Config::HEX), "F1000F");
167 ///
168 /// // Hexademical decoding allows mixing uppercase and lowercase
169 /// assert_eq!(decode!(b"beefBEEF", Config::HEX), &[0xBE, 0xEF, 0xBE, 0xEF]);
170 /// ```
171 ///
172 pub const HEX: Self = Self::new(Encoding::Hex(HexCharSet::Uppercase));
173
174 /// Configuration with the [`Hex`](crate::Encoding::Hex) (hexadecimal) encoding,
175 /// using the [`Lowercase`](crate::HexCharSet::Lowercase) character set.
176 ///
177 ///
178 /// # Example
179 ///
180 /// ```rust
181 /// use const_base::{Config, decode, encode};
182 ///
183 /// assert_eq!(encode!(&[0xf1, 0x00, 0x0f], Config::HEX_LOWER), "f1000f");
184 ///
185 /// // Hexademical decoding allows mixing uppercase and lowercase
186 /// assert_eq!(decode!(b"beefBEEF", Config::HEX_LOWER), &[0xBE, 0xEF, 0xBE, 0xEF]);
187 /// ```
188 ///
189 pub const HEX_LOWER: Self = Self::new(Encoding::Hex(HexCharSet::Lowercase));
190}