1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Error types and handling for the CertKit library.
//!
//! This module defines the comprehensive error system used throughout CertKit,
//! providing detailed error information for debugging and error handling.
//!
//! # Error Categories
//!
//! - **Encoding/Decoding**: Issues with data format conversion
//! - **Input Validation**: Problems with user-provided data
//! - **Cryptographic Operations**: Key generation and signing failures
//! - **Certificate Operations**: Certificate creation and validation issues
//!
//! # Examples
//!
//! ```rust
//! use certkit::{key::KeyPair, error::CertKitError};
//!
//! // Handle different error types
//! match KeyPair::import_from_pkcs8_pem("invalid pem") {
//! Ok(key) => println!("Key imported successfully"),
//! Err(CertKitError::DecodingError(msg)) => {
//! eprintln!("Failed to decode PEM: {}", msg);
//! }
//! Err(CertKitError::InvalidInput(msg)) => {
//! eprintln!("Invalid input provided: {}", msg);
//! }
//! Err(e) => eprintln!("Other error: {}", e),
//! }
//! ```
use Error;
/// Represents errors that can occur in the CertKit library.
///
/// This enum provides comprehensive error reporting for all operations
/// in CertKit, with detailed error messages and context information
/// to help with debugging and error handling.
///
/// # Error Handling Strategy
///
/// CertKit uses a centralized error type that covers all possible failure
/// scenarios. Each error variant includes a descriptive message and,
/// where appropriate, the underlying cause of the error.
///
/// # Examples
///
/// ## Basic Error Handling
///
/// ```rust
/// use certkit::{key::KeyPair, error::CertKitError};
///
/// fn handle_key_generation() -> Result<(), CertKitError> {
/// let key = KeyPair::generate_rsa(2048)?;
/// println!("Key generated successfully");
/// Ok(())
/// }
///
/// match handle_key_generation() {
/// Ok(()) => println!("Success!"),
/// Err(e) => eprintln!("Error: {}", e),
/// }
/// ```
///
/// ## Specific Error Type Matching
///
/// ```rust
/// use certkit::{key::KeyPair, error::CertKitError};
///
/// let result = KeyPair::import_from_pkcs8_pem("invalid data");
/// match result {
/// Ok(key) => println!("Key imported"),
/// Err(CertKitError::DecodingError(msg)) => {
/// eprintln!("Decoding failed: {}", msg);
/// // Handle decoding-specific error
/// }
/// Err(CertKitError::InvalidInput(msg)) => {
/// eprintln!("Invalid input: {}", msg);
/// // Handle input validation error
/// }
/// Err(e) => eprintln!("Unexpected error: {}", e),
/// }
/// ```