Coverage Report

Created: 2025-02-22 06:51

/rust/registry/src/index.crates.io-6f17d22bba15001f/base64ct-1.6.0/src/errors.rs
Line
Count
Source (jump to first uncovered line)
1
//! Error types
2
3
use core::fmt;
4
5
const INVALID_ENCODING_MSG: &str = "invalid Base64 encoding";
6
const INVALID_LENGTH_MSG: &str = "invalid Base64 length";
7
8
/// Insufficient output buffer length.
9
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10
pub struct InvalidLengthError;
11
12
impl fmt::Display for InvalidLengthError {
13
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
14
0
        f.write_str(INVALID_LENGTH_MSG)
15
0
    }
16
}
17
18
#[cfg(feature = "std")]
19
impl std::error::Error for InvalidLengthError {}
20
21
/// Invalid encoding of provided Base64 string.
22
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
23
pub struct InvalidEncodingError;
24
25
impl fmt::Display for InvalidEncodingError {
26
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
27
0
        f.write_str(INVALID_ENCODING_MSG)
28
0
    }
29
}
30
31
#[cfg(feature = "std")]
32
impl std::error::Error for InvalidEncodingError {}
33
34
/// Generic error, union of [`InvalidLengthError`] and [`InvalidEncodingError`].
35
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
36
pub enum Error {
37
    /// Invalid encoding of provided Base64 string.
38
    InvalidEncoding,
39
40
    /// Insufficient output buffer length.
41
    InvalidLength,
42
}
43
44
impl fmt::Display for Error {
45
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
46
0
        let s = match self {
47
0
            Self::InvalidEncoding => INVALID_ENCODING_MSG,
48
0
            Self::InvalidLength => INVALID_LENGTH_MSG,
49
        };
50
0
        f.write_str(s)
51
0
    }
52
}
53
54
impl From<InvalidEncodingError> for Error {
55
    #[inline]
56
0
    fn from(_: InvalidEncodingError) -> Error {
57
0
        Error::InvalidEncoding
58
0
    }
59
}
60
61
impl From<InvalidLengthError> for Error {
62
    #[inline]
63
0
    fn from(_: InvalidLengthError) -> Error {
64
0
        Error::InvalidLength
65
0
    }
Unexecuted instantiation: <base64ct::errors::Error as core::convert::From<base64ct::errors::InvalidLengthError>>::from
Unexecuted instantiation: <base64ct::errors::Error as core::convert::From<base64ct::errors::InvalidLengthError>>::from
66
}
67
68
impl From<core::str::Utf8Error> for Error {
69
    #[inline]
70
0
    fn from(_: core::str::Utf8Error) -> Error {
71
0
        Error::InvalidEncoding
72
0
    }
73
}
74
75
#[cfg(feature = "std")]
76
impl From<Error> for std::io::Error {
77
0
    fn from(err: Error) -> std::io::Error {
78
0
        // TODO(tarcieri): better customize `ErrorKind`?
79
0
        std::io::Error::new(std::io::ErrorKind::InvalidData, err)
80
0
    }
81
}
82
83
#[cfg(feature = "std")]
84
impl std::error::Error for Error {}