use std::{
fmt::{self, Display, Formatter},
io,
};
use faststr::FastStr;
use crate::msg_impl;
#[deprecated(
since = "0.11.0",
note = "Please use the `TransportException` instead. This type will be removed in the next release."
)]
pub type TransportError = TransportException;
#[derive(Debug)]
pub struct TransportException {
io_error: io::Error,
message: FastStr,
}
impl PartialEq for TransportException {
fn eq(&self, other: &Self) -> bool {
self.io_error.kind() == other.io_error.kind()
}
}
impl Eq for TransportException {}
impl TransportException {
#[inline]
pub fn io_error(&self) -> &io::Error {
&self.io_error
}
#[inline]
pub fn message(&self) -> &FastStr {
&self.message
}
#[inline]
pub fn kind(&self) -> io::ErrorKind {
self.io_error.kind()
}
msg_impl!();
}
impl From<io::Error> for TransportException {
#[inline]
fn from(err: io::Error) -> Self {
let message = FastStr::from_string(err.to_string());
TransportException {
io_error: err,
message,
}
}
}
impl Display for TransportException {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.kind(), self.message())
}
}
impl std::error::Error for TransportException {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.io_error)
}
}
impl Clone for TransportException {
fn clone(&self) -> Self {
Self {
io_error: io::Error::new(self.io_error().kind(), self.io_error().to_string()),
message: self.message.clone(),
}
}
}