Drop use of Option for Partition fields

This simplifies the code quite a bit and the Option<T> logic is only
used for string parsing.  s.is_empty() is just fine in this case IMO.
This commit is contained in:
2Shirt 2024-11-10 23:10:26 -08:00
parent 1e223aa56a
commit dd03962c84
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
4 changed files with 56 additions and 74 deletions

View file

@ -385,29 +385,31 @@ impl App {
if let Some(disk_index) = self.disk_index_dest { if let Some(disk_index) = self.disk_index_dest {
if let Some(disk) = disk_list.get(disk_index) { if let Some(disk) = disk_list.get(disk_index) {
let table_type = self.table_type.clone().unwrap(); let table_type = self.table_type.clone().unwrap();
let letter_boot = if let Some(s) = disk let letter_boot: String;
.parts let letter_os: String;
.get(self.part_index_boot.unwrap()) if let Some(part) =
.clone() disk.parts.get(self.part_index_boot.unwrap())
.unwrap()
.letter
.clone()
{ {
s letter_boot = if part.letter.is_empty() {
String::from("??")
} else {
part.letter.clone()
};
letter_os = if part.letter.is_empty() {
String::from("??")
} else {
part.letter.clone()
};
} else { } else {
String::from("??") self.action_tx.send(Action::DisplayPopup(
}; popup::Type::Error,
let letter_os = if let Some(s) = disk String::from(
.parts "Failed to get drive letters for destination",
.get(self.part_index_os.unwrap()) ),
.unwrap() ))?;
.letter self.action_tx.send(Action::SetMode(Mode::Done))?;
.clone() return Ok(());
{ }
s
} else {
String::from("??")
};
// Create boot files // Create boot files
self.action_tx.send(Action::Command( self.action_tx.send(Action::Command(

View file

@ -44,9 +44,9 @@ pub struct Disk {
pub struct Partition { pub struct Partition {
pub id: usize, pub id: usize,
pub is_selected: bool, pub is_selected: bool,
pub fs_type: Option<String>, pub fs_type: String,
pub label: Option<String>, pub label: String,
pub letter: Option<String>, pub letter: String,
pub part_type: String, pub part_type: String,
pub size: u64, // In bytes pub size: u64, // In bytes
} }
@ -102,10 +102,10 @@ impl fmt::Display for Disk {
impl fmt::Display for Partition { impl fmt::Display for Partition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s: String; let mut s: String;
let fs = if let Some(fs_type) = &self.fs_type { let fs = if self.fs_type.is_empty() {
format!("({fs_type})")
} else {
String::from("(?)") String::from("(?)")
} else {
format!("({})", self.fs_type)
}; };
if cfg!(windows) { if cfg!(windows) {
s = format!( s = format!(
@ -122,8 +122,8 @@ impl fmt::Display for Partition {
fs fs
); );
} }
if let Some(l) = &self.label { if !self.label.is_empty() {
s = format!("{s} \"{l}\""); s = format!("{s} \"{}\"", &self.label);
} }
write!(f, "{s}") write!(f, "{s}")
} }
@ -162,33 +162,31 @@ pub fn get_fake_disks() -> Vec<Disk> {
part_type: PartitionTableType::Legacy, part_type: PartitionTableType::Legacy,
parts: vec![ parts: vec![
Partition { Partition {
fs_type: Some(String::from("NTFS")),
id: 1, id: 1,
label: Some(String::from("System Reserved")), fs_type: String::from("NTFS"),
label: String::from("System Reserved"),
part_type: String::from("7"), part_type: String::from("7"),
size: 104_857_600, size: 104_857_600,
..Default::default() ..Default::default()
}, },
Partition { Partition {
fs_type: None,
id: 2, id: 2,
label: None,
part_type: String::from("5"), part_type: String::from("5"),
size: 267_806_310_400, size: 267_806_310_400,
..Default::default() ..Default::default()
}, },
Partition { Partition {
fs_type: Some(String::from("NTFS")),
id: 5, id: 5,
label: Some(String::from("Win7")), fs_type: String::from("NTFS"),
label: String::from("Win7"),
part_type: String::from("7"), part_type: String::from("7"),
size: 267_701_452_800, size: 267_701_452_800,
..Default::default() ..Default::default()
}, },
Partition { Partition {
fs_type: Some(String::from("NTFS")),
id: 6, id: 6,
label: Some(String::from("Tools")), fs_type: String::from("NTFS"),
label: String::from("Tools"),
part_type: String::from("7"), part_type: String::from("7"),
size: 524_288_000, size: 524_288_000,
..Default::default() ..Default::default()
@ -204,9 +202,9 @@ pub fn get_fake_disks() -> Vec<Disk> {
model: "ADATA Garbage".to_string(), model: "ADATA Garbage".to_string(),
part_type: PartitionTableType::Legacy, part_type: PartitionTableType::Legacy,
parts: vec![Partition { parts: vec![Partition {
fs_type: Some(String::from("NTFS")),
id: 1, id: 1,
label: Some(String::from("Scratch")), fs_type: String::from("NTFS"),
label: String::from("Scratch"),
part_type: String::from("7"), part_type: String::from("7"),
size: 249_998_951_424, size: 249_998_951_424,
..Default::default() ..Default::default()
@ -222,25 +220,23 @@ pub fn get_fake_disks() -> Vec<Disk> {
part_type: PartitionTableType::Guid, part_type: PartitionTableType::Guid,
parts: vec![ parts: vec![
Partition { Partition {
fs_type: Some(String::from("FAT32")),
id: 1, id: 1,
label: Some(String::from("ESP")), fs_type: String::from("FAT32"),
label: String::from("ESP"),
part_type: String::from("EFI"), part_type: String::from("EFI"),
size: 272_629_760, size: 272_629_760,
..Default::default() ..Default::default()
}, },
Partition { Partition {
fs_type: None,
id: 2, id: 2,
label: None,
part_type: String::from("MSR"), part_type: String::from("MSR"),
size: 16_777_216, size: 16_777_216,
..Default::default() ..Default::default()
}, },
Partition { Partition {
fs_type: Some(String::from("NTFS")),
id: 4, id: 4,
label: Some(String::from("Win10")), fs_type: String::from("NTFS"),
label: String::from("Win10"),
part_type: String::from("MS Basic Data"), part_type: String::from("MS Basic Data"),
size: 824_340_119_552, size: 824_340_119_552,
..Default::default() ..Default::default()
@ -257,17 +253,15 @@ pub fn get_fake_disks() -> Vec<Disk> {
part_type: PartitionTableType::Guid, part_type: PartitionTableType::Guid,
parts: vec![ parts: vec![
Partition { Partition {
fs_type: Some(String::from("FAT32")),
id: 1, id: 1,
label: Some(String::from("EFI Boot")), fs_type: String::from("FAT32"),
label: String::from("EFI Boot"),
part_type: String::from("EFI"), part_type: String::from("EFI"),
size: 209_715_200, size: 209_715_200,
..Default::default() ..Default::default()
}, },
Partition { Partition {
fs_type: None,
id: 2, id: 2,
label: None,
part_type: String::from("{48465300-0000-11AA-AA11-00306543ECAC}"), part_type: String::from("{48465300-0000-11AA-AA11-00306543ECAC}"),
size: 171_586_879_488, size: 171_586_879_488,
..Default::default() ..Default::default()
@ -281,7 +275,6 @@ pub fn get_fake_disks() -> Vec<Disk> {
conn_type: "MISC".to_string(), conn_type: "MISC".to_string(),
id: 5, id: 5,
part_type: PartitionTableType::Legacy, part_type: PartitionTableType::Legacy,
parts: Vec::new(),
model: "Iomega".to_string(), model: "Iomega".to_string(),
serial: "000".to_string(), serial: "000".to_string(),
size: 14, size: 14,

View file

@ -315,21 +315,9 @@ pub fn parse_partition_details(parts: &mut Vec<Partition>, contents: &str) {
for (_, [_id, letter, label, fs_type]) in for (_, [_id, letter, label, fs_type]) in
RE_VOL.captures_iter(vol_line).map(|c| c.extract()) RE_VOL.captures_iter(vol_line).map(|c| c.extract())
{ {
part.label = if label.trim().is_empty() { part.label = String::from(label.trim());
None part.letter = String::from(letter.trim());
} else { part.fs_type = String::from(fs_type.trim());
Some(String::from(label.trim()))
};
part.letter = if letter.trim().is_empty() {
None
} else {
Some(String::from(letter.trim()))
};
part.fs_type = if fs_type.trim().is_empty() {
None
} else {
Some(String::from(fs_type.trim()))
};
} }
} }
} }

View file

@ -40,10 +40,9 @@ mod diskpart {
fn get_partition_details() { fn get_partition_details() {
// Left // Left
let partition_1 = system::disk::Partition { let partition_1 = system::disk::Partition {
fs_type: Some(String::from("FAT32")),
id: 1, id: 1,
label: Some(String::from("ESP")), fs_type: String::from("FAT32"),
letter: None, label: String::from("ESP"),
part_type: String::from("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"), part_type: String::from("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"),
size: 272629760, size: 272629760,
..Default::default() ..Default::default()
@ -55,10 +54,10 @@ mod diskpart {
..Default::default() ..Default::default()
}; };
let partition_4 = system::disk::Partition { let partition_4 = system::disk::Partition {
fs_type: Some(String::from("NTFS")),
id: 4, id: 4,
label: Some(String::from("Windows")), fs_type: String::from("NTFS"),
letter: Some(String::from("C")), label: String::from("Windows"),
letter: String::from("C"),
part_type: String::from("ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"), part_type: String::from("ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"),
size: 50465865728, size: 50465865728,
..Default::default() ..Default::default()
@ -103,9 +102,9 @@ mod diskpart {
disk.parts.push(system::disk::Partition::default()); disk.parts.push(system::disk::Partition::default());
let partition_1 = system::disk::Partition { let partition_1 = system::disk::Partition {
fs_type: Some(String::from("FAT32")), fs_type: String::from("FAT32"),
label: Some(String::from("ESP")), label: String::from("ESP"),
letter: Some(String::from("S")), letter: String::from("S"),
part_type: String::from("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"), part_type: String::from("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"),
..Default::default() ..Default::default()
}; };