client: switch SinkItem to Packet

this breaks backwards compatibility
This commit is contained in:
Astro 2019-01-29 01:52:07 +01:00
parent 1e7031aa31
commit 2654722fa3
3 changed files with 10 additions and 20 deletions

View file

@ -2,7 +2,7 @@ use futures::{future, Future, Sink, Stream};
use std::env::args; use std::env::args;
use std::process::exit; use std::process::exit;
use tokio::runtime::current_thread::Runtime; use tokio::runtime::current_thread::Runtime;
use tokio_xmpp::Client; use tokio_xmpp::{Client, Packet};
use xmpp_parsers::{Jid, Element, TryFrom}; use xmpp_parsers::{Jid, Element, TryFrom};
use xmpp_parsers::message::{Body, Message, MessageType}; use xmpp_parsers::message::{Body, Message, MessageType};
use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType}; use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType};
@ -34,7 +34,7 @@ fn main() {
let presence = make_presence(); let presence = make_presence();
let sink = sink_state.take().unwrap(); let sink = sink_state.take().unwrap();
sink_future = Some(Box::new(sink.send(presence))); sink_future = Some(Box::new(sink.send(Packet::Stanza(presence))));
} else if let Some(message) = event } else if let Some(message) = event
.into_stanza() .into_stanza()
.and_then(|stanza| Message::try_from(stanza).ok()) .and_then(|stanza| Message::try_from(stanza).ok())
@ -42,15 +42,15 @@ fn main() {
match (message.from, message.bodies.get("")) { match (message.from, message.bodies.get("")) {
(Some(ref from), Some(ref body)) if body.0 == "die" => { (Some(ref from), Some(ref body)) if body.0 == "die" => {
println!("Secret die command triggered by {}", from); println!("Secret die command triggered by {}", from);
let sink = sink_state.as_mut().unwrap(); let sink = sink_state.take().unwrap();
sink.close().expect("close"); sink_future = Some(Box::new(sink.send(Packet::StreamEnd)));
} }
(Some(ref from), Some(ref body)) => { (Some(ref from), Some(ref body)) => {
if message.type_ != MessageType::Error { if message.type_ != MessageType::Error {
// This is a message we'll echo // This is a message we'll echo
let reply = make_reply(from.clone(), &body.0); let reply = make_reply(from.clone(), &body.0);
let sink = sink_state.take().unwrap(); let sink = sink_state.take().unwrap();
sink_future = Some(Box::new(sink.send(reply))); sink_future = Some(Box::new(sink.send(Packet::Stanza(reply))));
} }
} }
_ => {} _ => {}

View file

@ -1,6 +1,6 @@
use futures::{done, Async, AsyncSink, Future, Poll, Sink, StartSend, Stream}; use futures::{done, Async, AsyncSink, Future, Poll, Sink, StartSend, Stream};
use idna; use idna;
use xmpp_parsers::{Jid, JidParseError, Element}; use xmpp_parsers::{Jid, JidParseError};
use sasl::common::{ChannelBinding, Credentials}; use sasl::common::{ChannelBinding, Credentials};
use std::mem::replace; use std::mem::replace;
use std::str::FromStr; use std::str::FromStr;
@ -193,24 +193,13 @@ impl Stream for Client {
} }
impl Sink for Client { impl Sink for Client {
type SinkItem = Element; type SinkItem = Packet;
type SinkError = Error; type SinkError = Error;
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> { fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
match self.state { match self.state {
ClientState::Connected(ref mut stream) => { ClientState::Connected(ref mut stream) =>
match stream.start_send(Packet::Stanza(item)) { Ok(stream.start_send(item)?),
Ok(AsyncSink::NotReady(Packet::Stanza(stanza))) =>
Ok(AsyncSink::NotReady(stanza)),
Ok(AsyncSink::NotReady(_)) => {
panic!("Client.start_send with stanza but got something else back")
}
Ok(AsyncSink::Ready) =>
Ok(AsyncSink::Ready),
Err(e) =>
Err(e)?,
}
}
_ => _ =>
Ok(AsyncSink::NotReady(item)), Ok(AsyncSink::NotReady(item)),
} }

View file

@ -8,6 +8,7 @@ extern crate derive_error;
mod starttls; mod starttls;
mod stream_start; mod stream_start;
pub mod xmpp_codec; pub mod xmpp_codec;
pub use crate::xmpp_codec::Packet;
pub mod xmpp_stream; pub mod xmpp_stream;
pub use crate::starttls::StartTlsClient; pub use crate::starttls::StartTlsClient;
mod event; mod event;