From 59c9ec15126d3fb076b5b13faed2f248a3221029 Mon Sep 17 00:00:00 2001 From: evan Adam Date: Fri, 13 Jun 2025 16:28:55 +0200 Subject: [PATCH] enh(generic-snmp): apply code suggestion from cargo build --- experimental/src/compute/lexer.rs | 2 +- experimental/src/compute/mod.rs | 5 ++-- experimental/src/compute/threshold.rs | 18 +++++++------- experimental/src/main.rs | 2 +- experimental/src/output/mod.rs | 34 ++++++++++++++------------- experimental/src/snmp/mod.rs | 6 ++--- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/experimental/src/compute/lexer.rs b/experimental/src/compute/lexer.rs index f26da9fcd..bfa9fe888 100644 --- a/experimental/src/compute/lexer.rs +++ b/experimental/src/compute/lexer.rs @@ -156,7 +156,7 @@ impl<'input> Iterator for Lexer<'input> { } mod test { - use super::*; + use crate::compute::lexer::{Lexer, Tok}; fn init() { let _ = env_logger::builder().is_test(true).try_init(); diff --git a/experimental/src/compute/mod.rs b/experimental/src/compute/mod.rs index 797f2c2fa..475da4f14 100644 --- a/experimental/src/compute/mod.rs +++ b/experimental/src/compute/mod.rs @@ -99,8 +99,9 @@ impl<'a> Parser<'a> { } mod test { - use super::*; - use log::info; + use crate::compute::{Parser, ast::ExprResult, grammar, lexer}; + use crate::snmp::SnmpResult; + use log::{debug, info}; use std::collections::HashMap; fn init() { diff --git a/experimental/src/compute/threshold.rs b/experimental/src/compute/threshold.rs index e07100d3d..4089e1848 100644 --- a/experimental/src/compute/threshold.rs +++ b/experimental/src/compute/threshold.rs @@ -1,5 +1,4 @@ use crate::generic::error::Error; -use log::{debug, error, info, trace, warn}; use std::f64::INFINITY; pub struct Threshold { @@ -108,17 +107,14 @@ impl Threshold { return true; } } - if self.negation { - true - } else { - false - } + if self.negation { true } else { false } } } mod test { - use super::*; - + use crate::compute::threshold::Threshold; + use crate::generic::error::Error; + use std::f64::INFINITY; #[test] fn test_parse_value() { let expr = "1.2"; @@ -302,8 +298,10 @@ mod test { panic!("The threshold '{}' should not be valid", expr); } Err(err) => { - assert_eq!(err.to_string(), - "Threshold: This syntax is a shortcut of '0:-12', so -12 must be greater than 0."); + assert_eq!( + err.to_string(), + "Threshold: This syntax is a shortcut of '0:-12', so -12 must be greater than 0." + ); } } } diff --git a/experimental/src/main.rs b/experimental/src/main.rs index b5f4482a8..5061abf5f 100644 --- a/experimental/src/main.rs +++ b/experimental/src/main.rs @@ -16,8 +16,8 @@ mod output; mod snmp; use env_logger::Env; -use generic::error::*; use generic::Command; +use generic::error::*; use lalrpop_util::lalrpop_mod; use lexopt::Arg; use log::trace; diff --git a/experimental/src/output/mod.rs b/experimental/src/output/mod.rs index f7ed3f805..e85cbdfed 100644 --- a/experimental/src/output/mod.rs +++ b/experimental/src/output/mod.rs @@ -125,25 +125,27 @@ impl<'a> OutputFormatter<'a> { let parser = Parser::new(&self.collect); let res = parser.eval_str(&self.output_formatter.ok); let output = match res { - Ok(output) => { - match output { - ExprResult::Str(output) => output, - ExprResult::Number(_) => { - error!("Output expression evaluated to a number, expected a string"); + Ok(output) => match output { + ExprResult::Str(output) => output, + ExprResult::Number(_) => { + error!( + "Output expression evaluated to a number, expected a string" + ); + return "".to_string(); + } + ExprResult::StrVector(v) => { + if v.len() == 1 { + let output = v[0].clone(); + output + } else { + error!( + "Output expression evaluated to a vector with more than one element, expected a single string" + ); return "".to_string(); } - ExprResult::StrVector(v) => { - if v.len() == 1 { - let output = v[0].clone(); - output - } else { - error!("Output expression evaluated to a vector with more than one element, expected a single string"); - return "".to_string(); - } - } - _ => "".to_string(), } - } + _ => "".to_string(), + }, Err(err) => { error!("Error evaluating output expression: {:?}", err); self.output_formatter.ok.clone() diff --git a/experimental/src/snmp/mod.rs b/experimental/src/snmp/mod.rs index 678253cc9..f9daea01a 100644 --- a/experimental/src/snmp/mod.rs +++ b/experimental/src/snmp/mod.rs @@ -10,7 +10,7 @@ use rasn_snmp::v2::BulkPdu; use rasn_snmp::v2::Pdus; use rasn_snmp::v2::VarBind; use rasn_snmp::v2::VarBindValue; -use rasn_snmp::v2::{GetBulkRequest, GetNextRequest, GetRequest}; +use rasn_snmp::v2::GetBulkRequest; use rasn_snmp::v2c::Message; use std::collections::HashMap; use std::convert::TryInto; @@ -173,7 +173,7 @@ pub fn snmp_bulk_get<'a>( oid: &Vec<&str>, names: &Vec<&str>, ) -> SnmpResult { - let mut oids_tab = oid + let oids_tab = oid .iter() .map(|x| { x.split('.') @@ -186,7 +186,7 @@ pub fn snmp_bulk_get<'a>( items: HashMap::new(), last_oid: Vec::new(), }; - let mut request_id: i32 = 1; + let request_id: i32 = 1; let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); socket.connect(target).expect("connect function failed");