Skip to content

Commit fcd113c

Browse files
committed
add error when 2 partitions have the same name or type
1 parent 70e2811 commit fcd113c

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

espflash/src/error.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ pub enum PartitionTableError {
273273
#[error(transparent)]
274274
#[diagnostic(transparent)]
275275
Overlapping(#[from] OverlappingPartitionsError),
276+
#[error(transparent)]
277+
#[diagnostic(transparent)]
278+
Duplicate(#[from] DuplicatePartitionsError),
276279
}
277280

278281
#[derive(Debug, Error, Diagnostic)]
@@ -338,7 +341,7 @@ pub struct OverlappingPartitionsError {
338341
source_code: String,
339342
#[label("This partition")]
340343
partition1_span: SourceSpan,
341-
#[label("Overlaps with this partition")]
344+
#[label("overlaps with this partition")]
342345
partition2_span: SourceSpan,
343346
}
344347

@@ -352,6 +355,35 @@ impl OverlappingPartitionsError {
352355
}
353356
}
354357

358+
#[derive(Debug, Error, Diagnostic)]
359+
#[error("Duplicate partitions")]
360+
#[diagnostic(code(espflash::partition_table::duplicate))]
361+
pub struct DuplicatePartitionsError {
362+
#[source_code]
363+
source_code: String,
364+
#[label("This partition")]
365+
partition1_span: SourceSpan,
366+
#[label("has the same {} as this partition", self.ty)]
367+
partition2_span: SourceSpan,
368+
ty: &'static str,
369+
}
370+
371+
impl DuplicatePartitionsError {
372+
pub fn new(
373+
source: &str,
374+
partition1_line: usize,
375+
partition2_line: usize,
376+
ty: &'static str,
377+
) -> Self {
378+
DuplicatePartitionsError {
379+
source_code: source.into(),
380+
partition1_span: line_to_span(&source, partition1_line),
381+
partition2_span: line_to_span(&source, partition2_line),
382+
ty,
383+
}
384+
}
385+
}
386+
355387
#[derive(Debug, Error)]
356388
#[error("{0}")]
357389
pub struct ElfError(&'static str);

espflash/src/partition_table.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use md5::{Context, Digest};
22
use regex::Regex;
33
use serde::{Deserialize, Deserializer};
44

5-
use crate::error::{CSVError, OverlappingPartitionsError, PartitionTableError};
5+
use crate::error::{
6+
CSVError, DuplicatePartitionsError, OverlappingPartitionsError, PartitionTableError,
7+
};
68
use std::cmp::{max, min};
79
use std::io::Write;
810

@@ -18,7 +20,7 @@ pub enum Type {
1820
Data = 0x01,
1921
}
2022

21-
#[derive(Copy, Clone, Debug, Deserialize)]
23+
#[derive(Copy, Clone, Debug, Deserialize, PartialEq)]
2224
#[repr(u8)]
2325
#[allow(dead_code)]
2426
#[serde(rename_all = "lowercase")]
@@ -59,7 +61,7 @@ pub enum AppType {
5961
Test = 0x20,
6062
}
6163

62-
#[derive(Copy, Clone, Debug, Deserialize)]
64+
#[derive(Copy, Clone, Debug, Deserialize, PartialEq)]
6365
#[repr(u8)]
6466
#[allow(dead_code)]
6567
#[serde(rename_all = "lowercase")]
@@ -76,7 +78,7 @@ pub enum DataType {
7678
Spiffs = 0x82,
7779
}
7880

79-
#[derive(Debug, Deserialize)]
81+
#[derive(Debug, Deserialize, PartialEq)]
8082
#[allow(dead_code)]
8183
#[serde(untagged)]
8284
pub enum SubType {
@@ -195,8 +197,24 @@ impl PartitionTable {
195197
for partition1 in &self.partitions {
196198
for partition2 in &self.partitions {
197199
if let (Some(line1), Some(line2)) = (&partition1.line, &partition2.line) {
198-
if line1 != line2 && partition1.overlaps(partition2) {
199-
return Err(OverlappingPartitionsError::new(source, *line1, *line2).into());
200+
if line1 != line2 {
201+
if partition1.overlaps(partition2) {
202+
return Err(
203+
OverlappingPartitionsError::new(source, *line1, *line2).into()
204+
);
205+
}
206+
if partition1.name == partition2.name {
207+
return Err(DuplicatePartitionsError::new(
208+
source, *line1, *line2, "name",
209+
)
210+
.into());
211+
}
212+
if partition1.sub_type == partition2.sub_type {
213+
return Err(DuplicatePartitionsError::new(
214+
source, *line1, *line2, "sub-type",
215+
)
216+
.into());
217+
}
200218
}
201219
}
202220
}

0 commit comments

Comments
 (0)