Skip to main content

aescrypt_rs/
error.rs

1//! # Error Types
2//!
3//! This module defines the error types used throughout the library.
4//! All operations return [`Result<T, AescryptError>`](AescryptError) for comprehensive error handling.
5
6use thiserror::Error;
7
8/// The error type for all AES Crypt operations.
9///
10/// This enum covers I/O errors, cryptographic errors, header parsing errors,
11/// and version compatibility issues.
12#[derive(Error, Debug)]
13pub enum AescryptError {
14    /// I/O error occurred during file operations.
15    ///
16    /// This variant wraps [`std::io::Error`] and is automatically created
17    /// when I/O operations fail (e.g., file not found, read/write errors).
18    #[error("I/O error: {0}")]
19    Io(#[from] std::io::Error),
20
21    /// Cryptographic operation failed.
22    ///
23    /// This variant is used for errors in cryptographic operations such as:
24    /// - KDF derivation failures
25    /// - Invalid password encoding
26    /// - HMAC verification failures
27    /// - Other cryptographic errors
28    #[error("Crypto error: {0}")]
29    Crypto(String),
30
31    /// Header parsing or validation error.
32    ///
33    /// This variant is used for errors related to AES Crypt file headers:
34    /// - Invalid magic bytes
35    /// - Invalid version byte
36    /// - Invalid reserved byte
37    /// - Invalid KDF iteration count
38    /// - Missing or corrupted header data
39    #[error("Header error: {0}")]
40    Header(String),
41
42    /// Unsupported AES Crypt file version.
43    ///
44    /// This variant is returned when attempting to read a file with a version
45    /// that is not supported (currently only versions 0-3 are supported).
46    /// The contained value is the unsupported version number.
47    #[error("Unsupported version: {0}")]
48    UnsupportedVersion(u8),
49}
50
51impl From<&'static str> for AescryptError {
52    fn from(msg: &'static str) -> Self {
53        AescryptError::Crypto(msg.to_string())
54    }
55}