@@ -187,12 +187,10 @@ pub enum Error {
187
187
ParseChipRevError { chip_rev : String } ,
188
188
189
189
#[ error( "Error while connecting to device" ) ]
190
- #[ diagnostic( transparent) ]
191
- Connection ( #[ source] ConnectionError ) ,
190
+ Connection ( CoreError ) ,
192
191
193
192
#[ error( "Communication error while flashing device" ) ]
194
- #[ diagnostic( transparent) ]
195
- Flashing ( #[ source] ConnectionError ) ,
193
+ Flashing ( CoreError ) ,
196
194
197
195
#[ error( "Supplied ELF image is not valid" ) ]
198
196
#[ diagnostic(
@@ -203,20 +201,17 @@ pub enum Error {
203
201
204
202
#[ error( "Supplied ELF image contains an invalid application descriptor" ) ]
205
203
#[ diagnostic( code( espflash:: invalid_app_descriptor) ) ]
206
- InvalidAppDescriptor ( # [ from ] AppDescriptorError ) ,
204
+ InvalidAppDescriptor ( CoreError ) ,
207
205
208
206
#[ error( "The bootloader returned an error" ) ]
209
207
#[ cfg( feature = "serialport" ) ]
210
- #[ diagnostic( transparent) ]
211
- RomError ( #[ from] RomError ) ,
208
+ RomError ( CoreError ) ,
212
209
213
210
#[ error( "The selected partition does not exist in the partition table" ) ]
214
- #[ diagnostic( transparent) ]
215
- MissingPartition ( #[ from] MissingPartition ) ,
211
+ MissingPartition ( CoreError ) ,
216
212
217
213
#[ error( "The partition table is missing or invalid" ) ]
218
- #[ diagnostic( transparent) ]
219
- MissingPartitionTable ( #[ from] MissingPartitionTable ) ,
214
+ MissingPartitionTable ( CoreError ) ,
220
215
221
216
#[ cfg( feature = "cli" ) ]
222
217
#[ error( transparent) ]
@@ -271,6 +266,14 @@ pub enum Error {
271
266
AppDescriptorNotPresent ( String ) ,
272
267
}
273
268
269
+ #[ cfg( feature = "serialport" ) ]
270
+ impl From < SlipError > for Error {
271
+ fn from ( err : SlipError ) -> Self {
272
+ let conn_err: ConnectionError = err. into ( ) ; // uses first impl
273
+ Self :: Connection ( Box :: new ( conn_err) )
274
+ }
275
+ }
276
+
274
277
#[ cfg( feature = "serialport" ) ]
275
278
impl From < io:: Error > for Error {
276
279
fn from ( err : io:: Error ) -> Self {
@@ -286,13 +289,6 @@ impl From<serialport::Error> for Error {
286
289
}
287
290
}
288
291
289
- #[ cfg( feature = "serialport" ) ]
290
- impl From < SlipError > for Error {
291
- fn from ( err : SlipError ) -> Self {
292
- Self :: Connection ( err. into ( ) )
293
- }
294
- }
295
-
296
292
impl From < TryFromSliceError > for Error {
297
293
fn from ( err : TryFromSliceError ) -> Self {
298
294
Self :: TryFromSlice ( Box :: new ( err) )
@@ -305,6 +301,12 @@ impl From<object::Error> for Error {
305
301
}
306
302
}
307
303
304
+ impl From < AppDescriptorError > for Error {
305
+ fn from ( err : AppDescriptorError ) -> Self {
306
+ Self :: InvalidAppDescriptor ( Box :: new ( err) )
307
+ }
308
+ }
309
+
308
310
#[ cfg( feature = "cli" ) ]
309
311
impl From < dialoguer:: Error > for Error {
310
312
fn from ( err : dialoguer:: Error ) -> Self {
@@ -331,6 +333,7 @@ pub enum AppDescriptorError {
331
333
/// Connection-related errors.
332
334
#[ derive( Debug , Diagnostic , Error ) ]
333
335
#[ non_exhaustive]
336
+ #[ cfg( feature = "serialport" ) ]
334
337
pub enum ConnectionError {
335
338
#[ error( "Failed to connect to the device" ) ]
336
339
#[ diagnostic(
@@ -604,13 +607,21 @@ impl<T> ResultExt for Result<T, Error> {
604
607
}
605
608
606
609
fn for_command ( self , command : CommandType ) -> Self {
607
- match self {
608
- Err ( Error :: Connection ( ConnectionError :: Timeout ( _) ) ) => {
609
- Err ( Error :: Connection ( ConnectionError :: Timeout ( command. into ( ) ) ) )
610
- }
611
- Err ( Error :: Flashing ( ConnectionError :: Timeout ( _) ) ) => {
612
- Err ( Error :: Flashing ( ConnectionError :: Timeout ( command. into ( ) ) ) )
610
+ fn remap_if_timeout ( err : CoreError , command : CommandType ) -> CoreError {
611
+ match err. downcast :: < ConnectionError > ( ) {
612
+ Ok ( boxed_ce) => match * boxed_ce {
613
+ ConnectionError :: Timeout ( _) => {
614
+ Box :: new ( ConnectionError :: Timeout ( command. into ( ) ) )
615
+ }
616
+ other => Box :: new ( other) ,
617
+ } ,
618
+ Err ( orig) => orig,
613
619
}
620
+ }
621
+
622
+ match self {
623
+ Err ( Error :: Connection ( err) ) => Err ( Error :: Connection ( remap_if_timeout ( err, command) ) ) ,
624
+ Err ( Error :: Flashing ( err) ) => Err ( Error :: Flashing ( remap_if_timeout ( err, command) ) ) ,
614
625
res => res,
615
626
}
616
627
}
0 commit comments