1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-12 03:04:03 +02:00

tokio-xmpp: cleaner outgoing debug logs

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-05-30 17:37:52 +02:00
parent 5256575512
commit 1870a83424
2 changed files with 40 additions and 17 deletions

View File

@ -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<TlsError> for Error {
}
}
impl From<fmt::Error> for Error {
fn from(e: fmt::Error) -> Self {
Error::Fmt(e)
}
}
impl From<Utf8Error> for Error {
fn from(e: Utf8Error) -> Self {
Error::Utf8(e)
}
}
#[cfg(feature = "tls-rust")]
impl From<InvalidDnsNameError> for Error {
fn from(e: InvalidDnsNameError) -> Self {

View File

@ -114,7 +114,7 @@ impl Decoder for XMPPCodec {
}
impl Encoder<Packet> 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<Packet> 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, "</stream:stream>\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, "</stream:stream>\n").map_err(to_io_err),
}
Ok(())
}
}