From 42ae443bd3c3eb8150a8b0a0a681bc8ab315214f Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Fri, 25 Aug 2023 20:16:33 +0800 Subject: Use variable capture syntax in format macros where possible --- src/display/components/display_bandwidth.rs | 17 +++++++---------- src/display/components/header_details.rs | 2 +- src/display/components/table.rs | 4 ++-- src/display/ui.rs | 10 +++------- src/main.rs | 2 +- src/network/connection.rs | 3 +-- src/os/shared.rs | 23 ++++++++++------------- src/tests/cases/raw_mode.rs | 2 +- 8 files changed, 26 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/display/components/display_bandwidth.rs b/src/display/components/display_bandwidth.rs index 779e419..d34ffe1 100644 --- a/src/display/components/display_bandwidth.rs +++ b/src/display/components/display_bandwidth.rs @@ -10,20 +10,17 @@ impl fmt::Display for DisplayBandwidth { let suffix = if self.as_rate { "ps" } else { "" }; if self.bandwidth > 999_999_999_999.0 { // 1024 * 1024 * 1024 * 1024 - write!( - f, - "{:.2}TiB{}", - self.bandwidth / 1_099_511_627_776.0, - suffix - ) + write!(f, "{:.2}TiB{suffix}", self.bandwidth / 1_099_511_627_776.0,) } else if self.bandwidth > 999_999_999.0 { - write!(f, "{:.2}GiB{}", self.bandwidth / 1_073_741_824.0, suffix) // 1024 * 1024 * 1024 + // 1024 * 1024 * 1024 + write!(f, "{:.2}GiB{suffix}", self.bandwidth / 1_073_741_824.0) } else if self.bandwidth > 999_999.0 { - write!(f, "{:.2}MiB{}", self.bandwidth / 1_048_576.0, suffix) // 1024 * 1024 + // 1024 * 1024 + write!(f, "{:.2}MiB{suffix}", self.bandwidth / 1_048_576.0) } else if self.bandwidth > 999.0 { - write!(f, "{:.2}KiB{}", self.bandwidth / 1024.0, suffix) + write!(f, "{:.2}KiB{suffix}", self.bandwidth / 1024.0) } else { - write!(f, "{}B{}", self.bandwidth, suffix) + write!(f, "{}B{suffix}", self.bandwidth) } } } diff --git a/src/display/components/header_details.rs b/src/display/components/header_details.rs index 937a03b..2c32a18 100644 --- a/src/display/components/header_details.rs +++ b/src/display/components/header_details.rs @@ -102,7 +102,7 @@ impl<'a> HeaderDetails<'a> { match self.elapsed_time.as_secs() / SECONDS_IN_DAY { 0 => "".to_string(), 1 => "1 day, ".to_string(), - n => format!("{} days, ", n), + n => format!("{n} days, "), } } diff --git a/src/display/components/table.rs b/src/display/components/table.rs index a000e91..70e1b51 100644 --- a/src/display/components/table.rs +++ b/src/display/components/table.rs @@ -84,9 +84,9 @@ fn truncate_middle(row: &str, max_length: u16) -> String { .rev() .collect::(); if max_length % 2 == 0 { - format!("{}[...]{}", first_slice, second_slice) + format!("{first_slice}[...]{second_slice}") } else { - format!("{}[..]{}", first_slice, second_slice) + format!("{first_slice}[..]{second_slice}") } } else { row.to_string() diff --git a/src/display/ui.rs b/src/display/ui.rs index 54811b8..f70633f 100644 --- a/src/display/ui.rs +++ b/src/display/ui.rs @@ -52,9 +52,7 @@ where no_traffic: &mut bool| { for (process, process_network_data) in &state.processes { write_to_stdout(format!( - "process: <{}> \"{}\" up/down Bps: {}/{} connections: {}", - timestamp, - process, + "process: <{timestamp}> \"{process}\" up/down Bps: {}/{} connections: {}", process_network_data.total_bytes_uploaded, process_network_data.total_bytes_downloaded, process_network_data.connection_count @@ -67,8 +65,7 @@ where |write_to_stdout: &mut (dyn FnMut(String) + Send), no_traffic: &mut bool| { for (connection, connection_network_data) in &state.connections { write_to_stdout(format!( - "connection: <{}> {} up/down Bps: {}/{} process: \"{}\"", - timestamp, + "connection: <{timestamp}> {} up/down Bps: {}/{} process: \"{}\"", display_connection_string( connection, ip_to_host, @@ -86,8 +83,7 @@ where no_traffic: &mut bool| { for (remote_address, remote_address_network_data) in &state.remote_addresses { write_to_stdout(format!( - "remote_address: <{}> {} up/down Bps: {}/{} connections: {}", - timestamp, + "remote_address: <{timestamp}> {} up/down Bps: {}/{} connections: {}", display_ip_or_host(*remote_address, ip_to_host), remote_address_network_data.total_bytes_uploaded, remote_address_network_data.total_bytes_downloaded, diff --git a/src/main.rs b/src/main.rs index 53b3e7b..6361d11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,7 @@ pub struct RenderOpts { fn main() { if let Err(err) = try_main() { - eprintln!("Error: {}", err); + eprintln!("Error: {err}"); process::exit(2); } } diff --git a/src/network/connection.rs b/src/network/connection.rs index 780eafa..768bcb7 100644 --- a/src/network/connection.rs +++ b/src/network/connection.rs @@ -62,8 +62,7 @@ pub fn display_connection_string( interface_name: &str, ) -> String { format!( - "<{}>:{} => {}:{} ({})", - interface_name, + "<{interface_name}>:{} => {}:{} ({})", connection.local_socket.port, display_ip_or_host(connection.remote_socket.ip, ip_to_host), connection.remote_socket.port, diff --git a/src/os/shared.rs b/src/os/shared.rs index ee3415a..71e491c 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -49,8 +49,8 @@ pub(crate) fn get_datalink_channel( interface.name.to_owned(), )), _ => Err(GetInterfaceError::OtherError(format!( - "{}: {}", - &interface.name, e + "{}: {e}", + &interface.name ))), }, } @@ -97,7 +97,7 @@ where GetInterfaceError::PermissionError(interface_name) => { if let Some(prev_interface) = acc.permission { return UserErrors { - permission: Some(format!("{}, {}", prev_interface, interface_name)), + permission: Some(format!("{prev_interface}, {interface_name}")), ..acc }; } else { @@ -110,12 +110,12 @@ where error => { if let Some(prev_errors) = acc.other { return UserErrors { - other: Some(format!("{} \n {}", prev_errors, error)), + other: Some(format!("{prev_errors} \n {error}")), ..acc }; } else { return UserErrors { - other: Some(format!("{}", error)), + other: Some(format!("{error}")), ..acc }; } @@ -128,19 +128,17 @@ where if let Some(interface_name) = errors.permission { if let Some(other_errors) = errors.other { format!( - "\n\n{}: {} \nAdditional Errors: \n {}", - interface_name, + "\n\n{interface_name}: {} \nAdditional Errors: \n {other_errors}", eperm_message(), - other_errors ) } else { - format!("\n\n{}: {}", interface_name, eperm_message()) + format!("\n\n{interface_name}: {}", eperm_message()) } } else { let other_errors = errors .other .expect("asked to collect errors but found no errors"); - format!("\n\n {}", other_errors) + format!("\n\n {other_errors}") } } @@ -153,7 +151,7 @@ pub fn get_input( match get_interface(name) { Some(interface) => vec![interface], None => { - anyhow::bail!("Cannot find interface {}", name); + anyhow::bail!("Cannot find interface {name}"); // the homebrew formula relies on this wording, please be careful when changing } } @@ -205,8 +203,7 @@ pub fn get_input( let resolver = match runtime.block_on(dns::Resolver::new(dns_server)) { Ok(resolver) => resolver, Err(err) => anyhow::bail!( - "Could not initialize the DNS resolver. Are you offline?\n\nReason: {:?}", - err + "Could not initialize the DNS resolver. Are you offline?\n\nReason: {err:?}" ), }; let dns_client = dns::Client::new(resolver, runtime)?; diff --git a/src/tests/cases/raw_mode.rs b/src/tests/cases/raw_mode.rs index c382a2b..c05596e 100644 --- a/src/tests/cases/raw_mode.rs +++ b/src/tests/cases/raw_mode.rs @@ -42,7 +42,7 @@ fn format_raw_output(output: Vec) -> String { use regex::Regex; let timestamp = Regex::new(r"<\d+>").unwrap(); let replaced = timestamp.replace_all(&stdout_utf8, ""); - format!("{}", replaced) + format!("{replaced}") } #[test] -- cgit v1.2.3