improve style

Astro created

Change summary

src/client/auth.rs    | 37 ++++++++-------------
src/client/bind.rs    |  6 +-
src/client/mod.rs     |  6 +-
src/component/auth.rs |  6 +-
src/component/mod.rs  |  6 +-
src/happy_eyeballs.rs |  8 +--
src/stream_start.rs   | 76 ++++----------------------------------------
7 files changed, 37 insertions(+), 108 deletions(-)

Detailed changes

src/client/auth.rs 🔗

@@ -40,33 +40,26 @@ impl<S: AsyncWrite> ClientAuth<S> {
         ];
 
         let mech_names: Vec<String> =
-            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<S: AsyncWrite> ClientAuth<S> {
             }
         }
 
-        Err(AuthError::NoMechanism.into())
+        Err(AuthError::NoMechanism)?
     }
 
     fn send<N: Into<Element>>(&mut self, stream: XMPPStream<S>, nonza: N) {
@@ -107,7 +100,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientAuth<S> {
                         Ok(Async::NotReady)
                     },
                     Err(e) =>
-                        Err(e.into()),
+                        Err(e)?,
                 },
             ClientAuthState::WaitRecv(mut stream) =>
                 match stream.poll() {
@@ -122,7 +115,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientAuth<S> {
                             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<S: AsyncRead + AsyncWrite> Future for ClientAuth<S> {
                         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<S: AsyncRead + AsyncWrite> Future for ClientAuth<S> {
                         Ok(Async::NotReady)
                     },
                     Err(e) =>
-                        Err(e.into())
+                        Err(e)
                 },
             ClientAuthState::Invalid =>
                 unreachable!(),

src/client/bind.rs 🔗

@@ -61,7 +61,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientBind<S> {
                         Ok(Async::NotReady)
                     },
                     Err(e) =>
-                        Err(e.into())
+                        Err(e)?
                 }
             },
             ClientBind::WaitRecv(mut stream) => {
@@ -80,7 +80,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientBind<S> {
                                         Ok(Async::Ready(stream))
                                     },
                                     _ =>
-                                        Err(ProtocolError::InvalidBindResponse.into()),
+                                        Err(ProtocolError::InvalidBindResponse)?,
                                 }
                             } else {
                                 Ok(Async::NotReady)
@@ -96,7 +96,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientBind<S> {
                         Ok(Async::NotReady)
                     },
                     Err(e) =>
-                        Err(e.into()),
+                        Err(e)?,
                 }
             },
             ClientBind::Invalid =>

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)),

src/component/auth.rs 🔗

@@ -31,7 +31,7 @@ impl<S: AsyncWrite> ComponentAuth<S> {
             stream,
             Handshake::from_password_and_stream_id(&password, &sid)
         );
-        return Ok(this);
+        Ok(this)
     }
 
     fn send(&mut self, stream: XMPPStream<S>, handshake: Handshake) {
@@ -61,7 +61,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ComponentAuth<S> {
                         Ok(Async::NotReady)
                     },
                     Err(e) =>
-                        Err(e.into()),
+                        Err(e)?
                 },
             ComponentAuthState::WaitRecv(mut stream) =>
                 match stream.poll() {
@@ -85,7 +85,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ComponentAuth<S> {
                         Ok(Async::NotReady)
                     },
                     Err(e) =>
-                        Err(e.into()),
+                        Err(e)?
                 },
             ComponentAuthState::Invalid =>
                 unreachable!(),

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)),

src/happy_eyeballs.rs 🔗

@@ -50,13 +50,11 @@ impl Future for Connecter {
     fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
         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()),
             }
         }
 

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<S: AsyncRead + AsyncWrite> Future for StreamStart<S> {
             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<S: AsyncRead + AsyncWrite> Future for StreamStart<S> {
         }
     }
 }
-
-// #[derive(Debug)]
-// pub enum StreamStartError {
-//     MissingStreamNs,
-//     MissingStreamId,
-//     Unexpected,
-//     Parser(ParserError),
-//     IO(io::Error),
-// }
-
-// impl From<io::Error> for StreamStartError {
-//     fn from(e: io::Error) -> Self {
-//         StreamStartError::IO(e)
-//     }
-// }
-
-// impl From<ParserError> 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),
-//         }
-//     }
-// }