Add support for running commands

This commit is contained in:
Raj Vengalil 2022-08-06 13:54:41 +05:30
parent 35d40d3111
commit 839ce6f119
2 changed files with 36 additions and 0 deletions

View file

@ -26,6 +26,7 @@ default = []
ssl = ["rustls", "rustls/dangerous_configuration", "webpki", "webpki-roots"]
async = ["tokio"]
async-ssl = ["async", "tokio-rustls", "ssl"]
write = []
# a feature gate for examples
async-rt = ["async", "tokio/rt-multi-thread", "tokio/macros"]

View file

@ -18,6 +18,9 @@ pub enum Command<'a> {
NetworkVersion,
/// Queries the server version.
Version,
#[cfg(feature = "write")]
/// Run a command. Allow for on additional optional param.
Run(&'a str, Option<&'a str>),
/// Gracefully shuts down the connection.
Logout,
}
@ -33,6 +36,8 @@ impl<'a> Command<'a> {
Self::StartTLS => "STARTTLS",
Self::NetworkVersion => "NETVER",
Self::Version => "VER",
#[cfg(feature = "write")]
Self::Run(_, _) => "INSTCMD",
Self::Logout => "LOGOUT",
}
}
@ -44,6 +49,8 @@ impl<'a> Command<'a> {
Self::SetUsername(username) => vec![username],
Self::SetPassword(password) => vec![password],
Self::List(query) => query.to_vec(),
#[cfg(feature = "write")]
Self::Run(cmd, param) => vec![cmd, param.unwrap_or("")],
_ => Vec::new(),
}
}
@ -855,3 +862,31 @@ implement_action_commands! {
Command::Logout
}
}
#[cfg(feature = "write")]
impl crate::blocking::Connection {
/// Runs a command on the UPS.
pub fn run_command(&mut self, cmd: &str, param: Option<&str>) -> crate::Result<()> {
match self {
Self::Tcp(conn) => {
conn.write_cmd(Command::Run(cmd, param))?;
conn.read_response()?.expect_ok()?;
Ok(())
}
}
}
}
#[cfg(all(feature = "write", feature = "async"))]
impl crate::tokio::Connection {
/// Runs a command on the UPS.
pub async fn run_command(&mut self, cmd: &str, param: Option<&str>) -> crate::Result<()> {
match self {
Self::Tcp(conn) => {
conn.write_cmd(Command::Run(cmd, param)).await?;
conn.read_response().await?.expect_ok()?;
Ok(())
}
}
}
}