From 4b16af29c707ace9fa0d38050fbe4e7e438dee42 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 29 Oct 2018 13:41:54 -0700 Subject: [PATCH] add Connect::parse() method --- src/connector.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/connector.rs b/src/connector.rs index 00a88c5f..e8fbcfd3 100644 --- a/src/connector.rs +++ b/src/connector.rs @@ -56,6 +56,22 @@ impl Connect { } } + /// split the string by ':' and convert the second part to u16 + pub fn parse>(host: T) -> Option { + let mut parts_iter = host.as_ref().splitn(2, ':'); + if let Some(host) = parts_iter.next() { + let port_str = parts_iter.next().unwrap_or(""); + if let Ok(port) = port_str.parse::() { + return Some(Connect { + port, + host: host.to_owned(), + timeout: Duration::from_secs(1), + }); + } + } + None + } + /// Set connect timeout /// /// By default timeout is set to a 1 second.