diff --git a/src/client/auth.rs b/src/client/auth.rs index 97a6a12..55dfe2d 100644 --- a/src/client/auth.rs +++ b/src/client/auth.rs @@ -40,33 +40,26 @@ impl ClientAuth { ]; let mech_names: Vec = - match stream.stream_features.get_child("mechanisms", NS_XMPP_SASL) { - None => - return Err(AuthError::NoMechanism.into()), - Some(mechs) => - mechs.children() - .filter(|child| child.is("mechanism", NS_XMPP_SASL)) - .map(|mech_el| mech_el.text()) - .collect(), - }; + stream.stream_features.get_child("mechanisms", NS_XMPP_SASL) + .ok_or(AuthError::NoMechanism)? + .children() + .filter(|child| child.is("mechanism", NS_XMPP_SASL)) + .map(|mech_el| mech_el.text()) + .collect(); // println!("SASL mechanisms offered: {:?}", mech_names); for mut mech in mechs { let name = mech.name().to_owned(); if mech_names.iter().any(|name1| *name1 == name) { // println!("SASL mechanism selected: {:?}", name); - let initial = match mech.initial() { - Ok(initial) => initial, - Err(e) => return Err(AuthError::Sasl(e).into()), - }; + let initial = mech.initial() + .map_err(AuthError::Sasl)?; let mut this = ClientAuth { state: ClientAuthState::Invalid, mechanism: mech, }; - let mechanism = match XMPPMechanism::from_str(&name) { - Ok(mechanism) => mechanism, - Err(e) => return Err(ProtocolError::Parsers(e).into()), - }; + let mechanism = XMPPMechanism::from_str(&name) + .map_err(ProtocolError::Parsers)?; this.send( stream, Auth { @@ -78,7 +71,7 @@ impl ClientAuth { } } - Err(AuthError::NoMechanism.into()) + Err(AuthError::NoMechanism)? } fn send>(&mut self, stream: XMPPStream, nonza: N) { @@ -107,7 +100,7 @@ impl Future for ClientAuth { Ok(Async::NotReady) }, Err(e) => - Err(e.into()), + Err(e)?, }, ClientAuthState::WaitRecv(mut stream) => match stream.poll() { @@ -122,7 +115,7 @@ impl Future for ClientAuth { self.state = ClientAuthState::Start(start); self.poll() } else if let Ok(failure) = Failure::try_from(stanza) { - Err(AuthError::Fail(failure.defined_condition).into()) + Err(AuthError::Fail(failure.defined_condition))? } else { Ok(Async::NotReady) } @@ -136,7 +129,7 @@ impl Future for ClientAuth { Ok(Async::NotReady) }, Err(e) => - Err(ProtocolError::Parser(e).into()) + Err(ProtocolError::Parser(e))? }, ClientAuthState::Start(mut start) => match start.poll() { @@ -147,7 +140,7 @@ impl Future for ClientAuth { Ok(Async::NotReady) }, Err(e) => - Err(e.into()) + Err(e) }, ClientAuthState::Invalid => unreachable!(), diff --git a/src/client/bind.rs b/src/client/bind.rs index 974860b..b0268e6 100644 --- a/src/client/bind.rs +++ b/src/client/bind.rs @@ -61,7 +61,7 @@ impl Future for ClientBind { Ok(Async::NotReady) }, Err(e) => - Err(e.into()) + Err(e)? } }, ClientBind::WaitRecv(mut stream) => { @@ -80,7 +80,7 @@ impl Future for ClientBind { Ok(Async::Ready(stream)) }, _ => - Err(ProtocolError::InvalidBindResponse.into()), + Err(ProtocolError::InvalidBindResponse)?, } } else { Ok(Async::NotReady) @@ -96,7 +96,7 @@ impl Future for ClientBind { Ok(Async::NotReady) }, Err(e) => - Err(e.into()), + Err(e)?, } }, ClientBind::Invalid => diff --git a/src/client/mod.rs b/src/client/mod.rs index e3a3652..6c3226c 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -147,7 +147,7 @@ impl Stream for Client { Ok(Async::NotReady) => (), Ok(Async::Ready(())) => (), Err(e) => - return Err(e.into()), + return Err(e)?, }; // Poll stream @@ -167,7 +167,7 @@ impl Stream for Client { Ok(Async::NotReady) }, Err(e) => - Err(e.into()), + Err(e)?, } }, } @@ -190,7 +190,7 @@ impl Sink for Client { Ok(AsyncSink::Ready) }, Err(e) => - Err(e.into()), + Err(e)?, }, _ => Ok(AsyncSink::NotReady(item)), diff --git a/src/component/auth.rs b/src/component/auth.rs index 3b66511..b108ef3 100644 --- a/src/component/auth.rs +++ b/src/component/auth.rs @@ -31,7 +31,7 @@ impl ComponentAuth { stream, Handshake::from_password_and_stream_id(&password, &sid) ); - return Ok(this); + Ok(this) } fn send(&mut self, stream: XMPPStream, handshake: Handshake) { @@ -61,7 +61,7 @@ impl Future for ComponentAuth { Ok(Async::NotReady) }, Err(e) => - Err(e.into()), + Err(e)? }, ComponentAuthState::WaitRecv(mut stream) => match stream.poll() { @@ -85,7 +85,7 @@ impl Future for ComponentAuth { Ok(Async::NotReady) }, Err(e) => - Err(e.into()), + Err(e)? }, ComponentAuthState::Invalid => unreachable!(), diff --git a/src/component/mod.rs b/src/component/mod.rs index 150ffd9..088d20d 100644 --- a/src/component/mod.rs +++ b/src/component/mod.rs @@ -100,7 +100,7 @@ impl Stream for Component { Ok(Async::NotReady) => (), Ok(Async::Ready(())) => (), Err(e) => - return Err(e.into()), + return Err(e)?, }; // Poll stream @@ -123,7 +123,7 @@ impl Stream for Component { Ok(Async::NotReady) }, Err(e) => - Err(e.into()), + Err(e)?, } }, } @@ -146,7 +146,7 @@ impl Sink for Component { Ok(AsyncSink::Ready) }, Err(e) => - Err(e.into()), + Err(e)?, }, _ => Ok(AsyncSink::NotReady(item)), diff --git a/src/happy_eyeballs.rs b/src/happy_eyeballs.rs index 0a5fd23..e3bebe9 100644 --- a/src/happy_eyeballs.rs +++ b/src/happy_eyeballs.rs @@ -50,13 +50,11 @@ impl Future for Connecter { fn poll(&mut self) -> Poll { if self.resolver_opt.is_none() { //println!("Poll resolver future"); - match self.resolver_future.poll() { - Ok(Async::Ready(resolver)) => + match self.resolver_future.poll()? { + Async::Ready(resolver) => self.resolver_opt = Some(resolver), - Ok(Async::NotReady) => + Async::NotReady => return Ok(Async::NotReady), - Err(e) => - return Err(e.into()), } } diff --git a/src/stream_start.rs b/src/stream_start.rs index 8871840..ce9db27 100644 --- a/src/stream_start.rs +++ b/src/stream_start.rs @@ -1,6 +1,4 @@ use std::mem::replace; -// use std::error::Error as StdError; -// use std::{fmt, io}; use futures::{Future, Async, Poll, Stream, sink, Sink}; use tokio_io::{AsyncRead, AsyncWrite}; use tokio_codec::Framed; @@ -66,22 +64,18 @@ impl Future for StreamStart { StreamStartState::RecvStart(mut stream) => match stream.poll() { Ok(Async::Ready(Some(Packet::StreamStart(stream_attrs)))) => { - let stream_ns = match stream_attrs.get("xmlns") { - Some(ns) => ns.clone(), - None => - return Err(ProtocolError::NoStreamNamespace.into()), - }; + let stream_ns = stream_attrs.get("xmlns") + .ok_or(ProtocolError::NoStreamNamespace)? + .clone(); if self.ns == "jabber:client" { retry = true; // TODO: skip RecvFeatures for version < 1.0 (StreamStartState::RecvFeatures(stream, stream_ns), Ok(Async::NotReady)) } else { - let id = match stream_attrs.get("id") { - Some(id) => id.clone(), - None => - return Err(ProtocolError::NoStreamId.into()), - }; - // FIXME: huge hack, shouldn’t be an element! + let id = stream_attrs.get("id") + .ok_or(ProtocolError::NoStreamId)? + .clone(); + // FIXME: huge hack, shouldn’t be an element! let stream = XMPPStream::new(self.jid.clone(), stream, self.ns.clone(), Element::builder(id).build()); (StreamStartState::Invalid, Ok(Async::Ready(stream))) } @@ -119,59 +113,3 @@ impl Future for StreamStart { } } } - -// #[derive(Debug)] -// pub enum StreamStartError { -// MissingStreamNs, -// MissingStreamId, -// Unexpected, -// Parser(ParserError), -// IO(io::Error), -// } - -// impl From for StreamStartError { -// fn from(e: io::Error) -> Self { -// StreamStartError::IO(e) -// } -// } - -// impl From for StreamStartError { -// fn from(e: ParserError) -> Self { -// match e { -// ParserError::IO(e) => StreamStartError::IO(e), -// _ => StreamStartError::Parser(e) -// } -// } -// } - -// impl StdError for StreamStartError { -// fn description(&self) -> &str { -// match *self { -// StreamStartError::MissingStreamNs => "Missing stream namespace", -// StreamStartError::MissingStreamId => "Missing stream id", -// StreamStartError::Unexpected => "Unexpected", -// StreamStartError::Parser(ref pe) => pe.description(), -// StreamStartError::IO(ref ie) => ie.description(), -// } -// } - -// fn cause(&self) -> Option<&StdError> { -// match *self { -// StreamStartError::Parser(ref pe) => pe.cause(), -// StreamStartError::IO(ref ie) => ie.cause(), -// _ => None, -// } -// } -// } - -// impl fmt::Display for StreamStartError { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// match *self { -// StreamStartError::MissingStreamNs => write!(f, "Missing stream namespace"), -// StreamStartError::MissingStreamId => write!(f, "Missing stream id"), -// StreamStartError::Unexpected => write!(f, "Received unexpected data"), -// StreamStartError::Parser(ref pe) => write!(f, "{}", pe), -// StreamStartError::IO(ref ie) => write!(f, "{}", ie), -// } -// } -// }