From 1098a8c6641abbc8d06888413795f3435b050eb0 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Sat, 13 Apr 2019 22:04:02 +0200 Subject: Use parse() instead of to_socket_addrs() --- src/main.rs | 68 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 33 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index fce9cf6..b85b8a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use actix_web::{fs, middleware, server, App}; use clap::crate_version; use simplelog::{Config, LevelFilter, TermLogger}; use std::io::{self, Write}; -use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs}; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::thread; use std::time::Duration; use yansi::{Color, Paint}; @@ -85,18 +85,22 @@ fn main() { let inside_config = miniserve_config.clone(); - let interfaces = miniserve_config.interfaces.iter().map(|&interface| { - if interface == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) { - // If the interface is 0.0.0.0, we'll change it to localhost so that clicking the link will - // also work on Windows. Why can't Windows interpret 0.0.0.0? - String::from("localhost") - } else if interface.is_ipv6() { - // If the interface is IPv6 then we'll print it with brackets so that it is clickable. - format!("[{}]", interface) - } else { - format!("{}", interface) - } - }); + let interfaces = miniserve_config + .interfaces + .iter() + .map(|&interface| { + if interface == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) { + // If the interface is 0.0.0.0, we'll change it to localhost so that clicking the link will + // also work on Windows. Why can't Windows interpret 0.0.0.0? + String::from("127.0.0.1") + } else if interface.is_ipv6() { + // If the interface is IPv6 then we'll print it with brackets so that it is clickable. + format!("[{}]", interface) + } else { + format!("{}", interface) + } + }) + .collect::>(); let canon_path = miniserve_config.path.canonicalize().unwrap(); let path_string = canon_path.to_string_lossy(); @@ -120,7 +124,7 @@ fn main() { } } let mut addresses = String::new(); - for interface in interfaces { + for interface in &interfaces { if !addresses.is_empty() { addresses.push_str(", "); } @@ -129,7 +133,7 @@ fn main() { Color::Green .paint(format!( "http://{interface}:{port}", - interface = interface, + interface = &interface, port = miniserve_config.port )) .bold() @@ -154,30 +158,28 @@ fn main() { ); println!("\nQuit by pressing CTRL-C"); + let socket_addresses = interfaces + .iter() + .map(|interface| { + format!( + "{interface}:{port}", + interface = &interface, + port = miniserve_config.port, + ) + .parse::() + }) + .collect::, _>>(); + + // Note that this should not fail, since CLI parsing succeeded: valid IpAddr + valid port should imply valid SocketAddr + let socket_addresses = socket_addresses.expect("Failed to parse string as socket address"); + server::new(move || { App::with_state(inside_config.clone()) .middleware(auth::Auth) .middleware(middleware::Logger::default()) .configure(configure_app) }) - .bind( - miniserve_config - .interfaces - .iter() - .map(|interface| { - format!( - "{interface}:{port}", - interface = &interface, - port = miniserve_config.port, - ) - .to_socket_addrs() - .unwrap() - .next() - .unwrap() - }) - .collect::>() - .as_slice(), - ) + .bind(socket_addresses.as_slice()) .expect("Couldn't bind server") .shutdown_timeout(0) .start(); -- cgit v1.2.3 From d12ce55bc8654f265cfad5516d32cf48694b7836 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Sat, 13 Apr 2019 23:33:30 +0200 Subject: Fixed comment --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index b85b8a4..0b4c77f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,7 +90,7 @@ fn main() { .iter() .map(|&interface| { if interface == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) { - // If the interface is 0.0.0.0, we'll change it to localhost so that clicking the link will + // If the interface is 0.0.0.0, we'll change it to 127.0.0.1 so that clicking the link will // also work on Windows. Why can't Windows interpret 0.0.0.0? String::from("127.0.0.1") } else if interface.is_ipv6() { -- cgit v1.2.3 From 79e99ad5037b68cc7285632a44afb758b41eca96 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Sat, 13 Apr 2019 23:54:37 +0200 Subject: Improved comment --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 0b4c77f..3daca07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -170,7 +170,9 @@ fn main() { }) .collect::, _>>(); - // Note that this should not fail, since CLI parsing succeeded: valid IpAddr + valid port should imply valid SocketAddr + // Note that this should never fail, since CLI parsing succeeded + // This means the format of the IP address is valid, and so is the port + // Valid IpAddr + valid port == valid SocketAddr let socket_addresses = socket_addresses.expect("Failed to parse string as socket address"); server::new(move || { -- cgit v1.2.3 From 731a6e011cf4efd7019f516febf1f06b5398fe41 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Mon, 15 Apr 2019 17:35:09 +0200 Subject: Use to_string() and fix comment --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 3daca07..a413f2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,7 +92,7 @@ fn main() { if interface == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) { // If the interface is 0.0.0.0, we'll change it to 127.0.0.1 so that clicking the link will // also work on Windows. Why can't Windows interpret 0.0.0.0? - String::from("127.0.0.1") + "127.0.0.1".to_string() } else if interface.is_ipv6() { // If the interface is IPv6 then we'll print it with brackets so that it is clickable. format!("[{}]", interface) @@ -171,7 +171,7 @@ fn main() { .collect::, _>>(); // Note that this should never fail, since CLI parsing succeeded - // This means the format of the IP address is valid, and so is the port + // This means the format of each IP address is valid, and so is the port // Valid IpAddr + valid port == valid SocketAddr let socket_addresses = socket_addresses.expect("Failed to parse string as socket address"); -- cgit v1.2.3