/rust/registry/src/index.crates.io-1949cf8c6b5b557f/base64ct-1.8.0/src/errors.rs
Line | Count | Source |
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 | | impl core::error::Error for InvalidLengthError {} |
19 | | |
20 | | /// Invalid encoding of provided Base64 string. |
21 | | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
22 | | pub struct InvalidEncodingError; |
23 | | |
24 | | impl fmt::Display for InvalidEncodingError { |
25 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { |
26 | 0 | f.write_str(INVALID_ENCODING_MSG) |
27 | 0 | } |
28 | | } |
29 | | |
30 | | impl core::error::Error for InvalidEncodingError {} |
31 | | |
32 | | /// Generic error, union of [`InvalidLengthError`] and [`InvalidEncodingError`]. |
33 | | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
34 | | pub enum Error { |
35 | | /// Invalid encoding of provided Base64 string. |
36 | | InvalidEncoding, |
37 | | |
38 | | /// Insufficient output buffer length. |
39 | | InvalidLength, |
40 | | } |
41 | | |
42 | | impl fmt::Display for Error { |
43 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { |
44 | 0 | let s = match self { |
45 | 0 | Self::InvalidEncoding => INVALID_ENCODING_MSG, |
46 | 0 | Self::InvalidLength => INVALID_LENGTH_MSG, |
47 | | }; |
48 | 0 | f.write_str(s) |
49 | 0 | } |
50 | | } |
51 | | |
52 | | impl From<InvalidEncodingError> for Error { |
53 | | #[inline] |
54 | 0 | fn from(_: InvalidEncodingError) -> Error { |
55 | 0 | Error::InvalidEncoding |
56 | 0 | } |
57 | | } |
58 | | |
59 | | impl From<InvalidLengthError> for Error { |
60 | | #[inline] |
61 | 0 | fn from(_: InvalidLengthError) -> Error { |
62 | 0 | Error::InvalidLength |
63 | 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 |
64 | | } |
65 | | |
66 | | impl From<core::str::Utf8Error> for Error { |
67 | | #[inline] |
68 | 0 | fn from(_: core::str::Utf8Error) -> Error { |
69 | 0 | Error::InvalidEncoding |
70 | 0 | } |
71 | | } |
72 | | |
73 | | #[cfg(feature = "std")] |
74 | | impl From<Error> for std::io::Error { |
75 | | fn from(err: Error) -> std::io::Error { |
76 | | // TODO(tarcieri): better customize `ErrorKind`? |
77 | | std::io::Error::new(std::io::ErrorKind::InvalidData, err) |
78 | | } |
79 | | } |
80 | | |
81 | | impl core::error::Error for Error {} |