1use core::fmt;
4use embedded_storage::nor_flash::NorFlashErrorKind;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum Error {
10 Flash(NorFlashErrorKind),
12 NotMounted,
14 AlreadyMounted,
16 PayloadTooLarge,
18 BufferTooSmall,
20 CorruptEntry,
22 NoSpace,
24 InvalidConfig,
26 TooManySectors,
28 SizeMismatch {
30 expected: usize,
32 actual: usize,
34 },
35 SchemaMismatch {
41 expected: u8,
43 found: u8,
45 },
46 InvalidData,
48 InvalidRun,
56 Overflow,
58 NotAttached,
60 AlreadyAttached,
62 VolumeLost,
69 ForeignVolume {
74 expected: u32,
76 found: u32,
78 },
79 UnsupportedVersion {
81 found: u8,
83 },
84 GeometryMismatch,
89 CorruptCursor,
91 ReadsUnavailable,
95 RegionOverlap {
102 db_base: u32,
104 db_end: u32,
106 },
107}
108
109impl fmt::Display for Error {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 match self {
112 Error::Flash(kind) => write!(f, "flash error: {:?}", kind),
113 Error::NotMounted => write!(f, "database not mounted"),
114 Error::AlreadyMounted => write!(f, "database already mounted"),
115 Error::PayloadTooLarge => write!(f, "payload too large"),
116 Error::BufferTooSmall => write!(f, "buffer too small"),
117 Error::CorruptEntry => write!(f, "corrupt entry"),
118 Error::NoSpace => write!(f, "no space available"),
119 Error::InvalidConfig => write!(f, "invalid configuration"),
120 Error::TooManySectors => write!(f, "too many sectors for MAX_SECTORS"),
121 Error::SizeMismatch { expected, actual } => {
122 write!(
123 f,
124 "size mismatch: expected {} bytes, got {}",
125 expected, actual
126 )
127 }
128 Error::SchemaMismatch { expected, found } => {
129 write!(
130 f,
131 "schema mismatch: expected version {}, found {}",
132 expected, found
133 )
134 }
135 Error::InvalidData => {
136 write!(f, "invalid data: failed to interpret bytes as target type")
137 }
138 Error::InvalidRun => write!(f, "run cannot be encoded as a single entry"),
139 Error::Overflow => write!(f, "arithmetic overflow in offset calculation"),
140 Error::NotAttached => write!(f, "no volume attached"),
141 Error::AlreadyAttached => write!(f, "a volume is already attached"),
142 Error::VolumeLost => write!(f, "volume lost mid-operation"),
143 Error::ForeignVolume { expected, found } => write!(
144 f,
145 "foreign volume: expected log id {}, found {}",
146 expected, found
147 ),
148 Error::UnsupportedVersion { found } => {
149 write!(f, "unsupported sector format version {}", found)
150 }
151 Error::GeometryMismatch => write!(f, "incompatible flash geometry for replication"),
152 Error::CorruptCursor => write!(f, "corrupt replication cursor"),
153 Error::ReadsUnavailable => {
154 write!(
155 f,
156 "database mounted without a full index; reads unavailable"
157 )
158 }
159 Error::RegionOverlap { db_base, db_end } => write!(
160 f,
161 "region overlaps the database's own sectors [{}, {})",
162 db_base, db_end
163 ),
164 }
165 }
166}
167
168impl core::error::Error for Error {}
169
170impl<E: embedded_storage::nor_flash::NorFlashError> From<E> for Error {
171 fn from(e: E) -> Self {
172 Error::Flash(e.kind())
173 }
174}
175
176#[cfg(feature = "defmt")]
177impl defmt::Format for Error {
178 fn format(&self, f: defmt::Formatter) {
179 match self {
180 Error::Flash(kind) => defmt::write!(f, "flash error: {:?}", defmt::Debug2Format(kind)),
181 Error::NotMounted => defmt::write!(f, "database not mounted"),
182 Error::AlreadyMounted => defmt::write!(f, "database already mounted"),
183 Error::PayloadTooLarge => defmt::write!(f, "payload too large"),
184 Error::BufferTooSmall => defmt::write!(f, "buffer too small"),
185 Error::CorruptEntry => defmt::write!(f, "corrupt entry"),
186 Error::NoSpace => defmt::write!(f, "no space available"),
187 Error::InvalidConfig => defmt::write!(f, "invalid configuration"),
188 Error::TooManySectors => defmt::write!(f, "too many sectors for MAX_SECTORS"),
189 Error::SizeMismatch { expected, actual } => {
190 defmt::write!(
191 f,
192 "size mismatch: expected {} bytes, got {}",
193 expected,
194 actual
195 )
196 }
197 Error::SchemaMismatch { expected, found } => {
198 defmt::write!(
199 f,
200 "schema mismatch: expected version {}, found {}",
201 expected,
202 found
203 )
204 }
205 Error::InvalidData => {
206 defmt::write!(f, "invalid data: failed to interpret bytes as target type")
207 }
208 Error::InvalidRun => defmt::write!(f, "run cannot be encoded as a single entry"),
209 Error::Overflow => defmt::write!(f, "arithmetic overflow in offset calculation"),
210 Error::NotAttached => defmt::write!(f, "no volume attached"),
211 Error::AlreadyAttached => defmt::write!(f, "a volume is already attached"),
212 Error::VolumeLost => defmt::write!(f, "volume lost mid-operation"),
213 Error::ForeignVolume { expected, found } => defmt::write!(
214 f,
215 "foreign volume: expected log id {}, found {}",
216 expected,
217 found
218 ),
219 Error::UnsupportedVersion { found } => {
220 defmt::write!(f, "unsupported sector format version {}", found)
221 }
222 Error::GeometryMismatch => {
223 defmt::write!(f, "incompatible flash geometry for replication")
224 }
225 Error::CorruptCursor => defmt::write!(f, "corrupt replication cursor"),
226 Error::ReadsUnavailable => {
227 defmt::write!(
228 f,
229 "database mounted without a full index; reads unavailable"
230 )
231 }
232 Error::RegionOverlap { db_base, db_end } => defmt::write!(
233 f,
234 "region overlaps the database's own sectors [{}, {})",
235 db_base,
236 db_end
237 ),
238 }
239 }
240}