Implement GET NUMLOGINS

Fixes #11
This commit is contained in:
Aram 🍐 2021-08-01 01:19:31 -04:00
parent fd1f72e1d0
commit 2360bd4a3f
3 changed files with 47 additions and 0 deletions

View file

@ -35,6 +35,10 @@ async fn main() -> nut_client::Result<()> {
for (name, description) in conn.list_ups().await? {
println!("\t- Name: {}", name);
println!("\t Description: {}", description);
println!(
"\t Number of logins: {}",
conn.get_num_logins(&name).await?
);
// Get list of mutable variables
let mutable_vars = conn.list_mutable_vars(&name).await?;

View file

@ -34,6 +34,7 @@ fn main() -> nut_client::Result<()> {
for (name, description) in conn.list_ups()? {
println!("\t- Name: {}", name);
println!("\t Description: {}", description);
println!("\t Number of logins: {}", conn.get_num_logins(&name)?);
// Get list of mutable variables
let mutable_vars = conn.list_mutable_vars(&name)?;

View file

@ -92,6 +92,10 @@ pub enum Response {
///
/// Params: (variable description)
Desc(String),
/// A NUMLOGINS response.
///
/// Params (number of logins)
NumLogins(i32),
}
impl Response {
@ -301,6 +305,28 @@ impl Response {
}?;
Ok(Response::Desc(desc))
}
"NUMLOGINS" => {
let _device = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified NUMLOGINS device in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let num = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified NUMLOGINS number in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let num = num.parse::<i32>().map_err(|_| {
ClientError::from(NutError::Generic(
"Invalid NUMLOGINS number in response".into(),
))
})?;
Ok(Response::NumLogins(num))
}
_ => Err(NutError::UnknownResponseType(cmd_name).into()),
}
}
@ -380,6 +406,14 @@ impl Response {
Err(NutError::UnexpectedResponse.into())
}
}
pub fn expect_numlogins(&self) -> crate::Result<i32> {
if let Self::NumLogins(num) = &self {
Ok(*num)
} else {
Err(NutError::UnexpectedResponse.into())
}
}
}
/// A macro for implementing `LIST` commands.
@ -645,6 +679,14 @@ implement_get_commands! {
{ |row: Response| row.expect_cmddesc() },
)
}
/// Queries the number of logins to the specified UPS.
pub fn get_num_logins(ups_name: &str) -> i32 {
(
{ &["NUMLOGINS", ups_name] },
{ |row: Response| row.expect_numlogins() },
)
}
}
implement_simple_commands! {