From 6816b922f0689d6a9f2e560c9492042e1ae773e2 Mon Sep 17 00:00:00 2001 From: Miquel Ruiz Date: Sun, 5 Jun 2016 12:47:46 +0100 Subject: [PATCH] Try connection instead of unwrapping Return an error instead of panicking on connection errors, which allows to handle the problem from the caller. fixes #12 --- src/client.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index a1206ac..01f4abd 100644 --- a/src/client.rs +++ b/src/client.rs @@ -31,7 +31,7 @@ impl IMAPStream { pub fn connect>(host: S, port: u16, ssl_context: Option) -> Result { let host_string = host.into(); let connect_string = format!("{}:{}", host_string, port); - let tcp_stream = TcpStream::connect(&*connect_string).unwrap(); + let tcp_stream = try!(TcpStream::connect(&*connect_string)); let mut socket = match ssl_context { Some(context) => IMAPStream { stream: IMAPStreamTypes::Ssl(SslStream::connect(&context, tcp_stream).unwrap()), host: host_string, port: port, tag: 1, tag_prefix: "a"}, None => IMAPStream { stream: IMAPStreamTypes::Basic(tcp_stream), host: host_string, port: port, tag: 1, tag_prefix: "a"}, @@ -334,3 +334,9 @@ impl IMAPStream { return command; } } + +#[test] +fn connect() { + let imap = IMAPStream::connect("this-is-not-an-imap-server", 143, None); + assert!(imap.is_err()); +}