use prometeu_bytecode::TrapInfo; use prometeu_vm::VmInitError; use std::fmt; #[derive(Debug, Clone, PartialEq, Eq)] pub enum CrashReport { VmTrap { trap: TrapInfo }, VmPanic { message: String, pc: Option }, VmInit { error: VmInitError }, } impl CrashReport { pub fn kind(&self) -> &'static str { match self { Self::VmTrap { .. } => "vm_trap", Self::VmPanic { .. } => "vm_panic", Self::VmInit { .. } => "vm_init", } } pub fn pc(&self) -> Option { match self { Self::VmTrap { trap } => Some(trap.pc), Self::VmPanic { pc, .. } => *pc, Self::VmInit { .. } => None, } } pub fn summary(&self) -> String { match self { Self::VmTrap { trap } => format!( "PVM Trap 0x{:08X} at PC 0x{:X} (opcode 0x{:04X}): {}", trap.code, trap.pc, trap.opcode, trap.message ), Self::VmPanic { message, pc: Some(pc) } => { format!("PVM Panic at PC 0x{:X}: {}", pc, message) } Self::VmPanic { message, pc: None } => format!("PVM Panic: {}", message), Self::VmInit { error } => format!("PVM Init Error: {:?}", error), } } pub fn log_tag(&self) -> u16 { match self { Self::VmTrap { trap } => trap.code as u16, Self::VmPanic { .. } | Self::VmInit { .. } => 0, } } } impl fmt::Display for CrashReport { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.summary()) } }