mod application;
mod macros;
mod protocol;
mod transport;
use std::{
fmt::{self, Debug, Display, Formatter},
string,
};
pub use application::*;
use faststr::FastStr;
pub use protocol::*;
pub use transport::*;
#[derive(Debug, Clone)]
pub enum ThriftException {
Application(ApplicationException),
Protocol(ProtocolException),
Transport(TransportException),
}
impl From<ApplicationException> for ThriftException {
fn from(e: ApplicationException) -> Self {
ThriftException::Application(e)
}
}
impl From<TransportException> for ThriftException {
fn from(e: TransportException) -> Self {
ThriftException::Transport(e)
}
}
impl From<std::io::Error> for ThriftException {
fn from(e: std::io::Error) -> Self {
ThriftException::Transport(TransportException::from(e))
}
}
impl From<ProtocolException> for ThriftException {
fn from(e: ProtocolException) -> Self {
ThriftException::Protocol(e)
}
}
impl From<string::FromUtf8Error> for ThriftException {
fn from(err: string::FromUtf8Error) -> Self {
ThriftException::Protocol(ProtocolException::new(
ProtocolExceptionKind::InvalidData,
format!("{err:?}"),
))
}
}
impl Display for ThriftException {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
impl std::error::Error for ThriftException {}
impl ThriftException {
pub fn message(&self) -> &FastStr {
match self {
ThriftException::Application(e) => e.message(),
ThriftException::Protocol(e) => e.message(),
ThriftException::Transport(e) => e.message(),
}
}
pub fn append_msg(&mut self, message: &str) {
match self {
ThriftException::Application(e) => e.append_msg(message),
ThriftException::Protocol(e) => e.append_msg(message),
ThriftException::Transport(e) => e.append_msg(message),
}
}
pub fn prepend_msg(&mut self, message: &str) {
match self {
ThriftException::Application(e) => e.prepend_msg(message),
ThriftException::Protocol(e) => e.prepend_msg(message),
ThriftException::Transport(e) => e.prepend_msg(message),
}
}
}
pub fn new_application_exception<S: Into<FastStr>>(
kind: ApplicationExceptionKind,
message: S,
) -> ThriftException {
ThriftException::Application(ApplicationException::new(kind, message))
}
pub fn new_protocol_exception<S: Into<FastStr>>(
kind: ProtocolExceptionKind,
message: S,
) -> ThriftException {
ThriftException::Protocol(ProtocolException::new(kind, message))
}
#[deprecated(
since = "0.11.0",
note = "Please use `new_protocol_exception` instead. This function will be removed in the next release."
)]
pub fn new_protocol_error<S: Into<FastStr>>(
kind: ProtocolExceptionKind,
message: S,
) -> ThriftException {
new_protocol_exception(kind, message)
}