Skip to main content

secure_gate/
error.rs

1// secure-gate\src\error.rs
2
3//! Centralized error types for the secure-gate crate.
4
5/// Error type for slice conversion operations.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
7pub enum FromSliceError {
8    #[error("slice length mismatch")]
9    LengthMismatch,
10}
11
12#[cfg(feature = "encoding-bech32")]
13/// Error type for Bech32 operations (encoding and decoding).
14#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
15pub enum Bech32Error {
16    #[error("invalid Human-Readable Part (HRP)")]
17    InvalidHrp,
18    #[error("bit conversion failed")]
19    ConversionFailed,
20    #[error("bech32 operation failed")]
21    OperationFailed,
22    #[error("unexpected HRP: expected {expected}, got {got}")]
23    UnexpectedHrp { expected: String, got: String },
24    #[error("decoded length mismatch: expected {expected}, got {got}")]
25    InvalidLength { expected: usize, got: usize },
26}
27
28#[cfg(feature = "encoding-base64")]
29/// Error type for Base64 decoding operations.
30#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
31pub enum Base64Error {
32    #[error("invalid base64 string")]
33    InvalidBase64,
34    #[error("decoded length mismatch: expected {expected}, got {got}")]
35    InvalidLength { expected: usize, got: usize },
36}
37
38#[cfg(feature = "encoding-hex")]
39/// Error type for Hex decoding operations.
40#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
41pub enum HexError {
42    #[error("invalid hex string")]
43    InvalidHex,
44    #[error("decoded length mismatch: expected {expected}, got {got}")]
45    InvalidLength { expected: usize, got: usize },
46}
47
48/// Unified error type for decoding operations across formats.
49#[derive(Clone, Debug, thiserror::Error)]
50pub enum DecodingError {
51    #[cfg(feature = "encoding-bech32")]
52    #[error("invalid bech32 string")]
53    InvalidBech32,
54    #[cfg(feature = "encoding-base64")]
55    #[error("invalid base64 string")]
56    InvalidBase64,
57    #[cfg(feature = "encoding-hex")]
58    #[error("invalid hex string")]
59    InvalidHex,
60    #[error("invalid encoding: {hint}")]
61    InvalidEncoding {
62        /// Additional hint for debugging, e.g., "string does not match any supported format. Attempted order: [Bech32, Hex, Base64Url]".
63        /// In production, this can be redacted if it risks leaking metadata.
64        hint: String,
65    },
66}