@@ -19,6 +19,10 @@ use crate::{
19
19
targets:: Chip ,
20
20
} ;
21
21
22
+ // A type alias for a dynamic error that can be used in the library.
23
+ // https://d34dl0ck.me/rust-bites-designing-error-types-in-rust-libraries/index.html
24
+ type CoreError = Box < dyn core:: error:: Error + Send + Sync > ;
25
+
22
26
/// All possible errors returned by espflash.
23
27
#[ derive( Debug , Diagnostic , Error ) ]
24
28
#[ non_exhaustive]
@@ -195,7 +199,7 @@ pub enum Error {
195
199
code( espflash:: invalid_elf) ,
196
200
help( "Try running `cargo clean` and rebuilding the image" )
197
201
) ]
198
- InvalidElf ( # [ from ] object :: Error ) ,
202
+ InvalidElf ( CoreError ) ,
199
203
200
204
#[ error( "Supplied ELF image contains an invalid application descriptor" ) ]
201
205
#[ diagnostic( code( espflash:: invalid_app_descriptor) ) ]
@@ -218,10 +222,10 @@ pub enum Error {
218
222
#[ cfg( feature = "cli" ) ]
219
223
#[ error( transparent) ]
220
224
#[ diagnostic( code( espflash:: dialoguer_error) ) ]
221
- DialoguerError ( # [ from ] dialoguer :: Error ) ,
225
+ DialoguerError ( CoreError ) ,
222
226
223
227
#[ error( transparent) ]
224
- TryFromSlice ( # [ from ] TryFromSliceError ) ,
228
+ TryFromSlice ( CoreError ) ,
225
229
226
230
#[ error( "Failed to open file: {0}" ) ]
227
231
FileOpenError ( String , #[ source] io:: Error ) ,
@@ -283,6 +287,25 @@ impl From<SlipError> for Error {
283
287
}
284
288
}
285
289
290
+ impl From < TryFromSliceError > for Error {
291
+ fn from ( err : TryFromSliceError ) -> Self {
292
+ Self :: TryFromSlice ( Box :: new ( err) )
293
+ }
294
+ }
295
+
296
+ impl From < object:: Error > for Error {
297
+ fn from ( err : object:: Error ) -> Self {
298
+ Self :: InvalidElf ( err. into ( ) )
299
+ }
300
+ }
301
+
302
+ #[ cfg( feature = "cli" ) ]
303
+ impl From < dialoguer:: Error > for Error {
304
+ fn from ( err : dialoguer:: Error ) -> Self {
305
+ Self :: DialoguerError ( err. into ( ) )
306
+ }
307
+ }
308
+
286
309
/// App descriptor errors.
287
310
#[ derive( Debug , Diagnostic , Error ) ]
288
311
#[ non_exhaustive]
0 commit comments