chore: replace all panic's with proper error propagation

This commit is contained in:
2025-05-25 15:45:09 +09:00
parent 73b1a99cbd
commit 74e936ab76
4 changed files with 278 additions and 243 deletions

View File

@@ -1,6 +1,6 @@
//! Internal abstraction of all 8086 registers for disassembly.
use crate::operands::Operand;
use crate::{disasm::DisasmError, operands::Operand};
use core::fmt;
#[derive(Debug, Clone)]
@@ -36,29 +36,29 @@ pub type RegisterId = u8;
#[allow(dead_code)]
impl Register {
/// Find the register corresponding to the 8086 bytecode ID
pub fn by_id(id: Operand) -> Self {
pub fn by_id(id: Operand) -> Result<Self, DisasmError> {
match id {
Operand::Byte(b) => match b {
0b000 => Self::AL,
0b001 => Self::CL,
0b010 => Self::DL,
0b011 => Self::BL,
0b100 => Self::AH,
0b101 => Self::CH,
0b110 => Self::DH,
0b111 => Self::BH,
_ => panic!("Invalid 8bit register ID encountered"),
0b000 => Ok(Self::AL),
0b001 => Ok(Self::CL),
0b010 => Ok(Self::DL),
0b011 => Ok(Self::BL),
0b100 => Ok(Self::AH),
0b101 => Ok(Self::CH),
0b110 => Ok(Self::DH),
0b111 => Ok(Self::BH),
_ => Err(DisasmError::UnknownRegister(b as usize)),
},
Operand::Word(w) => match w {
0b000 => Self::AX,
0b001 => Self::CX,
0b010 => Self::DX,
0b011 => Self::BX,
0b100 => Self::SP,
0b101 => Self::BP,
0b110 => Self::SI,
0b111 => Self::DI,
_ => panic!("Invalid 16bit register ID encountered"),
0b000 => Ok(Self::AX),
0b001 => Ok(Self::CX),
0b010 => Ok(Self::DX),
0b011 => Ok(Self::BX),
0b100 => Ok(Self::SP),
0b101 => Ok(Self::BP),
0b110 => Ok(Self::SI),
0b111 => Ok(Self::DI),
_ => Err(DisasmError::UnknownRegister(w as usize)),
},
}
}
@@ -100,13 +100,13 @@ pub enum SegmentRegister {
#[allow(dead_code)]
impl SegmentRegister {
/// Find the SRegister corresponding to the 8086 bytecode ID
pub fn by_id(id: u8) -> Self {
pub fn by_id(id: u8) -> Result<Self, DisasmError> {
match id {
0x00 => Self::ES,
0x01 => Self::CS,
0x10 => Self::SS,
0x11 => Self::DS,
_ => panic!("Invalid segment register ID encountered"),
0x00 => Ok(Self::ES),
0x01 => Ok(Self::CS),
0x10 => Ok(Self::SS),
0x11 => Ok(Self::DS),
_ => Err(DisasmError::UnknownRegister(id as usize)),
}
}
}