Skip to main content

codec_base64/
error.rs

1// SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use thiserror::Error;
6
7/// Error returned when base64 decoding fails.
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum Base64Error {
11    /// The input was not valid base64.
12    #[error("invalid base64")]
13    Invalid,
14    /// Encoded text exceeded the supported resource limit.
15    #[error("base64 input too large")]
16    InputTooLarge,
17    /// Decoded bytes would exceed the caller's output budget.
18    #[error("base64 output too large")]
19    OutputTooLarge,
20    /// A supplied budget cannot describe an addressable buffer.
21    #[error("invalid base64 limits")]
22    InvalidLimits,
23    /// Checked length arithmetic could not represent the decoded size.
24    #[error("base64 length overflow")]
25    LengthOverflow,
26    /// The validated output reservation could not be satisfied.
27    #[error("base64 allocation failed")]
28    AllocationFailure,
29}
30
31impl Base64Error {
32    /// The schema-owned failure reason shared by native and SDK adapters.
33    #[must_use]
34    pub const fn reason(
35        &self,
36    ) -> codec_proto::generated::proto::reallyme::codec::v1::CodecErrorReason {
37        use codec_proto::generated::proto::reallyme::codec::v1::CodecErrorReason as R;
38        match self {
39            Self::Invalid => R::CODEC_ERROR_REASON_BASE_INVALID_BASE64,
40            Self::InputTooLarge => R::CODEC_ERROR_REASON_BASE_INPUT_TOO_LARGE,
41            Self::OutputTooLarge => R::CODEC_ERROR_REASON_BASE_OUTPUT_TOO_LARGE,
42            Self::InvalidLimits => R::CODEC_ERROR_REASON_BASE_INVALID_LIMITS,
43            Self::LengthOverflow => R::CODEC_ERROR_REASON_BASE_LENGTH_OVERFLOW,
44            Self::AllocationFailure => R::CODEC_ERROR_REASON_BASE_ALLOCATION_FAILURE,
45        }
46    }
47}