1use base64::engine::{Engine, general_purpose};
2
3pub fn b64u_encode(content: impl AsRef<[u8]>) -> String {
4 general_purpose::URL_SAFE_NO_PAD.encode(content)
5}
6
7pub fn b64u_decode(b64u: &str) -> Result<Vec<u8>> {
8 general_purpose::URL_SAFE_NO_PAD
9 .decode(b64u)
10 .map_err(|_| Error::FailToB64uDecode)
11}
12
13pub fn b64u_decode_to_string(b64u: &str) -> Result<String> {
14 b64u_decode(b64u)
15 .ok()
16 .and_then(|r| String::from_utf8(r).ok())
17 .ok_or(Error::FailToB64uDecode)
18}
19
20pub type Result<T> = core::result::Result<T, Error>;
21
22#[derive(Debug)]
23pub enum Error {
24 FailToB64uDecode,
25}
26
27impl core::fmt::Display for Error {
28 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
29 write!(fmt, "{self:?}")
30 }
31}
32
33impl std::error::Error for Error {}