Skip to main content

rusty_javac/bytecode/
error.rs

1#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
2#[error("{message}")]
3pub struct BytecodeError {
4    pub code: &'static str,
5    pub message: String,
6    pub line: Option<u16>,
7    pub needle: Option<String>,
8    pub label: Option<String>,
9    pub help: Option<String>,
10}
11
12impl BytecodeError {
13    pub fn new(message: impl Into<String>) -> Self {
14        Self {
15            code: "B0001",
16            message: message.into(),
17            line: None,
18            needle: None,
19            label: None,
20            help: None,
21        }
22    }
23
24    pub fn at_line(message: impl Into<String>, line: Option<u16>) -> Self {
25        Self {
26            code: "B0001",
27            message: message.into(),
28            line,
29            needle: None,
30            label: None,
31            help: None,
32        }
33    }
34
35    pub fn with_code(mut self, code: &'static str) -> Self {
36        self.code = code;
37        self
38    }
39
40    pub fn with_needle(mut self, needle: impl Into<String>) -> Self {
41        self.needle = Some(needle.into());
42        self
43    }
44
45    pub fn with_label(mut self, label: impl Into<String>) -> Self {
46        self.label = Some(label.into());
47        self
48    }
49
50    pub fn with_help(mut self, help: impl Into<String>) -> Self {
51        self.help = Some(help.into());
52        self
53    }
54}
55
56impl From<String> for BytecodeError {
57    fn from(value: String) -> Self {
58        Self::new(value)
59    }
60}
61
62impl From<&str> for BytecodeError {
63    fn from(value: &str) -> Self {
64        Self::new(value)
65    }
66}