summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcyqsimon <28627918+cyqsimon@users.noreply.github.com>2023-08-25 20:16:33 +0800
committercyqsimon <28627918+cyqsimon@users.noreply.github.com>2023-08-25 20:16:33 +0800
commit42ae443bd3c3eb8150a8b0a0a681bc8ab315214f (patch)
treec14fd72169bddd87133d4258d9a25c8701fedc1a
parentfb176a5212e2e1aba41e181648723e8048da2a2c (diff)
Use variable capture syntax in format macros where possible
-rw-r--r--build.rs12
-rw-r--r--src/display/components/display_bandwidth.rs17
-rw-r--r--src/display/components/header_details.rs2
-rw-r--r--src/display/components/table.rs4
-rw-r--r--src/display/ui.rs10
-rw-r--r--src/main.rs2
-rw-r--r--src/network/connection.rs3
-rw-r--r--src/os/shared.rs23
-rw-r--r--src/tests/cases/raw_mode.rs2
9 files changed, 32 insertions, 43 deletions
diff --git a/build.rs b/build.rs
index e844eeb..f306021 100644
--- a/build.rs
+++ b/build.rs
@@ -19,7 +19,7 @@ fn download_windows_pcap_sdk() {
let mut pcap_zip = Vec::new();
let res = request::get("https://npcap.com/dist/npcap-sdk-1.13.zip", &mut pcap_zip).unwrap();
- eprintln!("{:?}", res);
+ eprintln!("{res:?}");
let lib_dir = if cfg!(target_arch = "aarch64") {
"Lib/ARM64"
@@ -31,20 +31,20 @@ fn download_windows_pcap_sdk() {
panic!("Unsupported target!")
};
let lib_name = "Packet.lib";
- let lib_path = format!("{}/{}", lib_dir, lib_name);
+ let lib_path = format!("{lib_dir}/{lib_name}");
let mut archive = ZipArchive::new(io::Cursor::new(pcap_zip)).unwrap();
let mut pcap_lib = match archive.by_name(&lib_path) {
Ok(lib) => lib,
Err(err) => {
- panic!("{}", err);
+ panic!("{err}");
}
};
- fs::create_dir_all(format!("{}/{}", out_dir, lib_dir)).unwrap();
- let mut pcap_lib_file = fs::File::create(format!("{}/{}", out_dir, lib_path)).unwrap();
+ fs::create_dir_all(format!("{out_dir}/{lib_dir}")).unwrap();
+ let mut pcap_lib_file = fs::File::create(format!("{out_dir}/{lib_path}")).unwrap();
io::copy(&mut pcap_lib, &mut pcap_lib_file).unwrap();
pcap_lib_file.flush().unwrap();
- println!("cargo:rustc-link-search=native={}/{}", out_dir, lib_dir);
+ println!("cargo:rustc-link-search=native={out_dir}/{lib_dir}");
}
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::<String>();
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<u8>) -> String {
use regex::Regex;
let timestamp = Regex::new(r"<\d+>").unwrap();
let replaced = timestamp.replace_all(&stdout_utf8, "<TIMESTAMP_REMOVED>");
- format!("{}", replaced)
+ format!("{replaced}")
}
#[test]