use crate::proto::ClientSentences;
use core::fmt;
use std::io;
#[derive(Debug)]
pub enum NutError {
AccessDenied,
UnknownUps,
VarNotSupported,
CmdNotSupported,
InvalidArgument,
InstCmdFailed,
SetFailed,
ReadOnly,
TooLong,
FeatureNotSupported,
AlreadySslMode,
DriverNotConnected,
DataStale,
AlreadyLoggedIn,
InvalidPassword,
AlreadySetPassword,
InvalidUsername,
AlreadySetUsername,
UsernameRequired,
PasswordRequired,
UnknownCommand,
InvalidValue,
UnexpectedResponse,
UnknownResponseType(String),
SslNotSupported,
SslInvalidHostname,
FeatureNotConfigured,
Generic(String),
}
impl fmt::Display for NutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AccessDenied => write!(f, "Access denied"),
Self::UnknownUps => write!(f, "Unknown UPS device"),
Self::VarNotSupported => write!(f, "Variable not supported"),
Self::CmdNotSupported => write!(f, "Command not supported"),
Self::InvalidArgument => write!(f, "Invalid argument"),
Self::InstCmdFailed => write!(f, "Instant command failed"),
Self::SetFailed => write!(f, "Failed to set variable"),
Self::ReadOnly => write!(f, "Cannot set read-only variable"),
Self::TooLong => write!(f, "Value is too long"),
Self::FeatureNotSupported => write!(f, "Feature is not supported by server"),
Self::AlreadySslMode => write!(f, "Connection is already in TLS/SSL"),
Self::DriverNotConnected => write!(f, "Driver is not connected"),
Self::DataStale => write!(f, "Data is stale"),
Self::AlreadyLoggedIn => write!(f, "Connection is already authenticated"),
Self::InvalidPassword => write!(f, "Invalid password"),
Self::AlreadySetPassword => write!(f, "Password can only be set once"),
Self::InvalidUsername => write!(f, "Invalid username"),
Self::AlreadySetUsername => write!(f, "Username can only be set once"),
Self::UsernameRequired => write!(f, "Username required"),
Self::PasswordRequired => write!(f, "Password required"),
Self::UnknownCommand => write!(f, "Unknown command"),
Self::InvalidValue => write!(f, "Invalid value"),
Self::UnexpectedResponse => write!(f, "Unexpected server response content"),
Self::UnknownResponseType(ty) => write!(f, "Unknown response type: {}", ty),
Self::SslNotSupported => write!(f, "SSL not supported by server or transport"),
Self::SslInvalidHostname => write!(
f,
"Given hostname cannot be used for a strict SSL connection"
),
Self::FeatureNotConfigured => write!(f, "Feature not configured by server"),
Self::Generic(msg) => write!(f, "Client error: {}", msg),
}
}
}
impl<T: AsRef<ClientSentences>> From<T> for NutError {
fn from(sentence: T) -> Self {
if let ClientSentences::RespondErr { message, .. } = sentence.as_ref() {
match message.as_str() {
"ACCESS-DENIED" => Self::AccessDenied,
"UNKNOWN-UPS" => Self::UnknownUps,
"VAR-NOT-SUPPORTED" => Self::VarNotSupported,
"CMD-NOT-SUPPORTED" => Self::CmdNotSupported,
"INVALID-ARGUMENT" => Self::InvalidArgument,
"INSTCMD-FAILED" => Self::InstCmdFailed,
"SET-FAILED" => Self::SetFailed,
"READONLY" => Self::ReadOnly,
"TOO-LONG" => Self::TooLong,
"FEATURE-NOT-SUPPORTED" => Self::FeatureNotSupported,
"FEATURE-NOT-CONFIGURED" => Self::FeatureNotConfigured,
"ALREADY-SSL-MODE" => Self::AlreadySslMode,
"DRIVER-NOT-CONNECTED" => Self::DriverNotConnected,
"DATA-STALE" => Self::DataStale,
"ALREADY-LOGGED-IN" => Self::AlreadyLoggedIn,
"INVALID-PASSWORD" => Self::InvalidPassword,
"ALREADY-SET-PASSWORD" => Self::AlreadySetPassword,
"INVALID-USERNAME" => Self::InvalidUsername,
"ALREADY-SET-USERNAME" => Self::AlreadySetUsername,
"USERNAME-REQUIRED" => Self::UsernameRequired,
"PASSWORD-REQUIRED" => Self::PasswordRequired,
"UNKNOWN-COMMAND" => Self::UnknownCommand,
"INVALID-VALUE" => Self::InvalidValue,
_ => Self::Generic(message.to_string()),
}
} else {
panic!("Cannot convert {:?} into NutError", sentence.as_ref());
}
}
}
impl NutError {
pub fn generic<T: ToString>(message: T) -> Self {
Self::Generic(message.to_string())
}
}
impl std::error::Error for NutError {}
#[derive(Debug)]
pub enum ClientError {
Io(io::Error),
Nut(NutError),
}
impl ClientError {
pub fn generic<T: ToString>(message: T) -> Self {
NutError::generic(message.to_string()).into()
}
}
impl fmt::Display for ClientError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(err) => err.fmt(f),
Self::Nut(err) => err.fmt(f),
}
}
}
impl std::error::Error for ClientError {}
impl From<io::Error> for ClientError {
fn from(err: io::Error) -> Self {
ClientError::Io(err)
}
}
impl From<NutError> for ClientError {
fn from(err: NutError) -> Self {
ClientError::Nut(err)
}
}
pub type Result<T> = std::result::Result<T, ClientError>;