1use core::fmt::{self, Display};
4
5pub type Result<T> = core::result::Result<T, Error>;
7
8#[cfg(any(feature = "pkcs8", feature = "sec1"))]
10pub type DecodeResult<T> = core::result::Result<T, DecodeError>;
11
12#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14pub struct Error;
15
16impl core::error::Error for Error {}
17
18impl Display for Error {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 f.write_str("crypto error")
21 }
22}
23
24impl From<base16ct::Error> for Error {
25 fn from(_: base16ct::Error) -> Error {
26 Error
27 }
28}
29
30impl From<core::array::TryFromSliceError> for Error {
31 fn from(_: core::array::TryFromSliceError) -> Error {
32 Error
33 }
34}
35
36#[cfg(feature = "pkcs8")]
37impl From<pkcs8::Error> for Error {
38 fn from(_: pkcs8::Error) -> Error {
39 Error
40 }
41}
42
43#[cfg(feature = "sec1")]
44impl From<sec1::Error> for Error {
45 fn from(_: sec1::Error) -> Error {
46 Error
47 }
48}
49
50#[cfg(feature = "sec1")]
52#[derive(Debug, Copy, Clone, PartialEq, Eq)]
53#[non_exhaustive]
54pub enum DecodeError {
55 #[cfg(feature = "pem")]
57 Pem(pem_rfc7468::Error),
58
59 #[cfg(feature = "pkcs8")]
61 Pkcs8(::pkcs8::Error),
62
63 Sec1(::sec1::Error),
65}
66
67#[cfg(feature = "pem")]
68impl From<pem_rfc7468::Error> for DecodeError {
69 #[inline(always)]
70 fn from(error: pem_rfc7468::Error) -> Self {
71 Self::Pem(error)
72 }
73}
74
75#[cfg(feature = "pkcs8")]
76impl From<::pkcs8::Error> for DecodeError {
77 #[inline(always)]
78 fn from(error: ::pkcs8::Error) -> Self {
79 Self::Pkcs8(error)
80 }
81}
82
83#[cfg(feature = "sec1")]
84impl From<::sec1::Error> for DecodeError {
85 #[inline(always)]
86 fn from(error: ::sec1::Error) -> Self {
87 Self::Sec1(error)
88 }
89}
90
91#[cfg(feature = "sec1")]
92impl Display for DecodeError {
93 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
94 match self {
95 #[cfg(feature = "pem")]
96 Self::Pem(error) => write!(fmt, "couldn't parse PEM: {error}"),
97 #[cfg(feature = "pkcs8")]
98 Self::Pkcs8(error) => write!(fmt, "couldn't parse PKCS#8 key: {error}"),
99 Self::Sec1(error) => write!(fmt, "couldn't parse SEC1 key: {error}"),
100 }
101 }
102}
103
104#[cfg(feature = "sec1")]
105impl core::error::Error for DecodeError {
106 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
107 match self {
108 #[cfg(feature = "pem")]
109 Self::Pem(error) => Some(error),
110 #[cfg(feature = "pkcs8")]
111 Self::Pkcs8(error) => Some(error),
112 Self::Sec1(error) => Some(error),
113 }
114 }
115}