rust_openttd_admin/packet/serde/
error.rs1use std;
2use std::fmt::{self, Display};
3
4use serde::{de, ser};
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug)]
9pub enum Error {
10 Message(String),
11 TrailingCharacters,
12 NotSupported,
13 EndlessString,
14 InvalidBool,
15 InvalidOption,
17 InvalidChar,
19 IoError(std::io::Error),
20 Utf8Error(std::str::Utf8Error),
21}
22
23impl ser::Error for Error {
24 fn custom<T: Display>(msg: T) -> Self {
25 Error::Message(msg.to_string())
26 }
27}
28
29impl de::Error for Error {
30 fn custom<T: Display>(msg: T) -> Self {
31 Error::Message(msg.to_string())
32 }
33}
34
35impl Display for Error {
36 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
37 formatter.write_str(std::error::Error::description(self))
38 }
39}
40
41impl std::error::Error for Error {
42 fn description(&self) -> &str {
43 match *self {
44 Error::Message(ref msg) => msg,
45 Error::TrailingCharacters => "the input has trailing characters",
46 Error::IoError(ref err) => err.description(),
47 Error::Utf8Error(ref err) => err.description(),
48 Error::NotSupported => "the data type is not supported",
49 Error::EndlessString => "string doesn't end with \0",
50 Error::InvalidBool => "invalid bool",
51 Error::InvalidOption => "invalid option",
52 Error::InvalidChar => "invalid char",
53 }
54 }
55}
56
57impl From<std::io::Error> for Error {
58 fn from(err: std::io::Error) -> Error {
59 Error::IoError(err)
60 }
61}
62
63impl From<std::str::Utf8Error> for Error {
64 fn from(err: std::str::Utf8Error) -> Error {
65 Error::Utf8Error(err)
66 }
67}