1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
56//! A thin wrapper over [`base64-simd`](https://docs.rs/base64-simd/)
78use base64_simd::STANDARD;
9use std::error::Error;
1011/// Failure to decode a base64 value.
12#[derive(Debug)]
13pub struct DecodeError(base64_simd::Error);
1415impl Error for DecodeError {
16fn source(&self) -> Option<&(dyn Error + 'static)> {
17Some(&self.0)
18 }
19}
2021impl std::fmt::Display for DecodeError {
22fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23write!(f, "failed to decode base64")
24 }
25}
2627/// Decode `input` from base64 using the standard base64 alphabet
28///
29/// If input is not a valid base64 encoded string, this function will return `DecodeError`.
30pub fn decode(input: impl AsRef<str>) -> Result<Vec<u8>, DecodeError> {
31 STANDARD.decode_to_vec(input.as_ref()).map_err(DecodeError)
32}
3334/// Encode `input` into base64 using the standard base64 alphabet
35pub fn encode(input: impl AsRef<[u8]>) -> String {
36 STANDARD.encode_to_string(input.as_ref())
37}
3839/// Returns the base64 representation's length for the given `length` of data
40pub fn encoded_length(length: usize) -> usize {
41 STANDARD.encoded_length(length)
42}