~starkingdoms/starkingdoms

ref: 339f063faf61671754325b60bcad3ab02492e742 starkingdoms/kabel/src/runtime_error.rs -rw-r--r-- 1.4 KiB
339f063f — ghostly_zsh print, add, sub run with manually typed bytecode 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#[derive(Debug, Clone)]
pub struct KabelRuntimeError {
    pub kind: RuntimeErrorKind,
    pub message: String,
    pub line: usize,
    pub code: String,
}

impl KabelRuntimeError {
    pub fn new(kind: RuntimeErrorKind, message: String, line: usize, code: String) -> Self {
        Self {
            kind,
            message,
            line,
            code,
        }
    }
}

impl std::fmt::Display for KabelRuntimeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!(
            "Error {:0>4}: {1} at line {2}\n\
                    {3}\n",
            self.kind.clone() as usize,
            self.message,
            self.line + 1,
            self.code,
        ))
    }
}

impl std::error::Error for KabelRuntimeError {}

#[derive(Debug, Clone)]
pub enum RuntimeErrorKind {
    MismatchedTypes,
    WrongType,
}

impl std::fmt::Display for RuntimeErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use RuntimeErrorKind::*;
        match self {
            MismatchedTypes => f.write_str("Mismatched types"),
            WrongType => f.write_str("WrongType"),
        }
    }
}

impl From<RuntimeErrorKind> for usize {
    fn from(value: RuntimeErrorKind) -> Self {
        use RuntimeErrorKind::*;
        match value {
            MismatchedTypes => 0x06,
            WrongType => 0x07,
        }
    }
}