1
1
//! Library and application errors
2
2
3
- use std:: {
4
- fmt:: { Display , Formatter } ,
5
- io,
6
- } ;
3
+ #[ cfg( feature = "serialport" ) ]
4
+ use std:: fmt:: { Display , Formatter } ;
7
5
8
6
use miette:: Diagnostic ;
9
- use slip_codec :: SlipError ;
10
- use strum:: { FromRepr , VariantNames } ;
7
+ use std :: io ;
8
+ use strum:: VariantNames ;
11
9
use thiserror:: Error ;
12
10
13
11
#[ cfg( feature = "cli" ) ]
14
12
use crate :: cli:: monitor:: parser:: esp_defmt:: DefmtError ;
13
+ #[ cfg( feature = "serialport" ) ]
14
+ use crate :: command:: CommandType ;
15
15
use crate :: {
16
- command:: CommandType ,
17
16
flasher:: { FlashFrequency , FlashSize } ,
18
17
targets:: Chip ,
19
18
} ;
19
+ #[ cfg( feature = "serialport" ) ]
20
+ use slip_codec:: SlipError ;
20
21
21
22
/// All possible errors returned by espflash
22
23
#[ derive( Debug , Diagnostic , Error ) ]
@@ -106,7 +107,7 @@ pub enum Error {
106
107
107
108
#[ cfg( not( feature = "serialport" ) ) ]
108
109
#[ error( transparent) ]
109
- IoError ( #[ from] std :: io:: Error ) ,
110
+ IoError ( #[ from] io:: Error ) ,
110
111
111
112
#[ error( "Specified partition table path is not a .bin or .csv file" ) ]
112
113
#[ diagnostic( code( espflash:: invalid_partition_table_path) ) ]
@@ -183,10 +184,11 @@ pub enum Error {
183
184
InvalidElf ( #[ from] ElfError ) ,
184
185
185
186
#[ error( "The bootloader returned an error" ) ]
187
+ #[ cfg( feature = "serialport" ) ]
186
188
#[ diagnostic( transparent) ]
187
189
RomError ( #[ from] RomError ) ,
188
190
189
- #[ cfg( feature = "serialport " ) ]
191
+ #[ cfg( feature = "cli " ) ]
190
192
#[ error( transparent) ]
191
193
#[ diagnostic( transparent) ]
192
194
Defmt ( #[ from] DefmtError ) ,
@@ -204,7 +206,7 @@ pub enum Error {
204
206
InternalError ,
205
207
206
208
#[ error( "Failed to open file: {0}" ) ]
207
- FileOpenError ( String , #[ source] std :: io:: Error ) ,
209
+ FileOpenError ( String , #[ source] io:: Error ) ,
208
210
209
211
#[ error( "Failed to parse partition table" ) ]
210
212
Partition ( #[ from] esp_idf_part:: Error ) ,
@@ -278,6 +280,7 @@ pub enum ConnectionError {
278
280
#[ diagnostic( code( espflash:: read_missmatch) ) ]
279
281
ReadMissmatch ( u32 , u32 ) ,
280
282
283
+ #[ cfg( feature = "serialport" ) ]
281
284
#[ error( "Timeout while running {0}command" ) ]
282
285
#[ diagnostic( code( espflash:: timeout) ) ]
283
286
Timeout ( TimedOutCommand ) ,
@@ -327,10 +330,12 @@ impl From<SlipError> for ConnectionError {
327
330
328
331
/// An executed command which has timed out
329
332
#[ derive( Clone , Debug , Default ) ]
333
+ #[ cfg( feature = "serialport" ) ]
330
334
pub struct TimedOutCommand {
331
335
command : Option < CommandType > ,
332
336
}
333
337
338
+ #[ cfg( feature = "serialport" ) ]
334
339
impl Display for TimedOutCommand {
335
340
fn fmt ( & self , f : & mut Formatter ) -> std:: fmt:: Result {
336
341
match & self . command {
@@ -340,16 +345,18 @@ impl Display for TimedOutCommand {
340
345
}
341
346
}
342
347
348
+ #[ cfg( feature = "serialport" ) ]
343
349
impl From < CommandType > for TimedOutCommand {
344
350
fn from ( ct : CommandType ) -> Self {
345
351
TimedOutCommand { command : Some ( ct) }
346
352
}
347
353
}
348
354
349
355
/// Errors originating from a device's ROM functionality
350
- #[ derive( Clone , Copy , Debug , Default , Diagnostic , Error , FromRepr ) ]
356
+ #[ derive( Clone , Copy , Debug , Default , Diagnostic , Error , strum :: FromRepr ) ]
351
357
#[ non_exhaustive]
352
358
#[ repr( u8 ) ]
359
+ #[ cfg( feature = "serialport" ) ]
353
360
pub enum RomErrorKind {
354
361
#[ error( "Invalid message received" ) ]
355
362
#[ diagnostic( code( espflash:: rom:: invalid_message) ) ]
@@ -425,6 +432,7 @@ pub enum RomErrorKind {
425
432
Other = 0xff ,
426
433
}
427
434
435
+ #[ cfg( feature = "serialport" ) ]
428
436
impl From < u8 > for RomErrorKind {
429
437
fn from ( raw : u8 ) -> Self {
430
438
Self :: from_repr ( raw) . unwrap_or_default ( )
@@ -434,13 +442,15 @@ impl From<u8> for RomErrorKind {
434
442
/// An error originating from a device's ROM functionality
435
443
#[ derive( Clone , Copy , Debug , Diagnostic , Error ) ]
436
444
#[ error( "Error while running {command} command" ) ]
445
+ #[ cfg( feature = "serialport" ) ]
437
446
#[ non_exhaustive]
438
447
pub struct RomError {
439
448
command : CommandType ,
440
449
#[ source]
441
450
kind : RomErrorKind ,
442
451
}
443
452
453
+ #[ cfg( feature = "serialport" ) ]
444
454
impl RomError {
445
455
pub fn new ( command : CommandType , kind : RomErrorKind ) -> RomError {
446
456
RomError { command, kind }
@@ -482,13 +492,15 @@ impl From<&'static str> for ElfError {
482
492
}
483
493
}
484
494
495
+ #[ cfg( feature = "serialport" ) ]
485
496
pub ( crate ) trait ResultExt {
486
497
/// Mark an error as having occurred during the flashing stage
487
498
fn flashing ( self ) -> Self ;
488
499
/// Mark the command from which this error originates
489
500
fn for_command ( self , command : CommandType ) -> Self ;
490
501
}
491
502
503
+ #[ cfg( feature = "serialport" ) ]
492
504
impl < T > ResultExt for Result < T , Error > {
493
505
fn flashing ( self ) -> Self {
494
506
match self {
@@ -516,7 +528,7 @@ fn from_error_kind<E>(kind: io::ErrorKind, err: E) -> ConnectionError
516
528
where
517
529
E : Into < serialport:: Error > ,
518
530
{
519
- use std :: io:: ErrorKind ;
531
+ use io:: ErrorKind ;
520
532
521
533
match kind {
522
534
ErrorKind :: TimedOut => ConnectionError :: Timeout ( TimedOutCommand :: default ( ) ) ,
0 commit comments