use core::fmt;
use thiserror::Error;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Error, Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Error {
#[error("invalid module format: {0}")]
InvalidFormat(String),
#[error("cryptographic error: {0}")]
Crypto(String),
#[error("signature verification failed: {details}")]
VerificationFailed {
details: String,
},
#[error("flash error: {0}")]
Flash(String),
#[error("delta error: {0}")]
Delta(String),
#[error("storage error: {0}")]
Storage(String),
#[cfg(feature = "std")]
#[error("I/O error: {0}")]
Io(String),
#[error("invalid parameter: {0}")]
InvalidParameter(String),
#[error("capacity exceeded: {0}")]
CapacityExceeded(String),
#[error("version mismatch: expected {expected}, found {found}")]
VersionMismatch {
expected: u16,
found: u16,
},
#[error("anti-rollback: current version {current}, attempted {attempted}")]
AntiRollback {
current: u64,
attempted: u64,
},
}
impl Error {
pub fn invalid_format(msg: impl Into<String>) -> Self {
Self::InvalidFormat(msg.into())
}
pub fn crypto(msg: impl Into<String>) -> Self {
Self::Crypto(msg.into())
}
pub fn flash(msg: impl Into<String>) -> Self {
Self::Flash(msg.into())
}
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())
}
}