Run cargo fmt on the entire project.

This commit is contained in:
Emmanuel Gil Peyrot 2018-12-18 19:04:31 +01:00
commit 51bae9b0e5
14 changed files with 591 additions and 607 deletions

View file

@ -1,11 +1,11 @@
use futures::{sink, Async, Future, Poll, Stream};
use std::mem::replace;
use futures::{Future, Poll, Async, sink, Stream};
use tokio_io::{AsyncRead, AsyncWrite};
use xmpp_parsers::component::Handshake;
use crate::xmpp_codec::Packet;
use crate::xmpp_stream::XMPPStream;
use crate::{Error, AuthError};
use crate::{AuthError, Error};
const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept";
@ -29,7 +29,7 @@ impl<S: AsyncWrite> ComponentAuth<S> {
};
this.send(
stream,
Handshake::from_password_and_stream_id(&password, &sid)
Handshake::from_password_and_stream_id(&password, &sid),
);
Ok(this)
}
@ -50,45 +50,40 @@ impl<S: AsyncRead + AsyncWrite> Future for ComponentAuth<S> {
let state = replace(&mut self.state, ComponentAuthState::Invalid);
match state {
ComponentAuthState::WaitSend(mut send) =>
match send.poll() {
Ok(Async::Ready(stream)) => {
self.state = ComponentAuthState::WaitRecv(stream);
self.poll()
},
Ok(Async::NotReady) => {
self.state = ComponentAuthState::WaitSend(send);
Ok(Async::NotReady)
},
Err(e) =>
Err(e)?
},
ComponentAuthState::WaitRecv(mut stream) =>
match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) =>
{
self.state = ComponentAuthState::Invalid;
Ok(Async::Ready(stream))
},
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
if stanza.is("error", "http://etherx.jabber.org/streams") =>
{
Err(AuthError::ComponentFail.into())
},
Ok(Async::Ready(event)) => {
println!("ComponentAuth ignore {:?}", event);
Ok(Async::NotReady)
},
Ok(_) => {
self.state = ComponentAuthState::WaitRecv(stream);
Ok(Async::NotReady)
},
Err(e) =>
Err(e)?
},
ComponentAuthState::Invalid =>
unreachable!(),
ComponentAuthState::WaitSend(mut send) => match send.poll() {
Ok(Async::Ready(stream)) => {
self.state = ComponentAuthState::WaitRecv(stream);
self.poll()
}
Ok(Async::NotReady) => {
self.state = ComponentAuthState::WaitSend(send);
Ok(Async::NotReady)
}
Err(e) => Err(e)?,
},
ComponentAuthState::WaitRecv(mut stream) => match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) =>
{
self.state = ComponentAuthState::Invalid;
Ok(Async::Ready(stream))
}
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
if stanza.is("error", "http://etherx.jabber.org/streams") =>
{
Err(AuthError::ComponentFail.into())
}
Ok(Async::Ready(event)) => {
println!("ComponentAuth ignore {:?}", event);
Ok(Async::NotReady)
}
Ok(_) => {
self.state = ComponentAuthState::WaitRecv(stream);
Ok(Async::NotReady)
}
Err(e) => Err(e)?,
},
ComponentAuthState::Invalid => unreachable!(),
}
}
}

View file

@ -1,18 +1,18 @@
//! Components in XMPP are services/gateways that are logged into an
//! XMPP server under a JID consisting of just a domain name. They are
//! allowed to use any user and resource identifiers in their stanzas.
use futures::{done, Async, AsyncSink, Future, Poll, Sink, StartSend, Stream};
use jid::{Jid, JidParseError};
use minidom::Element;
use std::mem::replace;
use std::str::FromStr;
use tokio::net::TcpStream;
use tokio_io::{AsyncRead, AsyncWrite};
use futures::{Future, Stream, Poll, Async, Sink, StartSend, AsyncSink, done};
use minidom::Element;
use jid::{Jid, JidParseError};
use super::event::Event;
use super::happy_eyeballs::Connecter;
use super::xmpp_codec::Packet;
use super::xmpp_stream;
use super::happy_eyeballs::Connecter;
use super::event::Event;
use super::Error;
mod auth;
@ -31,7 +31,7 @@ const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept";
enum ComponentState {
Invalid,
Disconnected,
Connecting(Box<Future<Item=XMPPStream, Error=Error>>),
Connecting(Box<Future<Item = XMPPStream, Error = Error>>),
Connected(XMPPStream),
}
@ -50,19 +50,30 @@ impl Component {
})
}
fn make_connect(jid: Jid, password: String, server: &str, port: u16) -> impl Future<Item=XMPPStream, Error=Error> {
fn make_connect(
jid: Jid,
password: String,
server: &str,
port: u16,
) -> impl Future<Item = XMPPStream, Error = Error> {
let jid1 = jid.clone();
let password = password;
done(Connecter::from_lookup(server, None, port))
.flatten()
.and_then(move |tcp_stream| {
xmpp_stream::XMPPStream::start(tcp_stream, jid1, NS_JABBER_COMPONENT_ACCEPT.to_owned())
}).and_then(move |xmpp_stream| {
Self::auth(xmpp_stream, password).expect("auth")
xmpp_stream::XMPPStream::start(
tcp_stream,
jid1,
NS_JABBER_COMPONENT_ACCEPT.to_owned(),
)
})
.and_then(move |xmpp_stream| Self::auth(xmpp_stream, password).expect("auth"))
}
fn auth<S: AsyncRead + AsyncWrite>(stream: xmpp_stream::XMPPStream<S>, password: String) -> Result<ComponentAuth<S>, Error> {
fn auth<S: AsyncRead + AsyncWrite>(
stream: xmpp_stream::XMPPStream<S>,
password: String,
) -> Result<ComponentAuth<S>, Error> {
ComponentAuth::new(stream, password)
}
}
@ -75,31 +86,25 @@ impl Stream for Component {
let state = replace(&mut self.state, ComponentState::Invalid);
match state {
ComponentState::Invalid =>
Err(Error::InvalidState),
ComponentState::Disconnected =>
Ok(Async::Ready(None)),
ComponentState::Connecting(mut connect) => {
match connect.poll() {
Ok(Async::Ready(stream)) => {
self.state = ComponentState::Connected(stream);
Ok(Async::Ready(Some(Event::Online)))
},
Ok(Async::NotReady) => {
self.state = ComponentState::Connecting(connect);
Ok(Async::NotReady)
},
Err(e) =>
Err(e),
ComponentState::Invalid => Err(Error::InvalidState),
ComponentState::Disconnected => Ok(Async::Ready(None)),
ComponentState::Connecting(mut connect) => match connect.poll() {
Ok(Async::Ready(stream)) => {
self.state = ComponentState::Connected(stream);
Ok(Async::Ready(Some(Event::Online)))
}
Ok(Async::NotReady) => {
self.state = ComponentState::Connecting(connect);
Ok(Async::NotReady)
}
Err(e) => Err(e),
},
ComponentState::Connected(mut stream) => {
// Poll sink
match stream.poll_complete() {
Ok(Async::NotReady) => (),
Ok(Async::Ready(())) => (),
Err(e) =>
return Err(e)?,
Err(e) => return Err(e)?,
};
// Poll stream
@ -107,24 +112,23 @@ impl Stream for Component {
Ok(Async::NotReady) => {
self.state = ComponentState::Connected(stream);
Ok(Async::NotReady)
},
}
Ok(Async::Ready(None)) => {
// EOF
self.state = ComponentState::Disconnected;
Ok(Async::Ready(Some(Event::Disconnected)))
},
}
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
self.state = ComponentState::Connected(stream);
Ok(Async::Ready(Some(Event::Stanza(stanza))))
},
}
Ok(Async::Ready(_)) => {
self.state = ComponentState::Connected(stream);
Ok(Async::NotReady)
},
Err(e) =>
Err(e)?,
}
Err(e) => Err(e)?,
}
},
}
}
}
}
@ -135,30 +139,26 @@ impl Sink for Component {
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
match self.state {
ComponentState::Connected(ref mut stream) =>
match stream.start_send(Packet::Stanza(item)) {
Ok(AsyncSink::NotReady(Packet::Stanza(stanza))) =>
Ok(AsyncSink::NotReady(stanza)),
Ok(AsyncSink::NotReady(_)) =>
panic!("Component.start_send with stanza but got something else back"),
Ok(AsyncSink::Ready) => {
Ok(AsyncSink::Ready)
},
Err(e) =>
Err(e)?,
},
_ =>
Ok(AsyncSink::NotReady(item)),
ComponentState::Connected(ref mut stream) => match stream
.start_send(Packet::Stanza(item))
{
Ok(AsyncSink::NotReady(Packet::Stanza(stanza))) => Ok(AsyncSink::NotReady(stanza)),
Ok(AsyncSink::NotReady(_)) => {
panic!("Component.start_send with stanza but got something else back")
}
Ok(AsyncSink::Ready) => Ok(AsyncSink::Ready),
Err(e) => Err(e)?,
},
_ => Ok(AsyncSink::NotReady(item)),
}
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
match &mut self.state {
&mut ComponentState::Connected(ref mut stream) =>
stream.poll_complete()
.map_err(|e| e.into()),
_ =>
Ok(Async::Ready(())),
&mut ComponentState::Connected(ref mut stream) => {
stream.poll_complete().map_err(|e| e.into())
}
_ => Ok(Async::Ready(())),
}
}
}