From b6e3a96aa13aab015bac8ffcb0326c1f229629fd Mon Sep 17 00:00:00 2001 From: Aram Peres Date: Sun, 1 Aug 2021 00:28:31 -0400 Subject: [PATCH] Implement GET CMDDESC Fixes #16 --- nut-client/examples/async.rs | 3 ++- nut-client/examples/blocking.rs | 3 ++- nut-client/src/cmd.rs | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/nut-client/examples/async.rs b/nut-client/examples/async.rs index 75ab5ae..7dcc948 100644 --- a/nut-client/examples/async.rs +++ b/nut-client/examples/async.rs @@ -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); } } diff --git a/nut-client/examples/blocking.rs b/nut-client/examples/blocking.rs index cf22fc3..2bfaa0e 100644 --- a/nut-client/examples/blocking.rs +++ b/nut-client/examples/blocking.rs @@ -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); } } diff --git a/nut-client/src/cmd.rs b/nut-client/src/cmd.rs index 6b434a4..6af9834 100644 --- a/nut-client/src/cmd.rs +++ b/nut-client/src/cmd.rs @@ -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 { + 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! {