From 2457693d4fb0b341fab7a0b0f533483b3e4fca44 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Mon, 16 Mar 2026 00:09:55 +0100 Subject: [PATCH] minor optimizations --- src/gui/pages/connection_details_page.rs | 9 +++------ src/gui/pages/inspect_page.rs | 16 +++++----------- src/gui/pages/settings_notifications_page.rs | 2 +- src/networking/types/data_representation.rs | 16 +++++++--------- src/notifications/notify_and_log.rs | 15 ++------------- src/report/get_report_entries.rs | 10 ++++++---- 6 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/gui/pages/connection_details_page.rs b/src/gui/pages/connection_details_page.rs index b83bed2c..e27f4bda 100644 --- a/src/gui/pages/connection_details_page.rs +++ b/src/gui/pages/connection_details_page.rs @@ -61,11 +61,8 @@ fn page_content<'a>(sniffer: &Sniffer, key: &AddressPortPair) -> Container<'a, M let data_repr = sniffer.conf.data_repr; let info_traffic = &sniffer.info_traffic; - let val = info_traffic - .map - .get(key) - .unwrap_or(&InfoAddressPortPair::default()) - .clone(); + let default_val = InfoAddressPortPair::default(); + let val = info_traffic.map.get(key).unwrap_or(&default_val); let address_to_lookup = get_address_to_lookup(key, val.traffic_direction); let host_option = sniffer.addresses_resolved.get(&address_to_lookup); let default_host = Host::default(); @@ -126,7 +123,7 @@ fn page_content<'a>(sniffer: &Sniffer, key: &AddressPortPair) -> Container<'a, M dest_col = dest_col.push(host_info_col); } - let col_info = col_info(key, &val, data_repr, language); + let col_info = col_info(key, val, data_repr, language); let content = assemble_widgets(col_info, source_col, dest_col); diff --git a/src/gui/pages/inspect_page.rs b/src/gui/pages/inspect_page.rs index efaa163e..83de3f92 100644 --- a/src/gui/pages/inspect_page.rs +++ b/src/gui/pages/inspect_page.rs @@ -102,18 +102,12 @@ fn report<'a>(sniffer: &Sniffer) -> Column<'a, Message, StyleType> { let mut scroll_report = Column::new().align_x(Alignment::Start); let start_entry_num = (sniffer.page_number.saturating_sub(1)) * 30 + 1; let end_entry_num = start_entry_num + search_results.len() - 1; - for report_entry in search_results { + for (key, val) in search_results { scroll_report = scroll_report.push( - button(row_report_entry( - &report_entry.0, - &report_entry.1, - data_repr, - )) - .padding(2) - .on_press(Message::ShowModal(MyModal::ConnectionDetails( - report_entry.0, - ))) - .class(ButtonType::Neutral), + button(row_report_entry(key, val, data_repr)) + .padding(2) + .on_press(Message::ShowModal(MyModal::ConnectionDetails(*key))) + .class(ButtonType::Neutral), ); } if results_number > 0 { diff --git a/src/gui/pages/settings_notifications_page.rs b/src/gui/pages/settings_notifications_page.rs index c891f00b..13fb289f 100644 --- a/src/gui/pages/settings_notifications_page.rs +++ b/src/gui/pages/settings_notifications_page.rs @@ -270,7 +270,7 @@ fn input_group_bytes<'a>( let mut curr_threshold_str = (bytes_notification.threshold.unwrap_or_default() / bytes_notification.byte_multiple.multiplier()) .to_string(); - curr_threshold_str.push_str(&bytes_notification.byte_multiple.get_char()); + curr_threshold_str.push_str(bytes_notification.byte_multiple.get_char()); let input_row = Row::new() .spacing(5) .align_y(Alignment::Center) diff --git a/src/networking/types/data_representation.rs b/src/networking/types/data_representation.rs index 0de8ed61..67f00a66 100644 --- a/src/networking/types/data_representation.rs +++ b/src/networking/types/data_representation.rs @@ -45,8 +45,6 @@ impl DataRepr { } let precision = usize::from(byte_multiple != ByteMultiple::B && n <= 9.95); format!("{n:.precision$} {}", byte_multiple.pretty_print(self)) - .trim() - .to_string() } pub fn data_exceeded_translation(&self, language: Language) -> &str { @@ -121,14 +119,14 @@ impl ByteMultiple { } } - pub fn get_char(self) -> String { + pub fn get_char(self) -> &'static str { match self { - Self::B => String::new(), - Self::KB => "K".to_string(), - Self::MB => "M".to_string(), - Self::GB => "G".to_string(), - Self::TB => "T".to_string(), - Self::PB => "P".to_string(), + Self::B => "", + Self::KB => "K", + Self::MB => "M", + Self::GB => "G", + Self::TB => "T", + Self::PB => "P", } } diff --git a/src/notifications/notify_and_log.rs b/src/notifications/notify_and_log.rs index 8ed182bd..d004c517 100644 --- a/src/notifications/notify_and_log.rs +++ b/src/notifications/notify_and_log.rs @@ -18,7 +18,6 @@ use crate::utils::error_logger::{ErrorLogger, Location}; use crate::utils::formatted_strings::APP_VERSION; use crate::utils::formatted_strings::get_formatted_timestamp; use crate::{InfoTraffic, SNIFFNET_LOWERCASE, location}; -use std::cmp::min; use std::collections::{HashMap, HashSet}; use std::net::IpAddr; @@ -171,13 +170,8 @@ fn threshold_hosts( data_repr, ) }); - let n_entry = min(hosts.len(), 4); + hosts.truncate(4); hosts - .get(..n_entry) - .unwrap_or_default() - .to_owned() - .into_iter() - .collect() } fn threshold_services( @@ -191,13 +185,8 @@ fn threshold_services( .map(|(s, data_info_fav)| (*s, data_info_fav.data_info)) .collect(); services.sort_by(|(_, a), (_, b)| a.compare(b, SortType::Descending, data_repr)); - let n_entry = min(services.len(), 4); + services.truncate(4); services - .get(..n_entry) - .unwrap_or_default() - .to_owned() - .into_iter() - .collect() } fn favorites_last_interval( diff --git a/src/report/get_report_entries.rs b/src/report/get_report_entries.rs index b46a3166..81295b38 100644 --- a/src/report/get_report_entries.rs +++ b/src/report/get_report_entries.rs @@ -13,7 +13,11 @@ use crate::networking::types::info_address_port_pair::InfoAddressPortPair; /// with their packets, in-bytes, and out-bytes count pub fn get_searched_entries( sniffer: &Sniffer, -) -> (Vec<(AddressPortPair, InfoAddressPortPair)>, usize, DataInfo) { +) -> ( + Vec<(&AddressPortPair, &InfoAddressPortPair)>, + usize, + DataInfo, +) { let mut agglomerate = DataInfo::default(); let info_traffic = &sniffer.info_traffic; let mut all_results: Vec<(&AddressPortPair, &InfoAddressPortPair)> = info_traffic @@ -69,9 +73,7 @@ pub fn get_searched_entries( all_results .get((sniffer.page_number.saturating_sub(1)) * 30..upper_bound) .unwrap_or_default() - .iter() - .map(|&(key, val)| (key.to_owned(), val.to_owned())) - .collect(), + .to_vec(), all_results.len(), agglomerate, )