//! base64url-without-padding, the single encoding every JOSE/SD-JWT field uses.
//!
//! Centralizing it keeps the unpadded URL-safe alphabet from being chosen
//! field-by-field, where one padded call would silently break digest equality.

use crate::error::Error;
use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;

/// Encode bytes as base64url without padding.
#[must_use]
pub(crate) fn encode(bytes: impl AsRef<[u8]>) -> String {
    URL_SAFE_NO_PAD.encode(bytes)
}

/// Decode a base64url-without-padding string, tagging failures with where they
/// occurred.
pub(crate) fn decode(value: &str, context: &'static str) -> Result<Vec<u8>, Error> {
    URL_SAFE_NO_PAD
        .decode(value.as_bytes())
        .map_err(Error::base64(context))
}
