From dd03962c8414262db3968782fdcfff9f4e62c95d Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Sun, 10 Nov 2024 23:10:26 -0800 Subject: [PATCH] Drop use of Option for Partition fields This simplifies the code quite a bit and the Option logic is only used for string parsing. s.is_empty() is just fine in this case IMO. --- src/app.rs | 44 +++++++++++++++++++----------------- src/system/disk.rs | 51 ++++++++++++++++++------------------------ src/system/diskpart.rs | 18 +++------------ src/tests/mod.rs | 17 +++++++------- 4 files changed, 56 insertions(+), 74 deletions(-) diff --git a/src/app.rs b/src/app.rs index d90adbf..d888099 100644 --- a/src/app.rs +++ b/src/app.rs @@ -385,29 +385,31 @@ impl App { if let Some(disk_index) = self.disk_index_dest { if let Some(disk) = disk_list.get(disk_index) { let table_type = self.table_type.clone().unwrap(); - let letter_boot = if let Some(s) = disk - .parts - .get(self.part_index_boot.unwrap()) - .clone() - .unwrap() - .letter - .clone() + let letter_boot: String; + let letter_os: String; + if let Some(part) = + disk.parts.get(self.part_index_boot.unwrap()) { - 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 { - String::from("??") - }; - let letter_os = if let Some(s) = disk - .parts - .get(self.part_index_os.unwrap()) - .unwrap() - .letter - .clone() - { - s - } else { - String::from("??") - }; + self.action_tx.send(Action::DisplayPopup( + popup::Type::Error, + String::from( + "Failed to get drive letters for destination", + ), + ))?; + self.action_tx.send(Action::SetMode(Mode::Done))?; + return Ok(()); + } // Create boot files self.action_tx.send(Action::Command( diff --git a/src/system/disk.rs b/src/system/disk.rs index d65c02f..be6c3c3 100644 --- a/src/system/disk.rs +++ b/src/system/disk.rs @@ -44,9 +44,9 @@ pub struct Disk { pub struct Partition { pub id: usize, pub is_selected: bool, - pub fs_type: Option, - pub label: Option, - pub letter: Option, + pub fs_type: String, + pub label: String, + pub letter: String, pub part_type: String, pub size: u64, // In bytes } @@ -102,10 +102,10 @@ impl fmt::Display for Disk { impl fmt::Display for Partition { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut s: String; - let fs = if let Some(fs_type) = &self.fs_type { - format!("({fs_type})") - } else { + let fs = if self.fs_type.is_empty() { String::from("(?)") + } else { + format!("({})", self.fs_type) }; if cfg!(windows) { s = format!( @@ -122,8 +122,8 @@ impl fmt::Display for Partition { fs ); } - if let Some(l) = &self.label { - s = format!("{s} \"{l}\""); + if !self.label.is_empty() { + s = format!("{s} \"{}\"", &self.label); } write!(f, "{s}") } @@ -162,33 +162,31 @@ pub fn get_fake_disks() -> Vec { part_type: PartitionTableType::Legacy, parts: vec![ Partition { - fs_type: Some(String::from("NTFS")), id: 1, - label: Some(String::from("System Reserved")), + fs_type: String::from("NTFS"), + label: String::from("System Reserved"), part_type: String::from("7"), size: 104_857_600, ..Default::default() }, Partition { - fs_type: None, id: 2, - label: None, part_type: String::from("5"), size: 267_806_310_400, ..Default::default() }, Partition { - fs_type: Some(String::from("NTFS")), id: 5, - label: Some(String::from("Win7")), + fs_type: String::from("NTFS"), + label: String::from("Win7"), part_type: String::from("7"), size: 267_701_452_800, ..Default::default() }, Partition { - fs_type: Some(String::from("NTFS")), id: 6, - label: Some(String::from("Tools")), + fs_type: String::from("NTFS"), + label: String::from("Tools"), part_type: String::from("7"), size: 524_288_000, ..Default::default() @@ -204,9 +202,9 @@ pub fn get_fake_disks() -> Vec { model: "ADATA Garbage".to_string(), part_type: PartitionTableType::Legacy, parts: vec![Partition { - fs_type: Some(String::from("NTFS")), id: 1, - label: Some(String::from("Scratch")), + fs_type: String::from("NTFS"), + label: String::from("Scratch"), part_type: String::from("7"), size: 249_998_951_424, ..Default::default() @@ -222,25 +220,23 @@ pub fn get_fake_disks() -> Vec { part_type: PartitionTableType::Guid, parts: vec![ Partition { - fs_type: Some(String::from("FAT32")), id: 1, - label: Some(String::from("ESP")), + fs_type: String::from("FAT32"), + label: String::from("ESP"), part_type: String::from("EFI"), size: 272_629_760, ..Default::default() }, Partition { - fs_type: None, id: 2, - label: None, part_type: String::from("MSR"), size: 16_777_216, ..Default::default() }, Partition { - fs_type: Some(String::from("NTFS")), id: 4, - label: Some(String::from("Win10")), + fs_type: String::from("NTFS"), + label: String::from("Win10"), part_type: String::from("MS Basic Data"), size: 824_340_119_552, ..Default::default() @@ -257,17 +253,15 @@ pub fn get_fake_disks() -> Vec { part_type: PartitionTableType::Guid, parts: vec![ Partition { - fs_type: Some(String::from("FAT32")), id: 1, - label: Some(String::from("EFI Boot")), + fs_type: String::from("FAT32"), + label: String::from("EFI Boot"), part_type: String::from("EFI"), size: 209_715_200, ..Default::default() }, Partition { - fs_type: None, id: 2, - label: None, part_type: String::from("{48465300-0000-11AA-AA11-00306543ECAC}"), size: 171_586_879_488, ..Default::default() @@ -281,7 +275,6 @@ pub fn get_fake_disks() -> Vec { conn_type: "MISC".to_string(), id: 5, part_type: PartitionTableType::Legacy, - parts: Vec::new(), model: "Iomega".to_string(), serial: "000".to_string(), size: 14, diff --git a/src/system/diskpart.rs b/src/system/diskpart.rs index 714b813..8e5bade 100644 --- a/src/system/diskpart.rs +++ b/src/system/diskpart.rs @@ -315,21 +315,9 @@ pub fn parse_partition_details(parts: &mut Vec, contents: &str) { for (_, [_id, letter, label, fs_type]) in RE_VOL.captures_iter(vol_line).map(|c| c.extract()) { - part.label = if label.trim().is_empty() { - None - } else { - 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())) - }; + part.label = String::from(label.trim()); + part.letter = String::from(letter.trim()); + part.fs_type = String::from(fs_type.trim()); } } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 259ecbf..cf96690 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -40,10 +40,9 @@ mod diskpart { fn get_partition_details() { // Left let partition_1 = system::disk::Partition { - fs_type: Some(String::from("FAT32")), id: 1, - label: Some(String::from("ESP")), - letter: None, + fs_type: String::from("FAT32"), + label: String::from("ESP"), part_type: String::from("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"), size: 272629760, ..Default::default() @@ -55,10 +54,10 @@ mod diskpart { ..Default::default() }; let partition_4 = system::disk::Partition { - fs_type: Some(String::from("NTFS")), id: 4, - label: Some(String::from("Windows")), - letter: Some(String::from("C")), + fs_type: String::from("NTFS"), + label: String::from("Windows"), + letter: String::from("C"), part_type: String::from("ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"), size: 50465865728, ..Default::default() @@ -103,9 +102,9 @@ mod diskpart { disk.parts.push(system::disk::Partition::default()); let partition_1 = system::disk::Partition { - fs_type: Some(String::from("FAT32")), - label: Some(String::from("ESP")), - letter: Some(String::from("S")), + fs_type: String::from("FAT32"), + label: String::from("ESP"), + letter: String::from("S"), part_type: String::from("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"), ..Default::default() };