use std::error;
use std::fmt;
use std::io::Error as IoError;
use std::num::ParseIntError;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum ErrorKind {
CapacityOverflow,
InvalidInput,
}
impl ErrorKind {
fn as_str(&self) -> &str {
use ErrorKind::*;
match *self {
CapacityOverflow => "data capacity exceeds the ord path's maximum",
InvalidInput => "invalid input",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
}
impl Error {
pub(crate) const fn new(kind: ErrorKind) -> Error {
Error { kind }
}
pub const fn kind(&self) -> ErrorKind {
self.kind
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
self.kind.fmt(fmt)
}
}
impl error::Error for Error {
#[allow(deprecated, deprecated_in_future)]
fn description(&self) -> &str {
self.kind.as_str()
}
}
impl From<ParseIntError> for Error {
fn from(_: ParseIntError) -> Error {
Error::new(ErrorKind::InvalidInput)
}
}
impl From<IoError> for Error {
fn from(_: IoError) -> Error {
Error::new(ErrorKind::InvalidInput)
}
}