1use crate::LabelError;
4use core::fmt;
5
6pub type Result<T> = core::result::Result<T, Error>;
8
9#[derive(Clone, Debug, Eq, PartialEq)]
11#[non_exhaustive]
12pub enum Error {
13 #[cfg(feature = "alloc")]
15 Alloc(alloc::collections::TryReserveError),
16
17 #[cfg(feature = "base64")]
19 Base64(base64ct::Error),
20
21 CharacterEncoding,
23
24 Failed,
26
27 Label(LabelError),
29
30 Length,
32
33 MpintEncoding,
35
36 NestingDepth,
38
39 Overflow,
41
42 #[cfg(feature = "pem")]
44 Pem(pem_rfc7468::Error),
45
46 TrailingData {
48 remaining: usize,
50 },
51
52 InvalidDiscriminant(u128),
54}
55
56impl core::error::Error for Error {
57 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
58 match self {
59 #[cfg(feature = "alloc")]
60 Self::Alloc(err) => Some(err),
61 #[cfg(feature = "base64")]
62 Self::Base64(err) => Some(err),
63 #[cfg(feature = "pem")]
64 Self::Pem(err) => Some(err),
65 _ => None,
66 }
67 }
68}
69
70impl fmt::Display for Error {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self {
73 #[cfg(feature = "alloc")]
74 Error::Alloc(err) => write!(f, "allocation failed: {err}"),
75 #[cfg(feature = "base64")]
76 Error::Base64(err) => write!(f, "Base64 encoding error: {err}"),
77 Error::CharacterEncoding => write!(f, "character encoding invalid"),
78 Error::Failed => write!(f, "decoder previously failed"),
79 Error::Label(err) => write!(f, "{}", err),
80 Error::Length => write!(f, "length invalid"),
81 Error::MpintEncoding => write!(f, "`mpint` encoding invalid"),
82 Error::NestingDepth => write!(f, "maximum nesting depth exceeded"),
83 Error::Overflow => write!(f, "internal overflow error"),
84 #[cfg(feature = "pem")]
85 Error::Pem(err) => write!(f, "{err}"),
86 Error::TrailingData { remaining } => write!(
87 f,
88 "unexpected trailing data at end of message ({remaining} bytes)",
89 ),
90 Error::InvalidDiscriminant(discriminant) => {
91 write!(f, "invalid discriminant value: {discriminant}")
92 }
93 }
94 }
95}
96
97impl From<LabelError> for Error {
98 fn from(err: LabelError) -> Error {
99 Error::Label(err)
100 }
101}
102
103impl From<core::num::TryFromIntError> for Error {
104 fn from(_: core::num::TryFromIntError) -> Error {
105 Error::Overflow
106 }
107}
108
109impl From<core::str::Utf8Error> for Error {
110 fn from(_: core::str::Utf8Error) -> Error {
111 Error::CharacterEncoding
112 }
113}
114
115#[cfg(feature = "alloc")]
116impl From<alloc::collections::TryReserveError> for Error {
117 fn from(err: alloc::collections::TryReserveError) -> Error {
118 Error::Alloc(err)
119 }
120}
121
122#[cfg(feature = "alloc")]
123impl From<alloc::string::FromUtf8Error> for Error {
124 fn from(_: alloc::string::FromUtf8Error) -> Error {
125 Error::CharacterEncoding
126 }
127}
128
129#[cfg(feature = "base64")]
130impl From<base64ct::Error> for Error {
131 fn from(err: base64ct::Error) -> Error {
132 Error::Base64(err)
133 }
134}
135
136#[cfg(feature = "base64")]
137impl From<base64ct::InvalidLengthError> for Error {
138 fn from(_: base64ct::InvalidLengthError) -> Error {
139 Error::Length
140 }
141}
142
143#[cfg(feature = "bigint")]
144impl From<bigint::DecodeError> for Error {
145 fn from(_: bigint::DecodeError) -> Error {
146 Error::MpintEncoding
147 }
148}
149
150#[cfg(feature = "pem")]
151impl From<pem_rfc7468::Error> for Error {
152 fn from(err: pem_rfc7468::Error) -> Error {
153 Error::Pem(err)
154 }
155}