aboutsummaryrefslogtreecommitdiff
path: root/src/lua/serde/error.rs
blob: 8d5debf66d9bf204d19b56faca3caf0b01ed9d26 (plain)
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
// vendored from https://github.com/zrkn/rlua_serde because it dependend on an outdated rlua version
// Copyright (c) 2018 zrkn <zrkn@email.su>, licensed under the MIT License

use std::error::Error as StdError;
use std::fmt;
use std::result::Result as StdResult;

use rlua::Error as LuaError;
use serde;

#[derive(Debug)]
pub struct Error(LuaError);

pub type Result<T> = StdResult<T, Error>;

impl From<LuaError> for Error {
    fn from(err: LuaError) -> Error {
        Error(err)
    }
}

impl From<Error> for LuaError {
    fn from(err: Error) -> LuaError {
        err.0
    }
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(fmt)
    }
}

impl StdError for Error {
    fn description(&self) -> &'static str {
        "Failed to serialize to Lua value"
    }
}

impl serde::ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error(LuaError::ToLuaConversionError {
            from: "serialize",
            to: "value",
            message: Some(format!("{}", msg)),
        })
    }
}

impl serde::de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error(LuaError::FromLuaConversionError {
            from: "value",
            to: "deserialize",
            message: Some(format!("{}", msg)),
        })
    }
}