version: 0.0.3

feat: list UPS variables
docs: improve docs and enforce
This commit is contained in:
Aram 🍐 2020-11-18 00:14:55 -05:00
parent b36c855b2c
commit 0ba5e4565f
8 changed files with 66 additions and 13 deletions

View file

@ -7,10 +7,12 @@ use crate::{ClientError, Config, Host, NutError};
/// A blocking NUT client connection.
pub enum Connection {
/// A TCP connection.
Tcp(TcpConnection),
}
impl Connection {
/// Initializes a connection to a NUT server (upsd).
pub fn new(config: Config) -> crate::Result<Self> {
match &config.host {
Host::Tcp(socket_addr) => {
@ -19,11 +21,19 @@ impl Connection {
}
}
/// Queries a list of UPS devices.
pub fn list_ups(&mut self) -> crate::Result<Vec<(String, String)>> {
match self {
Self::Tcp(conn) => conn.list_ups(),
}
}
/// Queries the list of variables for a UPS device.
pub fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<(String, String)>> {
match self {
Self::Tcp(conn) => conn.list_vars(ups_name),
}
}
}
/// A blocking TCP NUT client connection.
@ -70,6 +80,17 @@ impl TcpConnection {
.collect())
}
fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<(String, String)>> {
let query = &["VAR", ups_name];
Self::write_cmd(&mut self.tcp_stream, Command::List(query))?;
let list = Self::read_list(&mut self.tcp_stream, query)?;
Ok(list
.into_iter()
.map(|mut row| (row.remove(0), row.remove(0)))
.collect())
}
fn write_cmd(stream: &mut TcpStream, line: Command) -> crate::Result<()> {
let line = format!("{}\n", line);
stream.write_all(line.as_bytes())?;