From c1d24a82787565ae71856f2f4b7a9beca610ce5b Mon Sep 17 00:00:00 2001 From: Aram Peres Date: Sat, 31 Jul 2021 22:13:41 -0400 Subject: [PATCH] Generify GET commands --- nut-client/src/blocking/mod.rs | 23 ++---------- nut-client/src/cmd.rs | 68 +++++++++++++++++++++++++++++++--- nut-client/src/tokio/mod.rs | 27 ++------------ 3 files changed, 69 insertions(+), 49 deletions(-) diff --git a/nut-client/src/blocking/mod.rs b/nut-client/src/blocking/mod.rs index 95ac426..11286f2 100644 --- a/nut-client/src/blocking/mod.rs +++ b/nut-client/src/blocking/mod.rs @@ -3,7 +3,7 @@ use std::net::{SocketAddr, TcpStream}; use crate::blocking::stream::ConnectionStream; use crate::cmd::{Command, Response}; -use crate::{ClientError, Config, Host, NutError, Variable}; +use crate::{ClientError, Config, Host, NutError}; mod stream; @@ -20,16 +20,6 @@ impl Connection { Host::Tcp(host) => Ok(Self::Tcp(TcpConnection::new(config.clone(), &host.addr)?)), } } - - /// Queries one variable for a UPS device. - pub fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result { - match self { - Self::Tcp(conn) => { - let var = conn.get_var(ups_name, variable)?; - Ok(Variable::parse(var.0.as_str(), var.1)) - } - } - } } /// A blocking TCP NUT client connection. @@ -129,13 +119,6 @@ impl TcpConnection { Ok(()) } - fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result<(String, String)> { - let query = &["VAR", ups_name, variable]; - self.write_cmd(Command::Get(query))?; - - self.read_response()?.expect_var() - } - #[allow(dead_code)] fn get_network_version(&mut self) -> crate::Result { self.write_cmd(Command::NetworkVersion)?; @@ -170,13 +153,13 @@ impl TcpConnection { Ok(args) } - fn read_response(&mut self) -> crate::Result { + pub(crate) fn read_response(&mut self) -> crate::Result { let mut reader = BufReader::new(&mut self.stream); let args = Self::parse_line(&mut reader, self.config.debug)?; Response::from_args(args) } - fn read_plain_response(&mut self) -> crate::Result { + pub(crate) fn read_plain_response(&mut self) -> crate::Result { let mut reader = BufReader::new(&mut self.stream); let args = Self::parse_line(&mut reader, self.config.debug)?; Ok(args.join(" ")) diff --git a/nut-client/src/cmd.rs b/nut-client/src/cmd.rs index eeb6ad2..1f8aff3 100644 --- a/nut-client/src/cmd.rs +++ b/nut-client/src/cmd.rs @@ -214,9 +214,9 @@ impl Response { } } - pub fn expect_var(&self) -> crate::Result<(String, String)> { + pub fn expect_var(&self) -> crate::Result { if let Self::Var(name, value) = &self { - Ok((name.to_owned(), value.to_owned())) + Ok(Variable::parse(name, value.to_owned())) } else { Err(NutError::UnexpectedResponse.into()) } @@ -289,12 +289,60 @@ macro_rules! implement_list_commands { }; } +/// A macro for implementing `GET` commands. +/// +/// Each function should return a 2-tuple with +/// (1) the query to pass to `GET` +/// (2) a closure for mapping the `Response` row to the return type +macro_rules! implement_get_commands { + ( + $( + $(#[$attr:meta])+ + fn $name:ident($($argname:ident: $argty:ty),*) -> $retty:ty { + ( + $query:block, + $mapper:block, + ) + } + )* + ) => { + impl crate::blocking::Connection { + $( + $(#[$attr])* + pub fn $name(&mut self$(, $argname: $argty)*) -> crate::Result<$retty> { + match self { + Self::Tcp(conn) => { + conn.write_cmd(Command::Get($query))?; + ($mapper)(conn.read_response()?) + }, + } + } + )* + } + + #[cfg(feature = "async")] + impl crate::tokio::Connection { + $( + $(#[$attr])* + pub async fn $name(&mut self$(, $argname: $argty)*) -> crate::Result<$retty> { + match self { + Self::Tcp(conn) => { + conn.write_cmd(Command::Get($query)).await?; + ($mapper)(conn.read_response().await?) + }, + } + } + )* + } + }; +} + implement_list_commands! { /// Queries a list of UPS devices. fn list_ups() -> Vec<(String, String)> { ( { &["UPS"] }, - { |row| row.expect_ups() }, + { |row: Response| row.expect_ups() }, ) } @@ -302,7 +350,7 @@ implement_list_commands! { fn list_clients(ups_name: &str) -> Vec { ( { &["CLIENT", ups_name] }, - { |row| row.expect_client() }, + { |row: Response| row.expect_client() }, ) } @@ -310,7 +358,17 @@ implement_list_commands! { fn list_vars(ups_name: &str) -> Vec { ( { &["VAR", ups_name] }, - { |row| row.expect_var().map(|var| Variable::parse(var.0.as_str(), var.1)) }, + { |row: Response| row.expect_var() }, + ) + } +} + +implement_get_commands! { + /// Queries one variable for a UPS device. + fn get_var(ups_name: &str, variable: &str) -> Variable { + ( + { &["VAR", ups_name, variable] }, + { |row: Response| row.expect_var() }, ) } } diff --git a/nut-client/src/tokio/mod.rs b/nut-client/src/tokio/mod.rs index 4943290..6e1e563 100644 --- a/nut-client/src/tokio/mod.rs +++ b/nut-client/src/tokio/mod.rs @@ -2,7 +2,7 @@ use std::net::SocketAddr; use crate::cmd::{Command, Response}; use crate::tokio::stream::ConnectionStream; -use crate::{Config, Host, NutError, Variable}; +use crate::{Config, Host, NutError}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::net::TcpStream; @@ -23,16 +23,6 @@ impl Connection { )), } } - - /// Queries one variable for a UPS device. - pub async fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result { - match self { - Self::Tcp(conn) => { - let var = conn.get_var(ups_name, variable).await?; - Ok(Variable::parse(var.0.as_str(), var.1)) - } - } - } } /// A blocking TCP NUT client connection. @@ -136,17 +126,6 @@ impl TcpConnection { Ok(()) } - async fn get_var<'a>( - &mut self, - ups_name: &'a str, - variable: &'a str, - ) -> crate::Result<(String, String)> { - let query = &["VAR", ups_name, variable]; - self.write_cmd(Command::Get(query)).await?; - - self.read_response().await?.expect_var() - } - #[allow(dead_code)] async fn get_network_version(&mut self) -> crate::Result { self.write_cmd(Command::NetworkVersion).await?; @@ -181,13 +160,13 @@ impl TcpConnection { Ok(args) } - async fn read_response(&mut self) -> crate::Result { + pub(crate) async fn read_response(&mut self) -> crate::Result { let mut reader = BufReader::new(&mut self.stream); let args = Self::parse_line(&mut reader, self.config.debug).await?; Response::from_args(args) } - async fn read_plain_response(&mut self) -> crate::Result { + pub(crate) async fn read_plain_response(&mut self) -> crate::Result { let mut reader = BufReader::new(&mut self.stream); let args = Self::parse_line(&mut reader, self.config.debug).await?; Ok(args.join(" "))