Skip to main content

qubit_codec_misc/
base64_error_kind.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Structured Base64 dependency error categories.
9
10use ::base64::DecodeError;
11
12/// Structured category for an error reported by the Base64 dependency.
13///
14/// # Examples
15///
16/// ```
17/// use qubit_codec_misc::Base64ErrorKind;
18///
19/// let kind = Base64ErrorKind::InvalidPadding;
20/// assert_eq!(kind, Base64ErrorKind::InvalidPadding);
21/// ```
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23#[must_use]
24pub enum Base64ErrorKind {
25    /// A byte outside the configured alphabet was encountered.
26    InvalidByte,
27    /// The number of Base64 symbols is invalid.
28    InvalidLength,
29    /// The final symbol contains bits that would be discarded.
30    InvalidLastSymbol,
31    /// Padding is absent, unexpected, or malformed for the configured engine.
32    InvalidPadding,
33}
34
35impl Base64ErrorKind {
36    /// Classifies a Base64 dependency error without discarding its source.
37    #[inline]
38    pub(crate) const fn from_decode_error(error: &DecodeError) -> Self {
39        match error {
40            DecodeError::InvalidByte(_, _) => Self::InvalidByte,
41            DecodeError::InvalidLength(_) => Self::InvalidLength,
42            DecodeError::InvalidLastSymbol { .. } => Self::InvalidLastSymbol,
43            DecodeError::InvalidPadding => Self::InvalidPadding,
44        }
45    }
46
47    /// Returns the input offset carried by this Base64 error, when available.
48    #[inline]
49    pub(crate) const fn input_index(error: &DecodeError) -> Option<usize> {
50        match error {
51            DecodeError::InvalidByte(index, _) | DecodeError::InvalidLastSymbol { offset: index, .. } => Some(*index),
52            DecodeError::InvalidLength(_) | DecodeError::InvalidPadding => None,
53        }
54    }
55
56    /// Returns the invalid symbol carried by this Base64 error, when present.
57    #[inline]
58    pub(crate) const fn symbol(error: &DecodeError) -> Option<u8> {
59        match error {
60            DecodeError::InvalidByte(_, symbol) | DecodeError::InvalidLastSymbol { symbol, .. } => Some(*symbol),
61            DecodeError::InvalidLength(_) | DecodeError::InvalidPadding => None,
62        }
63    }
64
65    /// Returns the invalid input length carried by this Base64 error, when
66    /// present.
67    #[inline]
68    pub(crate) const fn input_length(error: &DecodeError) -> Option<usize> {
69        match error {
70            DecodeError::InvalidLength(length) => Some(*length),
71            DecodeError::InvalidByte(_, _) | DecodeError::InvalidLastSymbol { .. } | DecodeError::InvalidPadding => {
72                None
73            }
74        }
75    }
76}