Skip to main content

hexz_common/
error.rs

1//! Unified error type and result alias for Hexz.
2//!
3//! Defines `Error` and the crate-wide `Result<T>` alias so that all
4//! library and CLI code can report I/O, format, compression, and
5//! encryption failures through a single type.
6
7use thiserror::Error;
8
9/// Unified error type for all Hexz library and CLI operations.
10///
11/// **Architectural intent:** Provides a single error surface that callers can
12/// use for I/O, format, compression, encryption, and feature-negotiation
13/// failures, enabling consistent reporting across crates.
14///
15/// **Constraints:** Variants must remain serializable as user-facing strings
16/// and stable enough for log analysis; binary compatibility is not guaranteed
17/// across releases.
18///
19/// **Side effects:** Many variants wrap underlying error types; formatting the
20/// error may allocate and may expose details from lower layers that should not
21/// be relied upon for programmatic decisions.
22#[derive(Error, Debug)]
23pub enum Error {
24    #[error("IO Error: {0}")]
25    Io(#[from] std::io::Error),
26
27    #[error("Serialization Error: {0}")]
28    Serialization(#[from] bincode::Error),
29
30    #[error("Compression Error: {0}")]
31    Compression(String),
32
33    #[error("Encryption Error: {0}")]
34    Encryption(String),
35
36    #[error("Corrupted Data: Checksum mismatch at block {0}")]
37    Corruption(u64),
38
39    #[error("Invalid Format: {0}")]
40    Format(String),
41
42    #[error("Block index {0} out of bounds")]
43    OutOfBounds(u64),
44
45    #[error("Feature Not Supported: {0}")]
46    NotSupported(String),
47}
48
49/// Convenience result alias for functions that can fail with `Error`.
50///
51/// **Architectural intent:** Standardizes the error channel across the
52/// codebase so APIs clearly communicate that failures are domain-specific
53/// Hexz errors rather than arbitrary `std` errors.
54///
55/// **Constraints:** Callers should treat `Error` as opaque and avoid
56/// depending on the exact display text; matching on variants is preferred for
57/// structured handling.
58///
59/// **Side effects:** No additional side effects beyond those of the underlying
60/// operation whose result is being wrapped.
61pub type Result<T> = std::result::Result<T, Error>;