#![allow(dead_code)]
use std::error;
use std::fmt;
pub struct Error {
repr: Repr
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.repr, f)
}
}
enum Repr {
Simple(ErrorKind),
Custom(Box<Custom>),
}
#[derive(Debug)]
struct Custom {
kind: ErrorKind,
error: Box<dyn error::Error+Send+Sync>,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ErrorKind {
FormatError,
MissingValue,
UnimplementedFormat,
Other,
}
impl ErrorKind {
pub(crate) fn as_str(self) -> &'static str {
match self {
ErrorKind::FormatError => "format_error",
ErrorKind::MissingValue => "missing_value",
ErrorKind::UnimplementedFormat => "unimplemented_format",
ErrorKind::Other => "other",
}
}
}
impl From<ErrorKind> for Error {
#[inline]
fn from(kind: ErrorKind) -> Error {
Error {
repr: Repr::Simple(kind)
}
}
}
impl Error {
pub fn new<E>(kind: ErrorKind, error: E) -> Error
where E: Into<Box<dyn error::Error+Send+Sync>>
{
Self::_new(kind, error.into())
}
fn _new(kind: ErrorKind, error: Box<dyn error::Error+Send+Sync>) -> Error {
Error {
repr: Repr::Custom(Box::new(Custom {
kind,
error,
}))
}
}
pub fn get_ref(&self) -> Option<&(dyn error::Error+Send+Sync+'static)> {
match self.repr {
Repr::Simple(..) => None,
Repr::Custom(ref c) => Some(&*c.error),
}
}
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error+Send+Sync+'static)> {
match self.repr {
Repr::Simple(..) => None,
Repr::Custom(ref mut c) => Some(&mut *c.error),
}
}
pub fn into_inner(self) -> Option<Box<dyn error::Error+Send+Sync>> {
match self.repr {
Repr::Simple(..) => None,
Repr::Custom(c) => Some(c.error)
}
}
pub fn kind(&self) -> ErrorKind {
match self.repr {
Repr::Custom(ref c) => c.kind,
Repr::Simple(kind) => kind,
}
}
}
impl fmt::Debug for Repr {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.repr {
Repr::Custom(ref c) => c.error.fmt(fmt),
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match self.repr {
Repr::Simple(..) => self.kind().as_str(),
Repr::Custom(ref c) => c.error.description(),
}
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn error::Error> {
match self.repr {
Repr::Simple(..) => None,
Repr::Custom(ref c) => c.error.cause(),
}
}
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self.repr {
Repr::Simple(..) => None,
Repr::Custom(ref c) => c.error.source(),
}
}
}
fn _assert_error_is_sync_send() {
fn _is_sync_send<T: Sync+Send>() {}
_is_sync_send::<Error>();
}
#[cfg(test)]
mod tests {
use std::error::Error as _;
use super::{Error, ErrorKind};
#[test]
fn custom() {
let error = Error::new(
ErrorKind::Other, "test error"
);
assert_eq!(error.kind(), ErrorKind::Other);
}
#[test]
fn simple() {
let error = Error::from(
ErrorKind::Other
);
assert_eq!(error.kind(), ErrorKind::Other);
}
#[test]
fn errorkind_as_str() {
let error_format_error = Error::from(ErrorKind::FormatError);
let error_missing_value = Error::from(ErrorKind::MissingValue);
let error_other = Error::from(ErrorKind::Other);
let error_unimplemented_format = Error::from(ErrorKind::UnimplementedFormat);
assert_eq!(error_format_error.kind().as_str(), "format_error");
assert_eq!(error_missing_value.kind().as_str(), "missing_value");
assert_eq!(error_other.kind().as_str(), "other");
assert_eq!(error_unimplemented_format.kind().as_str(), "unimplemented_format");
}
#[test]
fn custom_get_ref() {
let error = Error::new(ErrorKind::Other, "test error");
let ref_error = error.get_ref();
assert!(ref_error.is_some());
assert_eq!(ref_error.unwrap().description(), "test error");
}
#[test]
fn simple_get_ref() {
let error = Error::from(ErrorKind::Other);
assert!(error.get_ref().is_none());
}
#[test]
fn custom_get_mut() {
let mut error = Error::new(ErrorKind::Other, "test error");
let ref_error = error.get_mut();
assert!(ref_error.is_some());
assert_eq!(ref_error.unwrap().description(), "test error");
}
#[test]
fn simple_get_mut() {
let mut error = Error::from(ErrorKind::Other);
assert!(error.get_mut().is_none());
}
#[test]
fn custom_into_inner() {
let error = Error::new(ErrorKind::Other, "test error");
let inner_error = error.into_inner();
assert!(inner_error.is_some());
assert_eq!(inner_error.unwrap().description(), "test error");
}
#[test]
fn simple_into_inner() {
let error = Error::from(ErrorKind::Other);
assert!(error.into_inner().is_none());
}
#[test]
fn custom_description() {
let error = Error::new(ErrorKind::Other, "test error");
assert_eq!(error.description(), "test error");
}
#[test]
fn simple_description() {
let error = Error::from(ErrorKind::Other);
assert_eq!(error.description(), ErrorKind::Other.as_str());
}
#[test]
#[allow(deprecated)]
fn custom_cause() {
let error = Error::new(ErrorKind::Other, "test error");
let error_cause = error.cause();
assert!(error_cause.is_none());
}
#[test]
#[allow(deprecated)]
fn simple_cause() {
let error = Error::from(ErrorKind::Other);
let error_cause = error.cause();
assert!(error_cause.is_none());
}
#[test]
fn custom_source() {
let error = Error::new(ErrorKind::Other, "test error");
let error_source = error.source();
assert!(error_source.is_none());
}
#[test]
fn simple_source() {
let error = Error::from(ErrorKind::Other);
let error_source = error.source();
assert!(error_source.is_none());
}
#[test]
fn custom_debug() {
let error = Error::new(ErrorKind::Other, "test error");
assert_eq!(
format!("{:?}", error),
"Custom { kind: Other, error: \"test error\" }"
);
}
#[test]
fn simple_debug() {
let error = Error::from(ErrorKind::Other);
assert_eq!(format!("{:?}", error), "Kind(Other)");
}
#[test]
fn custom_display() {
let error = Error::new(ErrorKind::Other, "test error");
assert_eq!(format!("{}", error), "test error");
}
#[test]
fn simple_display() {
let error = Error::from(ErrorKind::Other);
assert_eq!(format!("{}", error), "other");
}
#[test]
fn assert_error_is_sync_send() {
super::_assert_error_is_sync_send();
}
}