Implement LIST CMD

Fixes #18
This commit is contained in:
Aram 🍐 2021-08-01 00:21:25 -04:00
parent 11f70642dd
commit f3814c831d
3 changed files with 50 additions and 1 deletions

View file

@ -41,6 +41,12 @@ async fn main() -> nut_client::Result<()> {
for var in conn.list_vars(&name).await? {
println!("\t\t- {}", var);
}
// List UPS commands
println!("\t Commands:");
for cmd in conn.list_commands(&name).await? {
println!("\t\t- {}", cmd);
}
}
// Gracefully shut down the connection using the `LOGOUT` command

View file

@ -40,6 +40,12 @@ fn main() -> nut_client::Result<()> {
for var in conn.list_vars(&name)? {
println!("\t\t- {}", var);
}
// List UPS commands
println!("\t Commands:");
for cmd in conn.list_commands(&name)? {
println!("\t\t- {}", cmd);
}
}
// Gracefully shut down the connection using the `LOGOUT` command

View file

@ -76,6 +76,10 @@ pub enum Response {
///
/// Params: (client IP)
Client(String),
/// A command (CMD) response.
///
/// Params: (command name)
Cmd(String),
}
impl Response {
@ -196,6 +200,23 @@ impl Response {
}?;
Ok(Response::Client(ip_address))
}
"CMD" => {
let _device = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified CMD device in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let name = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified CMD name in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
Ok(Response::Cmd(name))
}
_ => Err(NutError::UnknownResponseType(cmd_name).into()),
}
}
@ -243,6 +264,14 @@ impl Response {
Err(NutError::UnexpectedResponse.into())
}
}
pub fn expect_cmd(&self) -> crate::Result<String> {
if let Self::Cmd(name) = &self {
Ok(name.to_owned())
} else {
Err(NutError::UnexpectedResponse.into())
}
}
}
/// A macro for implementing `LIST` commands.
@ -451,7 +480,7 @@ implement_list_commands! {
)
}
/// Queries a list of client IP addresses connected to the given device.
/// Queries the list of client IP addresses connected to the given device.
pub fn list_clients(ups_name: &str) -> Vec<String> {
(
{ &["CLIENT", ups_name] },
@ -466,6 +495,14 @@ implement_list_commands! {
{ |row: Response| row.expect_var() },
)
}
/// Queries the list of commands available for the given device.
pub fn list_commands(ups_name: &str) -> Vec<String> {
(
{ &["CMD", ups_name] },
{ |row: Response| row.expect_cmd() },
)
}
}
implement_get_commands! {