From fdba07650b4b1cb6f971acb75f7d8644740c8ada Mon Sep 17 00:00:00 2001 From: "NATHAN.DANDRIMONT" Date: Thu, 20 Jun 2024 10:44:54 +0200 Subject: [PATCH 1/2] feat : add generics check for Machine --- src/architecture/generic.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/architecture/generic.rs b/src/architecture/generic.rs index 0f68247..76baa1a 100644 --- a/src/architecture/generic.rs +++ b/src/architecture/generic.rs @@ -158,8 +158,28 @@ impl Machine { dumpfile: PathBuf, outfolder: PathBuf, ) -> Self { - // TODO: Validate each field - + //Check if machine_type is valid + if !matches!(machine_type, MachineType::RiscV(_)) { + panic!("Machine type is not supported"); + } + // Check if the dump file exists + if !dumpfile.exists() { + panic!("Dump file does not exist"); + } + // Check if the output folder exists + if !outfolder.exists() { + panic!("Output folder does not exist"); + } + // Check if the output folder is empty + if outfolder.read_dir().unwrap().next().is_some() { + panic!("Output folder is not empty"); + } + // Check if the memory regions are valid + for region in &memory_regions.regions { + if region.start_address > region.end_address { + panic!("Memory region start address is greater than end address"); + } + } Self { machine_type, memory_regions, From 8b842086a176fbb985b4a46a38ccb5723bbbd11e Mon Sep 17 00:00:00 2001 From: "NATHAN.DANDRIMONT" Date: Thu, 20 Jun 2024 13:52:09 +0200 Subject: [PATCH 2/2] feat: change panic to Result --- src/architecture/generic.rs | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/architecture/generic.rs b/src/architecture/generic.rs index 76baa1a..235339b 100644 --- a/src/architecture/generic.rs +++ b/src/architecture/generic.rs @@ -32,7 +32,6 @@ impl MemoryRegion { "Invalid memory region, start address is greater than or equal to end address" )); } - Ok(Self { region_type, start_address, @@ -157,34 +156,24 @@ impl Machine { memory_regions: MemorySpace, dumpfile: PathBuf, outfolder: PathBuf, - ) -> Self { - //Check if machine_type is valid - if !matches!(machine_type, MachineType::RiscV(_)) { - panic!("Machine type is not supported"); - } + ) -> Result { // Check if the dump file exists if !dumpfile.exists() { - panic!("Dump file does not exist"); + return Err(anyhow::anyhow!("Dump file does not exist")); } // Check if the output folder exists if !outfolder.exists() { - panic!("Output folder does not exist"); + return Err(anyhow::anyhow!("Output folder does not exist")); } // Check if the output folder is empty - if outfolder.read_dir().unwrap().next().is_some() { - panic!("Output folder is not empty"); - } - // Check if the memory regions are valid - for region in &memory_regions.regions { - if region.start_address > region.end_address { - panic!("Memory region start address is greater than end address"); - } + if outfolder.read_dir()?.next().is_some() { + return Err(anyhow::anyhow!("Output folder is not empty")); } - Self { + Ok(Self { machine_type, memory_regions, dumpfile, outfolder, - } + }) } }