diff --git a/tokio-xmpp/src/error.rs b/tokio-xmpp/src/error.rs index 0818dab8..82f7e748 100644 --- a/tokio-xmpp/src/error.rs +++ b/tokio-xmpp/src/error.rs @@ -5,6 +5,7 @@ use std::borrow::Cow; use std::error::Error as StdError; use std::fmt; use std::io::Error as IoError; +use std::str::Utf8Error; #[cfg(feature = "tls-rust")] use tokio_rustls::rustls::client::InvalidDnsNameError; #[cfg(feature = "tls-rust")] @@ -40,6 +41,10 @@ pub enum Error { Disconnected, /// Shoud never happen InvalidState, + /// Fmt error + Fmt(fmt::Error), + /// Utf8 error + Utf8(Utf8Error), } impl fmt::Display for Error { @@ -56,6 +61,8 @@ impl fmt::Display for Error { Error::DnsNameError(e) => write!(fmt, "DNS name error: {}", e), Error::Disconnected => write!(fmt, "disconnected"), Error::InvalidState => write!(fmt, "invalid state"), + Error::Fmt(e) => write!(fmt, "Fmt error: {}", e), + Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e), } } } @@ -98,6 +105,18 @@ impl From for Error { } } +impl From for Error { + fn from(e: fmt::Error) -> Self { + Error::Fmt(e) + } +} + +impl From for Error { + fn from(e: Utf8Error) -> Self { + Error::Utf8(e) + } +} + #[cfg(feature = "tls-rust")] impl From for Error { fn from(e: InvalidDnsNameError) -> Self { diff --git a/tokio-xmpp/src/xmpp_codec.rs b/tokio-xmpp/src/xmpp_codec.rs index 77a8a123..1e47ec69 100644 --- a/tokio-xmpp/src/xmpp_codec.rs +++ b/tokio-xmpp/src/xmpp_codec.rs @@ -114,7 +114,7 @@ impl Decoder for XMPPCodec { } impl Encoder for XMPPCodec { - type Error = io::Error; + type Error = Error; fn encode(&mut self, item: Packet, dst: &mut BytesMut) -> Result<(), Self::Error> { let remaining = dst.capacity() - dst.len(); @@ -139,24 +139,28 @@ impl Encoder for XMPPCodec { } write!(buf, ">\n").map_err(to_io_err)?; - debug!(">> {:?}", buf); - write!(dst, "{}", buf).map_err(to_io_err) + let utf8 = std::str::from_utf8(dst)?; + debug!(">> {}", utf8); + write!(dst, "{}", buf)? + } + Packet::Stanza(stanza) => { + let _ = stanza + .write_to(&mut WriteBytes::new(dst)) + .map_err(|e| to_io_err(format!("{}", e)))?; + let utf8 = std::str::from_utf8(dst)?; + debug!(">> {}", utf8); + } + Packet::Text(text) => { + let _ = write_text(&text, dst).map_err(to_io_err)?; + let utf8 = std::str::from_utf8(dst)?; + debug!(">> {}", utf8); + } + Packet::StreamEnd => { + let _ = write!(dst, "\n").map_err(to_io_err); } - Packet::Stanza(stanza) => stanza - .write_to(&mut WriteBytes::new(dst)) - .and_then(|_| { - debug!(">> {:?}", dst); - Ok(()) - }) - .map_err(|e| to_io_err(format!("{}", e))), - Packet::Text(text) => write_text(&text, dst) - .and_then(|_| { - debug!(">> {:?}", dst); - Ok(()) - }) - .map_err(to_io_err), - Packet::StreamEnd => write!(dst, "\n").map_err(to_io_err), } + + Ok(()) } }