mismall 2.0.0

Streaming Huffman compression library with AES-256-GCM encryption and archive support
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Error types for mismall compression library
//!
//! This module defines a comprehensive error hierarchy that provides detailed
//! information about what went wrong during compression, decompression,
//! or archive operations, including rich context and helpful suggestions.
//!
//! # Examples
//!
//! ```rust
//! use mismall::error::{MismallError, Result};
//!
//! fn process_file() -> Result<()> {
//!     // This will automatically convert std::io::Error to MismallError
//!     std::fs::read_to_string("file.txt")?;
//!     Ok(())
//! }
//!
//! fn handle_error(error: MismallError) {
//!     match error {
//!         MismallError::Compression { error, .. } => {
//!             eprintln!("Compression failed: {}", error);
//!         }
//!         MismallError::Decompression { error, .. } => {
//!             eprintln!("Decompression failed: {}", error);
//!         }
//!         MismallError::Archive { error, .. } => {
//!             eprintln!("Archive operation failed: {}", error);
//!         }
//!         MismallError::Io { error, .. } => {
//!             eprintln!("I/O error: {}", error);
//!         }
//!         MismallError::InvalidInput { message, .. } => {
//!             eprintln!("Invalid input: {}", message);
//!         }
//!         MismallError::Unsupported { operation, .. } => {
//!             eprintln!("Unsupported operation: {}", operation);
//!         }
//!     }
//! }
//! ```

pub mod context;

use context::{ErrorContext, Suggestion};
use std::fmt;

/// Result type alias for convenience
pub type Result<T> = std::result::Result<T, MismallError>;

/// Specific compression error types
#[derive(Debug)]
pub enum CompressionError {
    /// Invalid chunk size provided
    InvalidChunkSize(usize),
    /// Failed to read input data
    InputRead(String),
    /// Failed to write output data
    OutputWrite(String),
    /// Compression algorithm failed
    CompressionFailed(String),
    /// Encryption error during compression
    Encryption(String),
}

impl fmt::Display for CompressionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CompressionError::InvalidChunkSize(size) => {
                write!(
                    f,
                    "Invalid chunk size: {} (must be between 1KB and 100MB)",
                    size
                )
            }
            CompressionError::InputRead(msg) => {
                write!(f, "Failed to read input: {}", msg)
            }
            CompressionError::OutputWrite(msg) => {
                write!(f, "Failed to write output: {}", msg)
            }
            CompressionError::CompressionFailed(msg) => {
                write!(f, "Compression failed: {}", msg)
            }
            CompressionError::Encryption(msg) => {
                write!(f, "Encryption error: {}", msg)
            }
        }
    }
}

impl std::error::Error for CompressionError {}

/// Specific decompression error types
#[derive(Debug)]
pub enum DecompressionError {
    /// Invalid chunk size provided
    InvalidChunkSize(usize),
    /// Failed to read input data
    InputRead(String),
    /// Failed to write output data
    OutputWrite(String),
    /// Decompression algorithm failed
    DecompressionFailed(String),
    /// Invalid file format
    InvalidFormat(String),
    /// Corrupted data detected
    CorruptedData(String),
    /// Decryption error during decompression
    DecryptionError(String),
}

impl fmt::Display for DecompressionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DecompressionError::InvalidChunkSize(size) => {
                write!(
                    f,
                    "Invalid chunk size: {} (must be between 1KB and 100MB)",
                    size
                )
            }
            DecompressionError::InputRead(msg) => {
                write!(f, "Failed to read input: {}", msg)
            }
            DecompressionError::OutputWrite(msg) => {
                write!(f, "Failed to write output: {}", msg)
            }
            DecompressionError::DecompressionFailed(msg) => {
                write!(f, "Decompression failed: {}", msg)
            }
            DecompressionError::InvalidFormat(msg) => {
                write!(f, "Invalid file format: {}", msg)
            }
            DecompressionError::CorruptedData(msg) => {
                write!(f, "Corrupted data: {}", msg)
            }
            DecompressionError::DecryptionError(msg) => {
                write!(f, "Decryption error: {}", msg)
            }
        }
    }
}

impl std::error::Error for DecompressionError {}

/// Specific archive error types
#[cfg(feature = "archives")]
#[derive(Debug)]
pub enum ArchiveError {
    /// Failed to create archive
    Creation(String),
    /// Too many files in archive
    TooManyFiles(usize),
    /// Archive too large
    TooLarge(u64),
    /// Failed to extract from archive
    Extraction(String),
    /// Invalid archive format
    InvalidFormat(String),
    /// File not found in archive
    FileNotFound(String),
}

#[cfg(feature = "archives")]
impl fmt::Display for ArchiveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ArchiveError::Creation(msg) => {
                write!(f, "Archive creation failed: {}", msg)
            }
            ArchiveError::TooManyFiles(count) => {
                write!(f, "Too many files in archive: {} (max 65535)", count)
            }
            ArchiveError::TooLarge(size) => {
                write!(f, "Archive too large: {} bytes (max 4GB)", size)
            }
            ArchiveError::Extraction(msg) => {
                write!(f, "Archive extraction failed: {}", msg)
            }
            ArchiveError::InvalidFormat(msg) => {
                write!(f, "Invalid archive format: {}", msg)
            }
            ArchiveError::FileNotFound(name) => {
                write!(f, "File not found in archive: {}", name)
            }
        }
    }
}

#[cfg(feature = "archives")]
impl std::error::Error for ArchiveError {}

/// Main error type for all mismall operations
#[derive(Debug)]
#[allow(clippy::result_large_err)] // All variants are large due to context/suggestions, this is expected
#[allow(clippy::result_large_err)]
pub enum MismallError {
    /// Errors that occur during compression operations
    Compression {
        error: CompressionError,
        context: Option<ErrorContext>,
        suggestion: Option<Suggestion>,
    },
    /// Errors that occur during decompression operations
    Decompression {
        error: DecompressionError,
        context: Option<ErrorContext>,
        suggestion: Option<Suggestion>,
    },
    /// Errors that occur during archive operations
    #[cfg(feature = "archives")]
    Archive {
        error: ArchiveError,
        context: Option<ErrorContext>,
        suggestion: Option<Suggestion>,
    },
    /// I/O related errors
    Io {
        error: std::io::Error,
        context: Option<ErrorContext>,
        suggestion: Option<Suggestion>,
    },
    /// Invalid input parameters
    InvalidInput {
        message: String,
        context: Option<ErrorContext>,
        suggestion: Option<Suggestion>,
    },
    /// Unsupported operation
    Unsupported {
        operation: String,
        context: Option<ErrorContext>,
        suggestion: Option<Suggestion>,
    },
}

impl fmt::Display for MismallError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MismallError::Compression { error, .. } => {
                write!(f, "Compression error: {}", error)
            }
            MismallError::Decompression { error, .. } => {
                write!(f, "Decompression error: {}", error)
            }
            #[cfg(feature = "archives")]
            MismallError::Archive { error, .. } => {
                write!(f, "Archive error: {}", error)
            }
            MismallError::Io { error, .. } => {
                write!(f, "I/O error: {}", error)
            }
            MismallError::InvalidInput { message, .. } => {
                write!(f, "Invalid input: {}", message)
            }
            MismallError::Unsupported { operation, .. } => {
                write!(f, "Unsupported operation: {}", operation)
            }
        }
    }
}

impl std::error::Error for MismallError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            MismallError::Compression { error, .. } => Some(error),
            MismallError::Decompression { error, .. } => Some(error),
            #[cfg(feature = "archives")]
            MismallError::Archive { error, .. } => Some(error),
            MismallError::Io { error, .. } => Some(error),
            MismallError::InvalidInput { .. } | MismallError::Unsupported { .. } => None,
        }
    }
}

/// Extension trait for adding context to MismallError results
pub trait ErrorExt {
    /// Add context information to an error
    fn with_context(self, context: ErrorContext) -> Self;

    /// Add a helpful suggestion to an error
    fn with_suggestion(self, suggestion: Suggestion) -> Self;
}

impl ErrorExt for crate::error::Result<()> {
    fn with_context(self, context: ErrorContext) -> Self {
        self.map_err(|e| match e {
            MismallError::Compression { error, .. } => MismallError::Compression {
                error,
                context: Some(context),
                suggestion: None,
            },
            MismallError::Decompression { error, .. } => MismallError::Decompression {
                error,
                context: Some(context),
                suggestion: None,
            },
            #[cfg(feature = "archives")]
            MismallError::Archive { error, .. } => MismallError::Archive {
                error,
                context: Some(context),
                suggestion: None,
            },
            MismallError::Io { error, .. } => MismallError::Io {
                error,
                context: Some(context),
                suggestion: None,
            },
            MismallError::InvalidInput { message, .. } => MismallError::InvalidInput {
                message,
                context: Some(context),
                suggestion: None,
            },
            MismallError::Unsupported { operation, .. } => MismallError::Unsupported {
                operation,
                context: Some(context),
                suggestion: None,
            },
        })
    }

    fn with_suggestion(self, suggestion: Suggestion) -> Self {
        self.map_err(|e| match e {
            MismallError::Compression { error, .. } => MismallError::Compression {
                error,
                context: None,
                suggestion: Some(suggestion),
            },
            MismallError::Decompression { error, .. } => MismallError::Decompression {
                error,
                context: None,
                suggestion: Some(suggestion),
            },
            #[cfg(feature = "archives")]
            MismallError::Archive { error, .. } => MismallError::Archive {
                error,
                context: None,
                suggestion: Some(suggestion),
            },
            MismallError::Io { error, .. } => MismallError::Io {
                error,
                context: None,
                suggestion: Some(suggestion),
            },
            MismallError::InvalidInput { message, .. } => MismallError::InvalidInput {
                message,
                context: None,
                suggestion: Some(suggestion),
            },
            MismallError::Unsupported { operation, .. } => MismallError::Unsupported {
                operation,
                context: None,
                suggestion: Some(suggestion),
            },
        })
    }
}

/// Enhanced error with rich context and suggestions
#[derive(Debug)]
pub struct EnhancedError<T> {
    /// The original error that occurred
    pub original: T,

    /// Context information about when and where an error occurred
    pub context: Option<ErrorContext>,

    /// Helpful suggestion for resolving the error
    pub suggestion: Option<Suggestion>,
}

impl<T> EnhancedError<T> {
    /// Create a new enhanced error
    pub fn new(original: T) -> Self {
        Self {
            original,
            context: None,
            suggestion: None,
        }
    }

    /// Create a new enhanced error with context
    pub fn new_with_context(original: T, context: ErrorContext) -> Self {
        Self {
            original,
            context: Some(context),
            suggestion: None,
        }
    }

    /// Create a new enhanced error with suggestion
    pub fn new_with_suggestion(original: T, suggestion: Suggestion) -> Self {
        Self {
            original,
            context: None,
            suggestion: Some(suggestion),
        }
    }
}

// Conversion traits for common error types
impl From<std::io::Error> for MismallError {
    fn from(err: std::io::Error) -> Self {
        MismallError::Io {
            error: err,
            context: None,
            suggestion: None,
        }
    }
}

impl From<CompressionError> for MismallError {
    fn from(err: CompressionError) -> Self {
        MismallError::Compression {
            error: err,
            context: None,
            suggestion: None,
        }
    }
}

impl From<DecompressionError> for MismallError {
    fn from(err: DecompressionError) -> Self {
        MismallError::Decompression {
            error: err,
            context: None,
            suggestion: None,
        }
    }
}

#[cfg(feature = "archives")]
impl From<ArchiveError> for MismallError {
    fn from(err: ArchiveError) -> Self {
        MismallError::Archive {
            error: err,
            context: None,
            suggestion: None,
        }
    }
}

impl From<Box<dyn std::error::Error>> for MismallError {
    fn from(err: Box<dyn std::error::Error>) -> Self {
        MismallError::InvalidInput {
            message: err.to_string(),
            context: None,
            suggestion: None,
        }
    }
}

impl From<&str> for MismallError {
    fn from(err: &str) -> Self {
        MismallError::InvalidInput {
            message: err.to_string(),
            context: None,
            suggestion: None,
        }
    }
}

impl From<String> for MismallError {
    fn from(err: String) -> Self {
        MismallError::InvalidInput {
            message: err,
            context: None,
            suggestion: None,
        }
    }
}