Skip to main content

speck_core/
error.rs

1//! Error types for Speck operations
2
3use core::fmt;
4use thiserror::Error;
5
6/// Result type alias for Speck operations
7pub type Result<T> = core::result::Result<T, Error>;
8
9/// Main error type for the Speck library
10#[derive(Error, Debug, Clone, PartialEq)]
11#[non_exhaustive]
12pub enum Error {
13    /// Invalid module format or corrupted data
14    #[error("invalid module format: {0}")]
15    InvalidFormat(String),
16    
17    /// Cryptographic operation failure
18    #[error("cryptographic error: {0}")]
19    Crypto(String),
20    
21    /// Signature verification failed
22    #[error("signature verification failed: {details}")]
23    VerificationFailed {
24        /// Details about the failure
25        details: String,
26    },
27    
28    /// Flash storage operation error
29    #[error("flash error: {0}")]
30    Flash(String),
31    
32    /// Delta patch application error
33    #[error("delta error: {0}")]
34    Delta(String),
35    
36    /// Storage management error
37    #[error("storage error: {0}")]
38    Storage(String),
39    
40    /// I/O error (wrapper for std::io::Error in std builds)
41    #[cfg(feature = "std")]
42    #[error("I/O error: {0}")]
43    Io(String),
44    
45    /// Invalid parameter provided
46    #[error("invalid parameter: {0}")]
47    InvalidParameter(String),
48    
49    /// Operation would exceed capacity limits
50    #[error("capacity exceeded: {0}")]
51    CapacityExceeded(String),
52    
53    /// Version mismatch or unsupported format
54    #[error("version mismatch: expected {expected}, found {found}")]
55    VersionMismatch {
56        /// Expected version
57        expected: u16,
58        /// Found version
59        found: u16,
60    },
61    
62    /// Anti-rollback protection triggered
63    #[error("anti-rollback: current version {current}, attempted {attempted}")]
64    AntiRollback {
65        /// Current installed version
66        current: u64,
67        /// Attempted version
68        attempted: u64,
69    },
70}
71
72impl Error {
73    /// Create a new invalid format error
74    pub fn invalid_format(msg: impl Into<String>) -> Self {
75        Self::InvalidFormat(msg.into())
76    }
77    
78    /// Create a new crypto error
79    pub fn crypto(msg: impl Into<String>) -> Self {
80        Self::Crypto(msg.into())
81    }
82    
83    /// Create a new flash error
84    pub fn flash(msg: impl Into<String>) -> Self {
85        Self::Flash(msg.into())
86    }
87    
88    /// Create a new delta error
89    pub fn delta(msg: impl Into<String>) -> Self {
90        Self::Delta(msg.into())
91    }
92}
93
94#[cfg(feature = "std")]
95impl From<std::io::Error> for Error {
96    fn from(e: std::io::Error) -> Self {
97        Error::Io(e.to_string())
98    }
99}
100
101impl From<ed25519_dalek::ed25519::Error> for Error {
102    fn from(e: ed25519_dalek::ed25519::Error) -> Self {
103        Error::Crypto(e.to_string())
104    }
105}