speck-core 0.2.0

Secure runtime package manager for MMU-less microcontrollers
Documentation
//! Error types for Speck operations

use core::fmt;
use thiserror::Error;

/// Result type alias for Speck operations
pub type Result<T> = core::result::Result<T, Error>;

/// Main error type for the Speck library
#[derive(Error, Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Error {
    /// Invalid module format or corrupted data
    #[error("invalid module format: {0}")]
    InvalidFormat(String),
    
    /// Cryptographic operation failure
    #[error("cryptographic error: {0}")]
    Crypto(String),
    
    /// Signature verification failed
    #[error("signature verification failed: {details}")]
    VerificationFailed {
        /// Details about the failure
        details: String,
    },
    
    /// Flash storage operation error
    #[error("flash error: {0}")]
    Flash(String),
    
    /// Delta patch application error
    #[error("delta error: {0}")]
    Delta(String),
    
    /// Storage management error
    #[error("storage error: {0}")]
    Storage(String),
    
    /// I/O error (wrapper for std::io::Error in std builds)
    #[cfg(feature = "std")]
    #[error("I/O error: {0}")]
    Io(String),
    
    /// Invalid parameter provided
    #[error("invalid parameter: {0}")]
    InvalidParameter(String),
    
    /// Operation would exceed capacity limits
    #[error("capacity exceeded: {0}")]
    CapacityExceeded(String),
    
    /// Version mismatch or unsupported format
    #[error("version mismatch: expected {expected}, found {found}")]
    VersionMismatch {
        /// Expected version
        expected: u16,
        /// Found version
        found: u16,
    },
    
    /// Anti-rollback protection triggered
    #[error("anti-rollback: current version {current}, attempted {attempted}")]
    AntiRollback {
        /// Current installed version
        current: u64,
        /// Attempted version
        attempted: u64,
    },
}

impl Error {
    /// Create a new invalid format error
    pub fn invalid_format(msg: impl Into<String>) -> Self {
        Self::InvalidFormat(msg.into())
    }
    
    /// Create a new crypto error
    pub fn crypto(msg: impl Into<String>) -> Self {
        Self::Crypto(msg.into())
    }
    
    /// Create a new flash error
    pub fn flash(msg: impl Into<String>) -> Self {
        Self::Flash(msg.into())
    }
    
    /// Create a new delta error
    pub fn delta(msg: impl Into<String>) -> Self {
        Self::Delta(msg.into())
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Io(e.to_string())
    }
}

impl From<ed25519_dalek::ed25519::Error> for Error {
    fn from(e: ed25519_dalek::ed25519::Error) -> Self {
        Error::Crypto(e.to_string())
    }
}