use std::io;
use thiserror::Error;
use tokio::sync::mpsc::error::SendError as MpscSendError;
use tokio::sync::oneshot::error::RecvError as OneshotRecvError;
use crate::{error, protocol::Status};
#[derive(Debug, Clone, Error)]
pub enum Error {
#[error("{}: {}", .0.status_code, .0.error_message)]
Status(Status),
#[error("I/O: {0}")]
IO(String),
#[error("Timeout")]
Timeout,
#[error("Limit exceeded: {0}")]
Limited(String),
#[error("Unexpected packet")]
UnexpectedPacket,
#[error("{0}")]
UnexpectedBehavior(String),
}
impl From<Status> for Error {
fn from(status: Status) -> Self {
Self::Status(status)
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Self::IO(error.to_string())
}
}
impl<T> From<MpscSendError<T>> for Error {
fn from(err: MpscSendError<T>) -> Self {
Self::UnexpectedBehavior(format!("SendError: {}", err))
}
}
impl From<OneshotRecvError> for Error {
fn from(err: OneshotRecvError) -> Self {
Self::UnexpectedBehavior(format!("RecvError: {}", err))
}
}
impl From<error::Error> for Error {
fn from(error: error::Error) -> Self {
Self::UnexpectedBehavior(error.to_string())
}
}