Implement GET CMDDESC

Fixes #16
This commit is contained in:
Aram 🍐 2021-08-01 00:28:31 -04:00
parent f3814c831d
commit b6e3a96aa1
3 changed files with 48 additions and 2 deletions

View file

@ -45,7 +45,8 @@ async fn main() -> nut_client::Result<()> {
// List UPS commands
println!("\t Commands:");
for cmd in conn.list_commands(&name).await? {
println!("\t\t- {}", cmd);
let description = conn.get_command_description(&name, &cmd).await?;
println!("\t\t- {} ({})", cmd, description);
}
}

View file

@ -44,7 +44,8 @@ fn main() -> nut_client::Result<()> {
// List UPS commands
println!("\t Commands:");
for cmd in conn.list_commands(&name)? {
println!("\t\t- {}", cmd);
let description = conn.get_command_description(&name, &cmd)?;
println!("\t\t- {} ({})", cmd, description);
}
}

View file

@ -80,6 +80,10 @@ pub enum Response {
///
/// Params: (command name)
Cmd(String),
/// A command description (CMDDESC) response.
///
/// Params: (command description)
CmdDesc(String),
}
impl Response {
@ -217,6 +221,30 @@ impl Response {
}?;
Ok(Response::Cmd(name))
}
"CMDDESC" => {
let _device = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified CMDDESC device in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let _name = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified CMDDESC name in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let desc = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified CMDDESC description in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
Ok(Response::CmdDesc(desc))
}
_ => Err(NutError::UnknownResponseType(cmd_name).into()),
}
}
@ -272,6 +300,14 @@ impl Response {
Err(NutError::UnexpectedResponse.into())
}
}
pub fn expect_cmddesc(&self) -> crate::Result<String> {
if let Self::CmdDesc(description) = &self {
Ok(description.to_owned())
} else {
Err(NutError::UnexpectedResponse.into())
}
}
}
/// A macro for implementing `LIST` commands.
@ -513,6 +549,14 @@ implement_get_commands! {
{ |row: Response| row.expect_var() },
)
}
/// Queries the description of a UPS command.
pub fn get_command_description(ups_name: &str, variable: &str) -> String {
(
{ &["CMDDESC", ups_name, variable] },
{ |row: Response| row.expect_cmddesc() },
)
}
}
implement_simple_commands! {