1
1
use crate :: flasher:: Command ;
2
2
use crate :: image_format:: ImageFormatId ;
3
3
use crate :: Chip ;
4
- use csv:: Position ;
5
4
use miette:: { Diagnostic , SourceOffset , SourceSpan } ;
6
5
use slip_codec:: Error as SlipError ;
7
6
use std:: fmt:: { Display , Formatter } ;
@@ -266,15 +265,25 @@ impl<T> ResultExt for Result<T, Error> {
266
265
}
267
266
}
268
267
268
+ #[ derive( Debug , Error , Diagnostic ) ]
269
+ pub enum PartitionTableError {
270
+ #[ error( transparent) ]
271
+ #[ diagnostic( transparent) ]
272
+ CSV ( #[ from] CSVError ) ,
273
+ #[ error( transparent) ]
274
+ #[ diagnostic( transparent) ]
275
+ Overlapping ( #[ from] OverlappingPartitionsError ) ,
276
+ }
277
+
269
278
#[ derive( Debug , Error , Diagnostic ) ]
270
279
#[ error( "Malformed partition table" ) ]
271
280
#[ diagnostic(
272
- code( espflash:: mallformed_partition_table ) ,
281
+ code( espflash:: partition_table :: mallformed ) ,
273
282
help( "See the espressif documentation for information on the partition table format:
274
283
275
284
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html#creating-custom-tables" )
276
285
) ]
277
- pub struct PartitionTableError {
286
+ pub struct CSVError {
278
287
#[ source_code]
279
288
source : String ,
280
289
#[ label( "{}" , self . hint) ]
@@ -284,12 +293,12 @@ pub struct PartitionTableError {
284
293
error : csv:: Error ,
285
294
}
286
295
287
- impl PartitionTableError {
296
+ impl CSVError {
288
297
pub fn new ( error : csv:: Error , source : String ) -> Self {
289
- let err_pos = match error. kind ( ) {
290
- csv:: ErrorKind :: Deserialize { pos : Some ( pos) , .. } => pos. clone ( ) ,
291
- csv:: ErrorKind :: UnequalLengths { pos : Some ( pos) , .. } => pos. clone ( ) ,
292
- _ => Position :: new ( ) ,
298
+ let err_line = match error. kind ( ) {
299
+ csv:: ErrorKind :: Deserialize { pos : Some ( pos) , .. } => pos. line ( ) ,
300
+ csv:: ErrorKind :: UnequalLengths { pos : Some ( pos) , .. } => pos. line ( ) ,
301
+ _ => 0 ,
293
302
} ;
294
303
let hint = match error. kind ( ) {
295
304
csv:: ErrorKind :: Deserialize { err, .. } => err. to_string ( ) ,
@@ -302,16 +311,9 @@ impl PartitionTableError {
302
311
_ => String :: new ( ) ,
303
312
} ;
304
313
305
- // since csv doesn't give us the position in the line the error occurs, we highlight the entire line
306
- let line_length = source
307
- . lines ( )
308
- . nth ( err_pos. line ( ) as usize - 1 )
309
- . unwrap ( )
310
- . len ( )
311
- . into ( ) ;
312
- let err_span = SourceSpan :: new ( pos_to_offset ( err_pos) , line_length) ;
314
+ let err_span = line_to_span ( & source, err_line as usize ) ;
313
315
314
- PartitionTableError {
316
+ CSVError {
315
317
source,
316
318
err_span,
317
319
hint,
@@ -320,8 +322,34 @@ impl PartitionTableError {
320
322
}
321
323
}
322
324
323
- fn pos_to_offset ( pos : Position ) -> SourceOffset {
324
- ( pos. byte ( ) as usize ) . into ( )
325
+ /// since csv doesn't give us the position in the line the error occurs, we highlight the entire line
326
+ ///
327
+ /// line starts at 1
328
+ fn line_to_span ( source : & str , line : usize ) -> SourceSpan {
329
+ let line_length = source. lines ( ) . nth ( line - 1 ) . unwrap ( ) . len ( ) . into ( ) ;
330
+ SourceSpan :: new ( SourceOffset :: from_location ( source, line, 2 ) , line_length)
331
+ }
332
+
333
+ #[ derive( Debug , Error , Diagnostic ) ]
334
+ #[ error( "Overlapping partitions" ) ]
335
+ #[ diagnostic( code( espflash:: partition_table:: overlapping) ) ]
336
+ pub struct OverlappingPartitionsError {
337
+ #[ source_code]
338
+ source_code : String ,
339
+ #[ label( "This partition" ) ]
340
+ partition1_span : SourceSpan ,
341
+ #[ label( "Overlaps with this partition" ) ]
342
+ partition2_span : SourceSpan ,
343
+ }
344
+
345
+ impl OverlappingPartitionsError {
346
+ pub fn new ( source : & str , partition1_line : usize , partition2_line : usize ) -> Self {
347
+ OverlappingPartitionsError {
348
+ source_code : source. into ( ) ,
349
+ partition1_span : line_to_span ( & source, partition1_line) ,
350
+ partition2_span : line_to_span ( & source, partition2_line) ,
351
+ }
352
+ }
325
353
}
326
354
327
355
#[ derive( Debug , Error ) ]
0 commit comments