Skip to main content

gpg_inspector_lib/
error.rs

1//! Error types for GPG packet parsing.
2//!
3//! This module provides the [`enum@Error`] enum for all parsing failures
4//! and the [`Result`] type alias for convenient error handling.
5
6use thiserror::Error;
7
8/// Errors that can occur during GPG packet parsing.
9///
10/// These errors cover armor decoding failures, binary parsing errors,
11/// and data validation issues.
12#[derive(Error, Debug)]
13pub enum Error {
14    /// The ASCII armor format is invalid (missing headers, malformed structure).
15    #[error("Invalid armor format: {0}")]
16    InvalidArmor(String),
17
18    /// Base64 decoding failed (invalid characters or padding).
19    #[error("Base64 decode error: {0}")]
20    Base64Error(String),
21
22    /// Reached end of data unexpectedly while parsing.
23    ///
24    /// The `usize` indicates the byte position where more data was expected.
25    #[error("Unexpected end of data at position {0}")]
26    UnexpectedEnd(usize),
27
28    /// The packet header byte is invalid (bit 7 not set).
29    ///
30    /// The `usize` indicates the byte position of the invalid header.
31    #[error("Invalid packet header at position {0}")]
32    InvalidPacketHeader(usize),
33
34    /// The packet tag value is not recognized.
35    #[error("Unknown packet tag: {0}")]
36    UnknownPacketTag(u8),
37
38    /// The packet body format is invalid.
39    ///
40    /// Contains the byte position and a description of the error.
41    #[error("Invalid packet format at position {0}: {1}")]
42    InvalidPacketFormat(usize, String),
43
44    /// The CRC24 checksum in the armor doesn't match the computed checksum.
45    #[error("Checksum mismatch: expected {expected:06x}, got {actual:06x}")]
46    ChecksumMismatch {
47        /// The checksum value from the armor footer.
48        expected: u32,
49        /// The computed checksum of the decoded data.
50        actual: u32,
51    },
52
53    /// A timestamp value could not be converted to a valid date/time.
54    #[error("Invalid timestamp: {0}")]
55    InvalidTimestamp(u32),
56}
57
58/// A specialized `Result` type for GPG parsing operations.
59pub type Result<T> = std::result::Result<T, Error>;