Implement GET DESC

Fixes #15
This commit is contained in:
Aram 🍐 2021-08-01 01:10:46 -04:00
parent ea8ecdbf0c
commit fd1f72e1d0

View file

@ -88,6 +88,10 @@ pub enum Response {
///
/// Params: (var name, var value)
Rw(String, String),
/// A variable description (DESC) response.
///
/// Params: (variable description)
Desc(String),
}
impl Response {
@ -273,6 +277,30 @@ impl Response {
}?;
Ok(Response::CmdDesc(desc))
}
"DESC" => {
let _device = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified DESC device in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let _name = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified DESC name in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let desc = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified DESC description in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
Ok(Response::Desc(desc))
}
_ => Err(NutError::UnknownResponseType(cmd_name).into()),
}
}
@ -344,6 +372,14 @@ impl Response {
Err(NutError::UnexpectedResponse.into())
}
}
pub fn expect_desc(&self) -> crate::Result<String> {
if let Self::Desc(description) = &self {
Ok(description.to_owned())
} else {
Err(NutError::UnexpectedResponse.into())
}
}
}
/// A macro for implementing `LIST` commands.
@ -594,6 +630,14 @@ implement_get_commands! {
)
}
/// Queries the description of a UPS variable.
pub fn get_var_description(ups_name: &str, variable: &str) -> String {
(
{ &["DESC", ups_name, variable] },
{ |row: Response| row.expect_desc() },
)
}
/// Queries the description of a UPS command.
pub fn get_command_description(ups_name: &str, variable: &str) -> String {
(