From fd1f72e1d02d20e86f1320df4af6ab59f15fc6cc Mon Sep 17 00:00:00 2001 From: Aram Peres Date: Sun, 1 Aug 2021 01:10:46 -0400 Subject: [PATCH] Implement GET DESC Fixes #15 --- nut-client/src/cmd.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/nut-client/src/cmd.rs b/nut-client/src/cmd.rs index f4022fe..199a859 100644 --- a/nut-client/src/cmd.rs +++ b/nut-client/src/cmd.rs @@ -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 { + 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 { (