Skip to main content

saorsa_logic/
error.rs

1// Copyright 2024 Saorsa Labs Limited
2//
3// Licensed under the Apache License, Version 2.0 or MIT license, at your option.
4// This file may not be copied, modified, or distributed except according to those terms.
5
6//! Error types for `saorsa-logic`.
7//!
8//! Designed to be `no_std` compatible while providing useful error information.
9
10use core::fmt;
11
12/// Result type for `saorsa-logic` operations.
13pub type LogicResult<T> = Result<T, LogicError>;
14
15/// Errors that can occur in `saorsa-logic`.
16///
17/// This enum is designed to be compact and `no_std` compatible.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum LogicError {
20    /// Invalid input length for an operation.
21    InvalidLength {
22        /// What was being validated
23        field: &'static str,
24        /// Expected length
25        expected: usize,
26        /// Actual length received
27        actual: usize,
28    },
29
30    /// Hash verification failed.
31    HashMismatch,
32
33    /// Signature verification failed.
34    SignatureInvalid,
35
36    /// Merkle proof verification failed.
37    MerkleProofInvalid,
38
39    /// `EntangledId` verification failed.
40    EntangledIdMismatch,
41
42    /// Binary hash not in allowlist.
43    BinaryNotAllowed,
44
45    /// Timestamp validation failed (e.g., sunset expired).
46    TimestampInvalid,
47
48    /// Nonce validation failed.
49    NonceInvalid,
50
51    /// Public key format is invalid.
52    PublicKeyInvalid,
53}
54
55impl fmt::Display for LogicError {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        match self {
58            Self::InvalidLength {
59                field,
60                expected,
61                actual,
62            } => {
63                write!(
64                    f,
65                    "invalid length for {field}: expected {expected}, got {actual}"
66                )
67            }
68            Self::HashMismatch => write!(f, "hash verification failed"),
69            Self::SignatureInvalid => write!(f, "signature verification failed"),
70            Self::MerkleProofInvalid => write!(f, "merkle proof verification failed"),
71            Self::EntangledIdMismatch => write!(f, "entangled ID verification failed"),
72            Self::BinaryNotAllowed => write!(f, "binary hash not in allowlist"),
73            Self::TimestampInvalid => write!(f, "timestamp validation failed"),
74            Self::NonceInvalid => write!(f, "nonce validation failed"),
75            Self::PublicKeyInvalid => write!(f, "public key format is invalid"),
76        }
77    }
78}
79
80#[cfg(feature = "std")]
81impl std::error::Error for LogicError {}