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!(
64                    f,
65                    "unexpected PEM type label: expecting \"BEGIN {expected}\""
66                )
67            }
68        }
69    }
70}
71
72impl core::error::Error for Error {}
73
74impl From<base64ct::Error> for Error {
75    fn from(err: base64ct::Error) -> Error {
76        Error::Base64(err)
77    }
78}
79
80impl From<base64ct::InvalidLengthError> for Error {
81    fn from(_: base64ct::InvalidLengthError) -> Error {
82        Error::Length
83    }
84}
85
86impl From<core::str::Utf8Error> for Error {
87    fn from(_: core::str::Utf8Error) -> Error {
88        Error::CharacterEncoding
89    }
90}
91
92#[cfg(feature = "std")]
93impl From<Error> for std::io::Error {
94    fn from(err: Error) -> std::io::Error {
95        let kind = match err {
96            Error::Base64(err) => return err.into(), // Use existing conversion
97            Error::CharacterEncoding
98            | Error::EncapsulatedText
99            | Error::Label
100            | Error::Preamble
101            | Error::PreEncapsulationBoundary
102            | Error::PostEncapsulationBoundary => std::io::ErrorKind::InvalidData,
103            Error::Length => std::io::ErrorKind::UnexpectedEof,
104            _ => std::io::ErrorKind::Other,
105        };
106
107        std::io::Error::new(kind, err)
108    }
109}