Skip to content

Commit

Permalink
feat: change panic to Result
Browse files Browse the repository at this point in the history
  • Loading branch information
Esgr0bar committed Jun 20, 2024
1 parent fdba076 commit 8b84208
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions src/architecture/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Self> {
// 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,
}
})
}
}

0 comments on commit 8b84208

Please sign in to comment.