1use std::{error::Error, fmt};
24
25use crate::KeyType;
26
27#[derive(Debug)]
29pub struct DecodingError {
30 msg: String,
31 source: Option<Box<dyn Error + Send + Sync>>,
32}
33
34impl DecodingError {
35 #[allow(dead_code)]
36 pub(crate) fn missing_feature(feature_name: &'static str) -> Self {
37 Self {
38 msg: format!("cargo feature `{feature_name}` is not enabled"),
39 source: None,
40 }
41 }
42
43 #[cfg(any(
44 feature = "ecdsa",
45 feature = "secp256k1",
46 feature = "ed25519",
47 feature = "rsa"
48 ))]
49 pub(crate) fn failed_to_parse<E, S>(what: &'static str, source: S) -> Self
50 where
51 E: Error + Send + Sync + 'static,
52 S: Into<Option<E>>,
53 {
54 Self {
55 msg: format!("failed to parse {what}"),
56 source: match source.into() {
57 None => None,
58 Some(e) => Some(Box::new(e)),
59 },
60 }
61 }
62
63 #[cfg(any(
64 feature = "ecdsa",
65 feature = "secp256k1",
66 feature = "ed25519",
67 feature = "rsa"
68 ))]
69 pub(crate) fn bad_protobuf(
70 what: &'static str,
71 source: impl Error + Send + Sync + 'static,
72 ) -> Self {
73 Self {
74 msg: format!("failed to decode {what} from protobuf"),
75 source: Some(Box::new(source)),
76 }
77 }
78
79 #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
80 pub(crate) fn encoding_unsupported(key_type: &'static str) -> Self {
81 Self {
82 msg: format!("encoding {key_type} key to Protobuf is unsupported"),
83 source: None,
84 }
85 }
86}
87
88impl fmt::Display for DecodingError {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 write!(f, "Key decoding error: {}", self.msg)
91 }
92}
93
94impl Error for DecodingError {
95 fn source(&self) -> Option<&(dyn Error + 'static)> {
96 self.source.as_ref().map(|s| &**s as &dyn Error)
97 }
98}
99
100#[derive(Debug)]
102pub struct SigningError {
103 msg: String,
104 source: Option<Box<dyn Error + Send + Sync>>,
105}
106
107impl SigningError {
109 #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
110 pub(crate) fn new<S: ToString>(msg: S) -> Self {
111 Self {
112 msg: msg.to_string(),
113 source: None,
114 }
115 }
116
117 #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
118 pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self {
119 Self {
120 source: Some(Box::new(source)),
121 ..self
122 }
123 }
124}
125
126impl fmt::Display for SigningError {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 write!(f, "Key signing error: {}", self.msg)
129 }
130}
131
132impl Error for SigningError {
133 fn source(&self) -> Option<&(dyn Error + 'static)> {
134 self.source.as_ref().map(|s| &**s as &dyn Error)
135 }
136}
137
138#[derive(Debug)]
140pub struct OtherVariantError {
141 actual: KeyType,
142}
143
144impl OtherVariantError {
145 #[allow(dead_code)] pub(crate) fn new(actual: KeyType) -> OtherVariantError {
147 OtherVariantError { actual }
148 }
149}
150
151impl fmt::Display for OtherVariantError {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 f.write_str(&format!(
154 "Cannot convert to the given type, the actual key type inside is {}",
155 self.actual
156 ))
157 }
158}
159
160impl Error for OtherVariantError {}