aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 3c8b756ad9a29385506da9e3023aa3035a2ea4aa (plain) (blame)
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
use std::fmt::{self, Display, Formatter};

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum LuaXError {
    InvalidStart, // used internally
    NonTableChildren,
    NonTableAttrs,
    NeededToken(String),
    ExpectedVar,
    ExpectedExpression,
    UnterminatedStringLiteral,
}

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

impl Display for LuaXError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            LuaXError::InvalidStart => write!(f, "Invalid start"),
            LuaXError::NonTableChildren => write!(f, "Children must be tables"),
            LuaXError::NonTableAttrs => write!(f, "Attrs must be tables"),
            LuaXError::NeededToken(token) => write!(f, "Needed token: {}", token),
            LuaXError::ExpectedVar => write!(f, "Expected variable"),
            LuaXError::ExpectedExpression => write!(f, "Expected expression"),
            LuaXError::UnterminatedStringLiteral => write!(f, "Unterminated string literal"),
        }
    }
}

#[derive(Debug)]
pub enum ReluaxError {
    LuaX(LuaXError),
    Lua(rlua::Error),
    Server(String),
}

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

impl Display for ReluaxError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ReluaxError::LuaX(err) => write!(f, "{}", err),
            ReluaxError::Lua(err) => write!(f, "{}", err),
            ReluaxError::Server(err) => write!(f, "{}", err),
        }
    }
}

impl From<LuaXError> for ReluaxError {
    fn from(err: LuaXError) -> Self {
        ReluaxError::LuaX(err)
    }
}

impl From<rlua::Error> for ReluaxError {
    fn from(err: rlua::Error) -> Self {
        ReluaxError::Lua(err)
    }
}