diff options
Diffstat (limited to 'src/lua/serde/error.rs')
-rw-r--r-- | src/lua/serde/error.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/lua/serde/error.rs b/src/lua/serde/error.rs new file mode 100644 index 0000000..8d5debf --- /dev/null +++ b/src/lua/serde/error.rs @@ -0,0 +1,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)), + }) + } +} |