From f3814c831d2ce0b554b8b6d2dc25fd54c1a71861 Mon Sep 17 00:00:00 2001 From: Aram Peres Date: Sun, 1 Aug 2021 00:21:25 -0400 Subject: [PATCH] Implement LIST CMD Fixes #18 --- nut-client/examples/async.rs | 6 +++++ nut-client/examples/blocking.rs | 6 +++++ nut-client/src/cmd.rs | 39 ++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/nut-client/examples/async.rs b/nut-client/examples/async.rs index e3c9bc3..75ab5ae 100644 --- a/nut-client/examples/async.rs +++ b/nut-client/examples/async.rs @@ -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 diff --git a/nut-client/examples/blocking.rs b/nut-client/examples/blocking.rs index 90435e3..cf22fc3 100644 --- a/nut-client/examples/blocking.rs +++ b/nut-client/examples/blocking.rs @@ -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 diff --git a/nut-client/src/cmd.rs b/nut-client/src/cmd.rs index eaadc94..6b434a4 100644 --- a/nut-client/src/cmd.rs +++ b/nut-client/src/cmd.rs @@ -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 { + 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 { ( { &["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 { + ( + { &["CMD", ups_name] }, + { |row: Response| row.expect_cmd() }, + ) + } } implement_get_commands! {