1use core::fmt;
4use thiserror::Error;
5
6pub type Result<T> = core::result::Result<T, Error>;
8
9#[derive(Error, Debug, Clone, PartialEq)]
11#[non_exhaustive]
12pub enum Error {
13 #[error("invalid module format: {0}")]
15 InvalidFormat(String),
16
17 #[error("cryptographic error: {0}")]
19 Crypto(String),
20
21 #[error("signature verification failed: {details}")]
23 VerificationFailed {
24 details: String,
26 },
27
28 #[error("flash error: {0}")]
30 Flash(String),
31
32 #[error("delta error: {0}")]
34 Delta(String),
35
36 #[error("storage error: {0}")]
38 Storage(String),
39
40 #[cfg(feature = "std")]
42 #[error("I/O error: {0}")]
43 Io(String),
44
45 #[error("invalid parameter: {0}")]
47 InvalidParameter(String),
48
49 #[error("capacity exceeded: {0}")]
51 CapacityExceeded(String),
52
53 #[error("version mismatch: expected {expected}, found {found}")]
55 VersionMismatch {
56 expected: u16,
58 found: u16,
60 },
61
62 #[error("anti-rollback: current version {current}, attempted {attempted}")]
64 AntiRollback {
65 current: u64,
67 attempted: u64,
69 },
70}
71
72impl Error {
73 pub fn invalid_format(msg: impl Into<String>) -> Self {
75 Self::InvalidFormat(msg.into())
76 }
77
78 pub fn crypto(msg: impl Into<String>) -> Self {
80 Self::Crypto(msg.into())
81 }
82
83 pub fn flash(msg: impl Into<String>) -> Self {
85 Self::Flash(msg.into())
86 }
87
88 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}