ddmw_client/
err.rs

1//! Error values.
2
3use std::fmt;
4
5use tokio::io;
6
7use blather::Params;
8
9/// `ddmw-client` error values.
10#[derive(Debug)]
11pub enum Error {
12  /// An error occurred in the Blather communications library.
13  Blather(String),
14
15  /// A `std::io` or `tokio::io` error occurred.
16  IO(String),
17
18  /// A DDMW core server return `Fail`.  The `Params` buffer contains details
19  /// about the error.
20  ServerError(Params),
21
22  /// A state was entered which was unexpected.  This can mean that the client
23  /// expected to receive something from the server, but received something
24  /// else, which may technically have been okay under different
25  /// circumstances.
26  BadState(String),
27
28  /// A server disconnected or the client is in a disconnected state.
29  Disconnected,
30
31  /// A function or method was called with an invalid/unknown input.
32  BadInput(String),
33
34  /// A function or method was called with incomplete or ambiguous parameters.
35  BadParams(String),
36
37  /// Authentication was requested, but the authentication context is invalid
38  /// (missing or invalid data).
39  InvalidCredentials(String),
40
41  /// Some expected data is missing.
42  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// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :