nut_client/
error.rs

1use core::fmt;
2use std::io;
3
4/// A NUT-native error.
5#[derive(Debug)]
6pub enum NutError {
7    /// Occurs when the username/password combination is rejected.
8    AccessDenied,
9    /// Occurs when the specified UPS device does not exist.
10    UnknownUps,
11    /// Occurs when the response type or content wasn't expected at the current stage.
12    UnexpectedResponse,
13    /// Occurs when the response type is not recognized by the client.
14    UnknownResponseType(String),
15    /// Occurs when attempting to use SSL in a transport that doesn't support it, or
16    /// if the server is not configured for it.
17    SslNotSupported,
18    /// Occurs when trying to initialize a strict SSL connection with an invalid hostname.
19    SslInvalidHostname,
20    /// Occurs when the client used a feature that is disabled by the server.
21    FeatureNotConfigured,
22    /// Generic (usually internal) client error.
23    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/// Encapsulation for errors emitted by the client library.
47#[derive(Debug)]
48pub enum ClientError {
49    /// Encapsulates IO errors.
50    Io(io::Error),
51    /// Encapsulates NUT and client-specific errors.
52    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
78/// Result type for [`ClientError`]
79pub type Result<T> = std::result::Result<T, ClientError>;