use thiserror::Error;
#[cfg(feature = "websocket")]
#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
use tokio_tungstenite::tungstenite::Error as WsError;
pub type WalleResult<T> = std::result::Result<T, WalleError>;
#[derive(Error, Debug)]
pub enum WalleError {
#[error("Serde Json error: {0}")]
SerdeJsonError(#[from] serde_json::Error),
#[error("Serde MsgPack error: {0}")]
SerdeMsgPackError(#[from] rmp_serde::decode::Error),
#[error("Authorization failed")]
AuthorizationFailed,
#[error("Tcpconnect connect to {0} failed")]
TcpConnectFailed(std::io::Error),
#[cfg(feature = "websocket")]
#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
#[error("Websocket upgrade failed: {0}")]
WebsocketUpgradeFail(#[from] WsError),
#[error("Websocket link has no peer address")]
WebsocketNoAddress,
#[error("Action send error")]
ActionSendError,
#[error("Action Response Timeout")]
ActionResponseTimeout,
#[error("Action Response RecvError:{0}")]
ActionResponseRecvError(#[from] tokio::sync::oneshot::error::RecvError),
#[error("TcpServer bind address error: {0}")]
TcpServerBindAddressError(#[from] std::io::Error),
#[error("OneBot is already running")]
AlreadyRunning,
#[error("ExtendedMap missed key: {0}")]
MapMissedKey(String),
#[error("ExtendedMap value type mismatch: expect {0}, got {1}")]
MapValueTypeMismatch(String, String),
}
pub(crate) trait WalleLogExt: Sized {
fn wran_err(&self);
fn error_err(&self);
fn info(self, s: &str) -> Self {
tracing::info!(target: "Walle-core", "{}", s);
self
}
fn debug(self, s: &str) -> Self {
tracing::debug!(target: "Walle-core", "{}", s);
self
}
fn trace(self, s: &str) -> Self {
tracing::trace!(target: "Walle-core", "{}", s);
self
}
}
impl<T> WalleLogExt for WalleResult<T> {
fn wran_err(&self) {
if let Err(e) = self {
e.wran_err();
}
}
fn error_err(&self) {
if let Err(e) = self {
e.error_err();
}
}
}
impl WalleLogExt for WalleError {
fn wran_err(&self) {
use tracing::warn;
warn!(target: "Walle-core","{}", self);
}
fn error_err(&self) {
use tracing::error;
error!(target: "Walle-core","{}", self);
}
}