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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! # Error types for PNGer Steganography Operations
//!
//! This module defines comprehensive error types for all PNGer operations, providing
//! detailed information about failures during embedding and extraction processes.
//! The error types are designed to help users diagnose issues and implement appropriate
//! error handling strategies.
//!
//! ## Error Hierarchy
//!
//! PNGer errors are organized into logical categories:
//!
//! - **Capacity Errors**: Issues related to image size vs payload size
//! - **Format Errors**: PNG parsing, encoding, or steganographic format issues
//! - **I/O Errors**: File system and data transfer problems
//! - **Cryptographic Errors**: Password, encryption, and random number generation failures
//! - **Processing Errors**: Payload handling and operation mode issues
//!
//! ## Error Handling Patterns
//!
//! ### Basic Pattern Matching
//!
//! ```rust
//! use pnger::{embed_payload_from_file, PngerError};
//!
//! match embed_payload_from_file("image.png", b"secret") {
//! Ok(result) => println!("Success!"),
//! Err(PngerError::PayloadTooLarge) => println!("Payload too large"),
//! Err(PngerError::FileIo(_)) => println!("File access error"),
//! Err(err) => println!("Other error: {}", err),
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ### Error Recovery Strategies
//!
//! ```rust
//! use pnger::{embed_payload_from_file, PngerError};
//!
//! fn embed_with_retry(image_path: &str, payload: &[u8]) -> Result<Vec<u8>, String> {
//! match embed_payload_from_file(image_path, payload) {
//! Ok(result) => Ok(result),
//! Err(PngerError::PayloadTooLarge) => {
//! // Try with compression or smaller payload
//! Err("Payload too large, try compressing first".to_string())
//! }
//! Err(PngerError::FileIo(_)) => {
//! // Retry with different file path or check permissions
//! Err("File access failed, check path and permissions".to_string())
//! }
//! Err(err) => Err(format!("Embedding failed: {}", err)),
//! }
//! }
//! ```
use io;
use Error;
/// Comprehensive error type for all PNGer steganography operations.
///
/// This enum covers all possible failure modes in the PNGer library, from basic
/// I/O errors to advanced cryptographic failures. Each variant provides specific
/// context about the nature of the failure to enable proper error handling.
///
/// # Error Categories
///
/// ## Capacity Errors
/// - [`PayloadTooLarge`](PngerError::PayloadTooLarge): Payload exceeds image capacity
/// - [`InsufficientCapacity`](PngerError::InsufficientCapacity): Image too small for payload
///
/// ## Format Errors
/// - [`PngDecodingError`](PngerError::PngDecodingError): Invalid or corrupted PNG data
/// - [`PngEncodingError`](PngerError::PngEncodingError): PNG reconstruction failed
/// - [`InvalidFormat`](PngerError::InvalidFormat): Malformed steganographic data
///
/// ## I/O Errors
/// - [`FileIo`](PngerError::FileIo): File system operations failed
/// - [`IoError`](PngerError::IoError): General I/O operations failed
///
/// ## Cryptographic Errors
/// - [`CryptoError`](PngerError::CryptoError): Password derivation or encryption failed
/// - [`RandomGenerationFailed`](PngerError::RandomGenerationFailed): PRNG operations failed
/// - [`InvalidSeedLength`](PngerError::InvalidSeedLength): Invalid cryptographic seed
/// - [`InvalidSaltLength`](PngerError::InvalidSaltLength): Invalid salt for key derivation
///
/// ## Processing Errors
/// - [`PayloadError`](PngerError::PayloadError): Payload processing failed
/// - [`UnsupportedMode`](PngerError::UnsupportedMode): Unsupported operation mode
///
/// # Examples
///
/// ## Basic Error Handling
///
/// ```rust
/// use pnger::{embed_payload_from_file, PngerError};
///
/// match embed_payload_from_file("image.png", b"secret") {
/// Ok(result) => {
/// std::fs::write("output.png", result)?;
/// println!("Embedding successful!");
/// }
/// Err(PngerError::PayloadTooLarge) => {
/// eprintln!("Error: The secret message is too large for this image.");
/// eprintln!("Try using a larger image or smaller payload.");
/// }
/// Err(PngerError::FileIo(io_err)) => {
/// eprintln!("File error: {}", io_err);
/// }
/// Err(other) => {
/// eprintln!("Other error: {}", other);
/// }
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Comprehensive Error Handling
///
/// ```rust
/// use pnger::{extract_payload_from_file_with_options, EmbeddingOptions, Strategy, PngerError};
/// use pnger::strategy::lsb::LSBConfig;
///
/// fn extract_with_fallback(file_path: &str, password: &str) -> Result<Vec<u8>, String> {
/// let strategy = Strategy::LSB(LSBConfig::random().with_password(password.to_string()));
/// let options = EmbeddingOptions::new(strategy);
///
/// match extract_payload_from_file_with_options(file_path, options) {
/// Ok(payload) => Ok(payload),
/// Err(PngerError::CryptoError(_)) => {
/// Err("Incorrect password or corrupted cryptographic data".to_string())
/// }
/// Err(PngerError::InvalidFormat(msg)) => {
/// Err(format!("No valid payload found: {}", msg))
/// }
/// Err(PngerError::PngDecodingError(_)) => {
/// Err("File is not a valid PNG image".to_string())
/// }
/// Err(err) => {
/// Err(format!("Extraction failed: {}", err))
/// }
/// }
/// }
/// ```