use std::error;
use std::fmt;
#[derive(Debug, Copy, Clone)]
pub struct Error(Kind);
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[repr(u8)]
pub(crate) enum Kind {
Shutdown = 1,
AtCapacity = 2,
Invalid = 3,
}
impl From<Kind> for Error {
fn from(k: Kind) -> Self {
Error(k)
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Elapsed(());
#[derive(Debug)]
pub(crate) enum InsertError {
Elapsed,
}
impl Error {
pub fn shutdown() -> Error {
Error(Kind::Shutdown)
}
pub fn is_shutdown(&self) -> bool {
matches!(self.0, Kind::Shutdown)
}
pub fn at_capacity() -> Error {
Error(Kind::AtCapacity)
}
pub fn is_at_capacity(&self) -> bool {
matches!(self.0, Kind::AtCapacity)
}
pub fn invalid() -> Error {
Error(Kind::Invalid)
}
pub fn is_invalid(&self) -> bool {
matches!(self.0, Kind::Invalid)
}
}
impl error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let descr = match self.0 {
Kind::Shutdown => {
"the timer is shutdown, must be called from the context of Tokio runtime"
}
Kind::AtCapacity => "timer is at capacity and cannot create a new entry",
Kind::Invalid => "timer duration exceeds maximum duration",
};
write!(fmt, "{}", descr)
}
}
impl Elapsed {
pub(crate) fn new() -> Self {
Elapsed(())
}
}
impl fmt::Display for Elapsed {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"deadline has elapsed".fmt(fmt)
}
}
impl std::error::Error for Elapsed {}
impl From<Elapsed> for std::io::Error {
fn from(_err: Elapsed) -> std::io::Error {
std::io::ErrorKind::TimedOut.into()
}
}