// rasm-core/src/disasm/err.rs
//
//! Ошибки дизассемблирования.
use std::fmt::Display;

#[derive(Debug, Clone)]
pub enum DisasmErrorKind {
    UnknownOpcode(u8, usize),
    TruncatedInstruction,
    InvalidModRM,
    InvalidSib,
}

impl Display for DisasmErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnknownOpcode(byte, offset) => {
                write!(f, "unknown opcode 0x{byte:02X} at offset 0x{offset:X}")
            }
            Self::TruncatedInstruction => {
                write!(f, "truncated instruction — unexpected end of input")
            }
            Self::InvalidModRM => write!(f, "invalid ModRM byte"),
            Self::InvalidSib => write!(f, "invalid Sib byte"),
        }
    }
}

#[derive(Debug)]
pub struct DisasmError {
    pub kind: DisasmErrorKind,
}

impl DisasmError {
    pub fn new(kind: DisasmErrorKind) -> Self {
        Self { kind }
    }
}

impl Display for DisasmError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.kind)
    }
}
