Coverage Report

Created: 2026-06-14 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pem-rfc7468-0.7.0/src/error.rs
Line
Count
Source
1
//! Error types
2
3
use core::fmt;
4
5
/// Result type with the `pem-rfc7468` crate's [`Error`] type.
6
pub type Result<T> = core::result::Result<T, Error>;
7
8
/// PEM errors.
9
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10
#[non_exhaustive]
11
pub 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
46
impl fmt::Display for Error {
47
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48
0
        match self {
49
0
            Error::Base64(err) => write!(f, "PEM Base64 error: {}", err),
50
0
            Error::CharacterEncoding => f.write_str("PEM character encoding error"),
51
0
            Error::EncapsulatedText => f.write_str("PEM error in encapsulated text"),
52
0
            Error::HeaderDisallowed => f.write_str("PEM headers disallowed by RFC7468"),
53
0
            Error::Label => f.write_str("PEM type label invalid"),
54
0
            Error::Length => f.write_str("PEM length invalid"),
55
0
            Error::Preamble => f.write_str("PEM preamble contains invalid data (NUL byte)"),
56
            Error::PreEncapsulationBoundary => {
57
0
                f.write_str("PEM error in pre-encapsulation boundary")
58
            }
59
            Error::PostEncapsulationBoundary => {
60
0
                f.write_str("PEM error in post-encapsulation boundary")
61
            }
62
0
            Error::UnexpectedTypeLabel { expected } => {
63
0
                write!(f, "unexpected PEM type label: expecting \"{}\"", expected)
64
            }
65
        }
66
0
    }
67
}
68
69
#[cfg(feature = "std")]
70
impl std::error::Error for Error {}
71
72
impl From<base64ct::Error> for Error {
73
0
    fn from(err: base64ct::Error) -> Error {
74
0
        Error::Base64(err)
75
0
    }
76
}
77
78
impl From<base64ct::InvalidLengthError> for Error {
79
0
    fn from(_: base64ct::InvalidLengthError) -> Error {
80
0
        Error::Length
81
0
    }
82
}
83
84
impl From<core::str::Utf8Error> for Error {
85
0
    fn from(_: core::str::Utf8Error) -> Error {
86
0
        Error::CharacterEncoding
87
0
    }
88
}
89
90
#[cfg(feature = "std")]
91
impl 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
}