1use core::fmt;
2use std::io;
3
4#[derive(Debug)]
6pub enum NutError {
7 AccessDenied,
9 UnknownUps,
11 UnexpectedResponse,
13 UnknownResponseType(String),
15 SslNotSupported,
18 SslInvalidHostname,
20 FeatureNotConfigured,
22 Generic(String),
24}
25
26impl fmt::Display for NutError {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 Self::AccessDenied => write!(f, "Authentication failed"),
30 Self::UnknownUps => write!(f, "Unknown UPS device name"),
31 Self::UnexpectedResponse => write!(f, "Unexpected server response content"),
32 Self::UnknownResponseType(ty) => write!(f, "Unknown response type: {}", ty),
33 Self::SslNotSupported => write!(f, "SSL not supported by server or transport"),
34 Self::SslInvalidHostname => write!(
35 f,
36 "Given hostname cannot be used for a strict SSL connection"
37 ),
38 Self::FeatureNotConfigured => write!(f, "Feature not configured by server"),
39 Self::Generic(msg) => write!(f, "Internal client error: {}", msg),
40 }
41 }
42}
43
44impl std::error::Error for NutError {}
45
46#[derive(Debug)]
48pub enum ClientError {
49 Io(io::Error),
51 Nut(NutError),
53}
54
55impl fmt::Display for ClientError {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match self {
58 Self::Io(err) => err.fmt(f),
59 Self::Nut(err) => err.fmt(f),
60 }
61 }
62}
63
64impl std::error::Error for ClientError {}
65
66impl From<io::Error> for ClientError {
67 fn from(err: io::Error) -> Self {
68 ClientError::Io(err)
69 }
70}
71
72impl From<NutError> for ClientError {
73 fn from(err: NutError) -> Self {
74 ClientError::Nut(err)
75 }
76}
77
78pub type Result<T> = std::result::Result<T, ClientError>;