1use std::fmt;
4
5use tokio::io;
6
7use blather::Params;
8
9#[derive(Debug)]
11pub enum Error {
12 Blather(String),
14
15 IO(String),
17
18 ServerError(Params),
21
22 BadState(String),
27
28 Disconnected,
30
31 BadInput(String),
33
34 BadParams(String),
36
37 InvalidCredentials(String),
40
41 MissingData(String),
43
44 Parse(String),
45
46 Figment(String)
47}
48
49impl Error {
50 pub fn invalid_cred(e: &str) -> Self {
51 Self::InvalidCredentials(e.to_string())
52 }
53 pub fn miss_data<S: ToString>(e: S) -> Self {
54 Self::MissingData(e.to_string())
55 }
56 pub fn bad_state<S: ToString>(e: S) -> Self {
57 Self::BadState(e.to_string())
58 }
59 pub fn parse<S: ToString>(e: S) -> Self {
60 Self::Parse(e.to_string())
61 }
62}
63
64
65impl std::error::Error for Error {}
66
67impl fmt::Display for Error {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 match self {
70 Error::Blather(s) => write!(f, "Msg buffer error; {}", s),
71 Error::IO(s) => write!(f, "I/O error; {}", s),
72 Error::ServerError(p) => write!(f, "Server replied: {}", p),
73 Error::BadState(s) => {
74 write!(f, "Encountred an unexpected/bad state: {}", s)
75 }
76 Error::Disconnected => write!(f, "Disconnected"),
77 Error::BadInput(s) => write!(f, "Bad input; {}", s),
78 Error::BadParams(s) => write!(f, "Bad parameters; {}", s),
79 Error::InvalidCredentials(s) => write!(f, "Invalid credentials; {}", s),
80 Error::MissingData(s) => write!(f, "Missing data; {}", s),
81 Error::Parse(s) => write!(f, "Parsing failed; {}", s),
82 Error::Figment(s) => write!(f, "Figment error; {}", s)
83 }
84 }
85}
86
87impl From<blather::Error> for Error {
88 fn from(err: blather::Error) -> Self {
89 Error::Blather(err.to_string())
90 }
91}
92
93impl From<io::Error> for Error {
94 fn from(err: io::Error) -> Self {
95 Error::IO(err.to_string())
96 }
97}
98
99impl From<figment::Error> for Error {
100 fn from(err: figment::Error) -> Self {
101 Error::Figment(err.to_string())
102 }
103}
104
105