1use crate::flags::ClassFlags;
2use lvm_common::error::{MethodDescriptorErr, SignatureErr, TypeDescriptorErr};
3use lvm_common::utils::cursor::CursorError;
4use std::fmt::{Display, Formatter};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum InstructionErr {
8 UnsupportedOpCode(u8),
9 UnknownArrayType(u8),
10 Cursor(CursorError),
11 UnexpectedEof,
12}
13
14impl Display for InstructionErr {
15 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16 match self {
17 InstructionErr::UnsupportedOpCode(opcode) => {
18 write!(f, "Unsupported opcode: {:#X}", opcode)
19 }
20 InstructionErr::UnknownArrayType(atype) => {
21 write!(f, "Unknown array type: {}", atype)
22 }
23 InstructionErr::Cursor(err) => write!(f, "Cursor error: {}", err),
24 InstructionErr::UnexpectedEof => write!(f, "Unexpected end of instruction stream"),
25 }
26 }
27}
28
29impl From<CursorError> for InstructionErr {
30 fn from(value: CursorError) -> Self {
31 InstructionErr::Cursor(value)
32 }
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub enum ClassFormatErr {
37 IllegalClassFlags(ClassFlags),
38 Cursor(CursorError),
40 WrongMagic(u32),
41 TrailingBytes,
42 UnknownTag(u8),
43 TypeError(u16, String, String),
45 ConstantNotFound(u16),
46 UnknownStackFrameType(u8),
47 UnknownAttribute(String),
48 AttributeIsNotShared(String),
49 InvalidMethodHandleKind(u8),
50 Signature(SignatureErr),
51 MethodDescriptor(MethodDescriptorErr),
52 Instruction(InstructionErr),
53 TypeDescriptor(TypeDescriptorErr),
54 Format(std::fmt::Error),
55}
56
57impl Display for ClassFormatErr {
58 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
59 match self {
60 ClassFormatErr::IllegalClassFlags(flags) => {
61 write!(f, "Illegal class modifiers: {:#x}", flags.get_raw())
62 }
63 ClassFormatErr::Cursor(err) => write!(f, "Cursor error: {}", err),
64 ClassFormatErr::WrongMagic(magic) => write!(f, "Wrong magic number: {:#X}", magic),
65 ClassFormatErr::TrailingBytes => write!(f, "Trailing bytes after class file"),
66 ClassFormatErr::UnknownTag(tag) => write!(f, "Unknown constant pool tag: {}", tag),
67 ClassFormatErr::TypeError(index, expected, actual) => write!(
68 f,
69 "Type error at index {}: expected {}, got {}",
70 index, expected, actual
71 ),
72 ClassFormatErr::ConstantNotFound(index) => {
73 write!(f, "Constant not found at index {}", index)
74 }
75 ClassFormatErr::UnknownStackFrameType(frame_type) => {
76 write!(f, "Unknown stack frame type: {}", frame_type)
77 }
78 ClassFormatErr::UnknownAttribute(name) => write!(f, "Unknown attribute: {}", name),
79 ClassFormatErr::AttributeIsNotShared(name) => {
80 write!(f, "Attribute is not shared: {}", name)
81 }
82 ClassFormatErr::InvalidMethodHandleKind(kind) => {
83 write!(f, "Invalid method handle kind: {}", kind)
84 }
85 ClassFormatErr::Signature(err) => write!(f, "Signature error: {}", err),
86 ClassFormatErr::MethodDescriptor(err) => {
87 write!(f, "Method descriptor error: {}", err)
88 }
89 ClassFormatErr::Instruction(err) => write!(f, "Instruction error: {}", err),
90 ClassFormatErr::TypeDescriptor(err) => {
91 write!(f, "Type descriptor error: {}", err)
92 }
93 ClassFormatErr::Format(err) => write!(f, "Format error: {}", err),
94 }
95 }
96}
97
98impl From<CursorError> for ClassFormatErr {
99 fn from(value: CursorError) -> Self {
100 ClassFormatErr::Cursor(value)
101 }
102}
103
104impl From<SignatureErr> for ClassFormatErr {
105 fn from(value: SignatureErr) -> Self {
106 ClassFormatErr::Signature(value)
107 }
108}
109
110impl From<MethodDescriptorErr> for ClassFormatErr {
111 fn from(value: MethodDescriptorErr) -> Self {
112 ClassFormatErr::MethodDescriptor(value)
113 }
114}
115
116impl From<InstructionErr> for ClassFormatErr {
117 fn from(value: InstructionErr) -> Self {
118 ClassFormatErr::Instruction(value)
119 }
120}
121
122impl From<TypeDescriptorErr> for ClassFormatErr {
123 fn from(value: TypeDescriptorErr) -> Self {
124 ClassFormatErr::TypeDescriptor(value)
125 }
126}
127
128impl From<std::fmt::Error> for ClassFormatErr {
129 fn from(value: std::fmt::Error) -> Self {
130 ClassFormatErr::Format(value)
131 }
132}