1use 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
12 PklSend,
13 PklRecv,
14 PklMalformedResponse { message: String },
15 PklProcessStart,
16 PklServerError { pkl_error: String },
17
18 SerializeAst,
19 ParseError(String),
20 DeserializeError(String),
21
22 MsgpackSerializeError(rmp_serde::encode::Error),
23 MsgpackEncodeError(rmpv::encode::Error),
24 MsgpackDecodeError(rmpv::decode::Error),
25
26 Eof,
27 Syntax,
28 ExpectedBoolean,
29 ExpectedInteger,
30 ExpectedString,
31 ExpectedNull,
32 ExpectedArray,
33 ExpectedArrayComma,
34 ExpectedArrayEnd,
35 ExpectedMap,
36 ExpectedMapColon,
37 ExpectedMapComma,
38 ExpectedMapEnd,
39 ExpectedEnum,
40 TrailingCharacters,
41}
42
43impl ser::Error for Error {
44 fn custom<T: Display>(msg: T) -> Self {
45 Error::Message(msg.to_string())
46 }
47}
48
49impl de::Error for Error {
50 fn custom<T: Display>(msg: T) -> Self {
51 Error::Message(msg.to_string())
52 }
53}
54
55impl Display for Error {
56 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
57 match self {
58 Error::Message(msg)
59 | Error::DeserializeError(msg)
60 | Error::ParseError(msg)
61 | Error::PklServerError { pkl_error: msg }
62 | Error::PklMalformedResponse { message: msg } => formatter.write_str(msg),
63
64 Error::MsgpackDecodeError(e) => formatter.write_str(&e.to_string()),
65
66 Error::Eof => formatter.write_str("unexpected end of input"),
67
68 _ => formatter.write_str("unknown error"),
69 }
70 }
71}
72
73impl std::error::Error for Error {}
74
75impl From<std::io::Error> for Error {
76 fn from(e: std::io::Error) -> Self {
77 Error::Message(e.to_string())
78 }
79}
80
81impl From<rmp_serde::encode::Error> for Error {
82 fn from(e: rmp_serde::encode::Error) -> Self {
83 Error::MsgpackSerializeError(e)
84 }
85}
86
87impl From<rmpv::encode::Error> for Error {
88 fn from(e: rmpv::encode::Error) -> Self {
89 Error::MsgpackEncodeError(e)
90 }
91}
92
93impl From<rmpv::decode::Error> for Error {
94 fn from(e: rmpv::decode::Error) -> Self {
95 Error::MsgpackDecodeError(e)
96 }
97}
98
99impl From<Box<dyn std::error::Error>> for Error {
100 fn from(e: Box<dyn std::error::Error>) -> Self {
101 Error::Message(e.to_string())
102 }
103}