base64id_core/
error.rs

1use core::fmt;
2
3/// Enum for base64url decoding errors
4#[derive(Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Error {
7    /// Returned when input data contains an invalid number of characters
8    ///
9    /// ## Expected Lengths
10    /// - `i64` or `u64`: 11 characters
11    /// - `i32` or `u32`: 6 characters
12    /// - `i16` or `u16`: 3 characters
13    InvalidLength,
14    /// Returned when input data conatins a character that is not within the base64url alphabet
15    InvalidCharacter,
16    /// Returned when the last character of input data is out of bounds
17    ///
18    /// For `i64`, `u64`, `i16` and `u16` values, the last character must be one of the following:
19    /// ```txt
20    /// AEIMQUYcgkosw048
21    /// ```
22    ///
23    /// For `i32` and `u32` values, the last character must be one of the following:
24    /// ```txt
25    /// AQgw
26    /// ```
27    OutOfBoundsCharacter,
28}
29
30impl core::error::Error for Error {}
31
32impl fmt::Display for Error {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        use Error::*;
35
36        match self {
37            InvalidLength => write!(f, "invalid length. number of characters was invalid"),
38            InvalidCharacter => write!(
39                f,
40                "invalid character(s). expected only base64url characters"
41            ),
42            OutOfBoundsCharacter => {
43                write!(f, "invalid character. last character was out of bounds")
44            }
45        }
46    }
47}