Skip to content

Commit 99f2a79

Browse files
committed
Prefer more cannonical match expression over multiple ifs
1 parent 94f550a commit 99f2a79

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/resolver/mod.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -422,49 +422,53 @@ async fn check_port(
422422
) -> (String, String) {
423423
let https_with_port = format!("https://{host}:{port}"); // Construct the HTTPS URL with the port
424424
let http_with_port = format!("http://{host}:{port}"); // Construct the HTTP URL with the port
425-
if port == 80 {
426-
let socket_address = SocketAddr::new(ip_addr.clone(), port); // Create a socket address using the IP address and port
425+
match port {
426+
80 => {
427+
let socket_address = SocketAddr::new(ip_addr.clone(), port); // Create a socket address using the IP address and port
427428

428-
// If the TCP connection is successful, print the HTTP URL with the port
429-
let ip = format!("http://{ip_addr}:{port}");
429+
// If the TCP connection is successful, print the HTTP URL with the port
430+
let ip = format!("http://{ip_addr}:{port}");
430431

431-
match tokio::time::timeout(timeout, TcpStream::connect(&socket_address)).await {
432-
Ok(Ok(_)) => {
433-
return (http_with_port, ip); // Return the HTTP URL and IP address
434-
}
435-
_ => {
436-
// If the TCP connection fails, return early
437-
return ("".to_string(), "".to_string()); // Return empty strings
432+
match tokio::time::timeout(timeout, TcpStream::connect(&socket_address)).await {
433+
Ok(Ok(_)) => {
434+
return (http_with_port, ip); // Return the HTTP URL and IP address
435+
}
436+
_ => {
437+
// If the TCP connection fails, return early
438+
return ("".to_string(), "".to_string()); // Return empty strings
439+
}
438440
}
439441
}
440-
} else if port == 443 {
441-
// If the TCP connection is successful, print the HTTPS URL with the port
442-
let ip = format!("https://{ip_addr}:{port}");
442+
443 => {
443+
// If the TCP connection is successful, print the HTTPS URL with the port
444+
let ip = format!("https://{ip_addr}:{port}");
443445

444-
let socket_address = SocketAddr::new(ip_addr.clone(), port); // Create a socket address using the IP address and port
446+
let socket_address = SocketAddr::new(ip_addr.clone(), port); // Create a socket address using the IP address and port
445447

446-
match tokio::time::timeout(timeout, TcpStream::connect(&socket_address)).await {
447-
Ok(Ok(_)) => {
448-
return (https_with_port, ip); // Return the HTTPS URL and IP address
449-
}
450-
_ => {
451-
// If the TCP connection fails, return early
452-
return ("".to_string(), "".to_string()); // Return empty strings
448+
match tokio::time::timeout(timeout, TcpStream::connect(&socket_address)).await {
449+
Ok(Ok(_)) => {
450+
return (https_with_port, ip); // Return the HTTPS URL and IP address
451+
}
452+
_ => {
453+
// If the TCP connection fails, return early
454+
return ("".to_string(), "".to_string()); // Return empty strings
455+
}
453456
}
454457
}
455-
} else {
456-
// If the TCP connection is successful, print the HTTPS URL with the port
457-
let ip = format!("https://{ip_addr}:{port}");
458+
_ => {
459+
// If the TCP connection is successful, print the HTTPS URL with the port
460+
let ip = format!("https://{ip_addr}:{port}");
458461

459-
let socket_address = SocketAddr::new(ip_addr.clone(), port); // Create a socket address using the IP address and port
462+
let socket_address = SocketAddr::new(ip_addr.clone(), port); // Create a socket address using the IP address and port
460463

461-
match tokio::time::timeout(timeout, TcpStream::connect(&socket_address)).await {
462-
Ok(Ok(_)) => {
463-
return (https_with_port, ip); // Return the HTTPS URL and IP address
464-
}
465-
_ => {
466-
// If the TCP connection fails, return early
467-
return ("".to_string(), "".to_string()); // Return empty strings
464+
match tokio::time::timeout(timeout, TcpStream::connect(&socket_address)).await {
465+
Ok(Ok(_)) => {
466+
return (https_with_port, ip); // Return the HTTPS URL and IP address
467+
}
468+
_ => {
469+
// If the TCP connection fails, return early
470+
return ("".to_string(), "".to_string()); // Return empty strings
471+
}
468472
}
469473
}
470474
}

0 commit comments

Comments
 (0)