mco/std/errors/
mod.rs

1use crate::std::io::{EOF, ERR_UNEXPECTED_EOF};
2use std::fmt::{self, Debug, Display, Formatter};
3use std::io::ErrorKind::UnexpectedEof;
4use std::sync::mpsc::RecvError;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Clone, Eq, PartialEq, Hash)]
9pub struct Error {
10    pub inner: String,
11}
12
13impl Error {
14    pub fn error(&self) -> String {
15        self.inner.clone()
16    }
17
18    pub fn warp<E>(e: E, info: &str) -> Self
19    where
20        E: std::fmt::Display,
21    {
22        Self {
23            inner: format!("{}{}", info, e),
24        }
25    }
26
27    pub fn to_string(&self) -> String {
28        self.inner.clone()
29    }
30}
31
32/// mco::std::errors::Error
33#[macro_export]
34macro_rules! err {
35     ($($arg:tt)*) => {{
36         $crate::std::errors::Error{
37             inner: format!($($arg)*)
38         }
39     }}
40}
41
42///new error
43#[inline]
44pub fn new(text: String) -> Error {
45    Error { inner: text }
46}
47
48pub trait FromError<T>: Sized {
49    fn from_err(_: T) -> Error;
50}
51
52impl Display for Error {
53    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
54        std::fmt::Display::fmt(&self.inner, f)
55    }
56}
57
58impl Debug for Error {
59    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60        std::fmt::Debug::fmt(&self.inner, f)
61    }
62}
63
64impl From<std::io::Error> for Error {
65    #[inline]
66    fn from(err: std::io::Error) -> Self {
67        if err.kind().eq(&UnexpectedEof) {
68            return EOF.clone();
69        }
70        if err.kind().eq(&std::io::ErrorKind::UnexpectedEof) {
71            return ERR_UNEXPECTED_EOF.clone();
72        }
73        new(err.to_string())
74    }
75}
76
77impl std::error::Error for Error {}
78
79impl From<&str> for Error {
80    fn from(arg: &str) -> Self {
81        return new(arg.to_string());
82    }
83}
84
85impl From<std::string::String> for Error {
86    fn from(arg: String) -> Self {
87        return new(arg);
88    }
89}
90
91impl From<&dyn std::error::Error> for Error {
92    fn from(arg: &dyn std::error::Error) -> Self {
93        return new(arg.to_string());
94    }
95}
96
97impl From<Box<dyn std::error::Error>> for Error {
98    fn from(arg: Box<dyn std::error::Error>) -> Self {
99        return new(arg.to_string());
100    }
101}
102
103impl From<&Box<dyn std::error::Error>> for Error {
104    fn from(arg: &Box<dyn std::error::Error>) -> Self {
105        return new(arg.to_string());
106    }
107}
108
109impl From<time::error::InvalidFormatDescription> for Error {
110    fn from(arg: time::error::InvalidFormatDescription) -> Self {
111        return new(arg.to_string());
112    }
113}
114
115impl From<time::error::Parse> for Error {
116    fn from(arg: time::error::Parse) -> Self {
117        return new(arg.to_string());
118    }
119}
120
121impl From<std::sync::mpsc::RecvError> for Error {
122    fn from(e: RecvError) -> Self {
123        return new(e.to_string());
124    }
125}
126
127impl<T> From<std::sync::mpsc::SendError<T>> for Error {
128    fn from(e: std::sync::mpsc::SendError<T>) -> Self {
129        return new(e.to_string());
130    }
131}