use std::error::Error as StdError;
use std::fmt;
#[derive(Debug)]
pub enum Error {
WouldBlock,
Stopped,
Disconnected(String),
InvalidConfig(String),
Backend(Box<dyn StdError + Send + Sync>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::WouldBlock => write!(f, "would block: device cannot accept more data"),
Error::Stopped => write!(f, "stopped: stream was explicitly stopped"),
Error::Disconnected(msg) => write!(f, "disconnected: {}", msg),
Error::InvalidConfig(msg) => write!(f, "invalid configuration: {}", msg),
Error::Backend(e) => write!(f, "backend error: {}", e),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::Backend(e) => Some(e.as_ref()),
_ => None,
}
}
}
impl Error {
pub fn disconnected(msg: impl Into<String>) -> Self {
Error::Disconnected(msg.into())
}
pub fn invalid_config(msg: impl Into<String>) -> Self {
Error::InvalidConfig(msg.into())
}
pub fn backend(err: impl StdError + Send + Sync + 'static) -> Self {
Error::Backend(Box::new(err))
}
pub fn is_would_block(&self) -> bool {
matches!(self, Error::WouldBlock)
}
pub fn is_disconnected(&self) -> bool {
matches!(self, Error::Disconnected(_))
}
pub fn is_stopped(&self) -> bool {
matches!(self, Error::Stopped)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
if err.kind() == std::io::ErrorKind::WouldBlock {
Error::WouldBlock
} else {
Error::Backend(Box::new(err))
}
}
}
pub type Result<T> = std::result::Result<T, Error>;