pem_rfc7468/
error.rs

1//! Error types
2
3use core::fmt;
4
5/// Result type with the `pem-rfc7468` crate's [`Error`] type.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// PEM errors.
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Base64-related errors.
13    Base64(base64ct::Error),
14
15    /// Character encoding-related errors.
16    CharacterEncoding,
17
18    /// Errors in the encapsulated text (which aren't specifically Base64-related).
19    EncapsulatedText,
20
21    /// Header detected in the encapsulated text.
22    HeaderDisallowed,
23
24    /// Invalid label.
25    Label,
26
27    /// Invalid length.
28    Length,
29
30    /// "Preamble" (text before pre-encapsulation boundary) contains invalid data.
31    Preamble,
32
33    /// Errors in the pre-encapsulation boundary.
34    PreEncapsulationBoundary,
35
36    /// Errors in the post-encapsulation boundary.
37    PostEncapsulationBoundary,
38
39    /// Unexpected PEM type label.
40    UnexpectedTypeLabel {
41        /// Type label that was expected.
42        expected: &'static str,
43    },
44}
45
46impl fmt::Display for Error {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Error::Base64(err) => write!(f, "PEM Base64 error: {}", err),
50            Error::CharacterEncoding => f.write_str("PEM character encoding error"),
51            Error::EncapsulatedText => f.write_str("PEM error in encapsulated text"),
52            Error::HeaderDisallowed => f.write_str("PEM headers disallowed by RFC7468"),
53            Error::Label => f.write_str("PEM type label invalid"),
54            Error::Length => f.write_str("PEM length invalid"),
55            Error::Preamble => f.write_str("PEM preamble contains invalid data (NUL byte)"),
56            Error::PreEncapsulationBoundary => {
57                f.write_str("PEM error in pre-encapsulation boundary")
58            }
59            Error::PostEncapsulationBoundary => {
60                f.write_str("PEM error in post-encapsulation boundary")
61            }
62            Error::UnexpectedTypeLabel { expected } => {
63                write!(f, "unexpected PEM type label: expecting \"{}\"", expected)
64            }
65        }
66    }
67}
68
69#[cfg(feature = "std")]
70impl std::error::Error for Error {}
71
72impl From<base64ct::Error> for Error {
73    fn from(err: base64ct::Error) -> Error {
74        Error::Base64(err)
75    }
76}
77
78impl From<base64ct::InvalidLengthError> for Error {
79    fn from(_: base64ct::InvalidLengthError) -> Error {
80        Error::Length
81    }
82}
83
84impl From<core::str::Utf8Error> for Error {
85    fn from(_: core::str::Utf8Error) -> Error {
86        Error::CharacterEncoding
87    }
88}
89
90#[cfg(feature = "std")]
91impl From<Error> for std::io::Error {
92    fn from(err: Error) -> std::io::Error {
93        let kind = match err {
94            Error::Base64(err) => return err.into(), // Use existing conversion
95            Error::CharacterEncoding
96            | Error::EncapsulatedText
97            | Error::Label
98            | Error::Preamble
99            | Error::PreEncapsulationBoundary
100            | Error::PostEncapsulationBoundary => std::io::ErrorKind::InvalidData,
101            Error::Length => std::io::ErrorKind::UnexpectedEof,
102            _ => std::io::ErrorKind::Other,
103        };
104
105        std::io::Error::new(kind, err)
106    }
107}