weakauras_codec_base64/
error.rs

1// Copyright 2025 Velithris
2// SPDX-License-Identifier: MIT
3
4use core::fmt;
5#[cfg(any(test, feature = "std"))]
6use std::error;
7
8/// Errors than can occur while decoding.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum DecodeError {
11    /// An invalid byte was found in the input. Its offset is provided.
12    InvalidByte(usize),
13    /// The input's length is invalid.
14    InvalidLength,
15}
16
17impl fmt::Display for DecodeError {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match *self {
20            Self::InvalidByte(offset) => write!(f, "Invalid byte at offset {}", offset),
21            Self::InvalidLength => write!(f, "Invalid length"),
22        }
23    }
24}
25
26#[cfg(any(test, feature = "std"))]
27impl error::Error for DecodeError {}
28
29/// Errors than can occur while decoding into a slice.
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub enum DecodeIntoSliceError {
32    /// A [DecodeError] occurred.
33    DecodeError(DecodeError),
34    /// The provided slice is too small.
35    OutputSliceIsTooSmall,
36}
37
38impl From<DecodeError> for DecodeIntoSliceError {
39    fn from(e: DecodeError) -> Self {
40        Self::DecodeError(e)
41    }
42}
43
44impl fmt::Display for DecodeIntoSliceError {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        match self {
47            Self::DecodeError(inner) => inner.fmt(f),
48            Self::OutputSliceIsTooSmall => write!(f, "Output slice is too small"),
49        }
50    }
51}
52
53#[cfg(any(test, feature = "std"))]
54impl error::Error for DecodeIntoSliceError {
55    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
56        match self {
57            Self::DecodeError(inner) => Some(inner),
58            Self::OutputSliceIsTooSmall => None,
59        }
60    }
61}
62
63/// Errors than can occur while encoding.
64#[derive(Clone, Debug, PartialEq, Eq)]
65pub enum EncodeError {
66    /// Encoding the input would require more than [usize::MAX] bytes of output.
67    DataIsTooLarge,
68}
69
70impl fmt::Display for EncodeError {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        match self {
73            Self::DataIsTooLarge => write!(f, "Data is too large"),
74        }
75    }
76}
77
78#[cfg(any(test, feature = "std"))]
79impl error::Error for EncodeError {}
80
81/// Errors than can occur while encoding into a slice.
82#[derive(Clone, Debug, PartialEq, Eq)]
83pub enum EncodeIntoSliceError {
84    /// An [EncodeError] occurred.
85    EncodeError(EncodeError),
86    /// The provided slice is too small.
87    OutputSliceIsTooSmall,
88}
89
90impl From<EncodeError> for EncodeIntoSliceError {
91    fn from(e: EncodeError) -> Self {
92        Self::EncodeError(e)
93    }
94}
95
96impl fmt::Display for EncodeIntoSliceError {
97    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98        match self {
99            Self::EncodeError(inner) => inner.fmt(f),
100            Self::OutputSliceIsTooSmall => write!(f, "Output slice is too small"),
101        }
102    }
103}
104
105#[cfg(any(test, feature = "std"))]
106impl error::Error for EncodeIntoSliceError {
107    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
108        match self {
109            Self::EncodeError(inner) => Some(inner),
110            Self::OutputSliceIsTooSmall => None,
111        }
112    }
113}