use std::fmt::{self, Display, Formatter};
use faststr::FastStr;
use crate::msg_impl;
#[deprecated(
since = "0.11.0",
note = "Please use the `ProtocolException` instead. This type will be removed in the next release."
)]
pub type ProtocolError = ProtocolException;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct ProtocolException {
kind: ProtocolExceptionKind,
message: FastStr,
}
impl ProtocolException {
pub fn new<S: Into<FastStr>>(kind: ProtocolExceptionKind, message: S) -> ProtocolException {
ProtocolException {
kind,
message: message.into(),
}
}
#[inline]
pub fn kind(&self) -> ProtocolExceptionKind {
self.kind
}
#[inline]
pub fn message(&self) -> &FastStr {
&self.message
}
msg_impl!();
}
impl Display for ProtocolException {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let error_text = match self.kind {
ProtocolExceptionKind::Unknown => "protocol error",
ProtocolExceptionKind::InvalidData => "bad data",
ProtocolExceptionKind::NegativeSize => "negative message size",
ProtocolExceptionKind::SizeLimit => "message too long",
ProtocolExceptionKind::BadVersion => "invalid thrift version",
ProtocolExceptionKind::NotImplemented => "not implemented",
ProtocolExceptionKind::DepthLimit => "maximum skip depth reached",
};
write!(f, "{}: {}", error_text, self.message)
}
}
impl std::error::Error for ProtocolException {}
#[deprecated(
since = "0.11.0",
note = "Please use the `ProtocolExceptionKind` instead. This type will be removed in the next release."
)]
pub type ProtocolErrorKind = ProtocolExceptionKind;
#[non_exhaustive]
#[derive(Clone, Copy, Eq, Debug, PartialEq)]
pub enum ProtocolExceptionKind {
Unknown,
InvalidData,
NegativeSize,
SizeLimit,
BadVersion,
NotImplemented,
DepthLimit,
}