Skip to main content

lvm_common/
error.rs

1use std::fmt::{Display, Formatter};
2
3// TODO: looks like a trash bin, needs refactoring
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum SignatureErr {
6    UnexpectedEnd,
7    MissingParamsOpenParen,
8    MissingParamsCloseParen,
9    TrailingCharacters,
10    InvalidIdentifier,
11    MissingSuper,
12    InvalidBound,
13    Type(TypeDescriptorErr),
14    InvalidSuperClassType,
15}
16
17impl Display for SignatureErr {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        match self {
20            SignatureErr::UnexpectedEnd => write!(f, "Unexpected end of signature"),
21            SignatureErr::MissingParamsOpenParen => {
22                write!(f, "Missing opening parenthesis for method parameters")
23            }
24            SignatureErr::MissingParamsCloseParen => {
25                write!(f, "Missing closing parenthesis for method parameters")
26            }
27            SignatureErr::TrailingCharacters => write!(f, "Trailing characters after signature"),
28            SignatureErr::InvalidIdentifier => write!(f, "Invalid identifier in signature"),
29            SignatureErr::MissingSuper => write!(f, "Missing 'super' in class signature"),
30            SignatureErr::InvalidBound => write!(f, "Invalid bound in type variable"),
31            SignatureErr::Type(err) => write!(f, "Type descriptor error: {}", err),
32            SignatureErr::InvalidSuperClassType => {
33                write!(f, "Invalid superclass type in class signature")
34            }
35        }
36    }
37}
38
39impl From<TypeDescriptorErr> for SignatureErr {
40    fn from(value: TypeDescriptorErr) -> Self {
41        SignatureErr::Type(value)
42    }
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub enum TypeDescriptorErr {
47    UnexpectedEnd,
48    InvalidType(char),
49    InvalidObjectRef,
50}
51
52impl Display for TypeDescriptorErr {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        match self {
55            TypeDescriptorErr::UnexpectedEnd => write!(f, "Unexpected end of type descriptor"),
56            TypeDescriptorErr::InvalidType(c) => write!(f, "Invalid type character: {}", c),
57            TypeDescriptorErr::InvalidObjectRef => write!(f, "Invalid object reference type"),
58        }
59    }
60}
61
62#[derive(Debug, Clone, PartialEq, Eq)]
63pub enum MethodDescriptorErr {
64    ShouldStartWithParentheses(String),
65    MissingClosingParenthesis(String),
66    TrailingCharacters,
67    Type(String, TypeDescriptorErr),
68}
69
70impl Display for MethodDescriptorErr {
71    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
72        match self {
73            MethodDescriptorErr::ShouldStartWithParentheses(desc) => {
74                write!(f, "Method descriptor should start with '(': {}", desc)
75            }
76            MethodDescriptorErr::MissingClosingParenthesis(desc) => {
77                write!(
78                    f,
79                    "Missing closing parenthesis in method descriptor: {}",
80                    desc
81                )
82            }
83            MethodDescriptorErr::TrailingCharacters => {
84                write!(f, "Trailing characters after method descriptor")
85            }
86            MethodDescriptorErr::Type(desc, err) => {
87                write!(f, "Type descriptor error in '{}': {}", desc, err)
88            }
89        }
90    }
91}