cogo/std/errors/
mod.rs

1use std::fmt::{self, Debug, Display, Formatter};
2use std::io::ErrorKind::UnexpectedEof;
3use std::sync::mpsc::RecvError;
4use crate::std::io::io;
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 where E: std::fmt::Display {
19        Self {
20            inner: format!("{}{}", info, e)
21        }
22    }
23
24    pub fn to_string(&self) -> String {
25        self.inner.clone()
26    }
27}
28
29
30/// cogo::std::errors::Error
31#[macro_export]
32macro_rules! err {
33     ($($arg:tt)*) => {{
34         $crate::std::errors::Error{
35             inner: format!($($arg)*)
36         }
37     }}
38}
39
40///new error
41#[inline]
42pub fn new(text: String) -> Error {
43    Error {
44        inner: text
45    }
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
64
65impl From<std::io::Error> for Error {
66    #[inline]
67    fn from(err: std::io::Error) -> Self {
68        if err.kind().eq(&UnexpectedEof) {
69            return io::EOF.clone();
70        }
71        if err.kind().eq(&std::io::ErrorKind::UnexpectedEof) {
72            return io::ErrUnexpectedEOF.clone();
73        }
74        new(err.to_string())
75    }
76}
77
78impl std::error::Error for Error {}
79
80impl From<&str> for Error {
81    fn from(arg: &str) -> Self {
82        return new(arg.to_string());
83    }
84}
85
86impl From<std::string::String> for Error {
87    fn from(arg: String) -> Self {
88        return new(arg);
89    }
90}
91
92impl From<&dyn std::error::Error> for Error {
93    fn from(arg: &dyn std::error::Error) -> Self {
94        return new(arg.to_string());
95    }
96}
97
98impl From<Box<dyn std::error::Error>> for Error {
99    fn from(arg: Box<dyn std::error::Error>) -> Self {
100        return new(arg.to_string());
101    }
102}
103
104impl From<&Box<dyn std::error::Error>> for Error {
105    fn from(arg: &Box<dyn std::error::Error>) -> Self {
106        return new(arg.to_string());
107    }
108}
109
110impl From<time::error::InvalidFormatDescription> for Error {
111    fn from(arg: time::error::InvalidFormatDescription) -> Self {
112        return new(arg.to_string());
113    }
114}
115
116impl From<time::error::Parse> for Error {
117    fn from(arg: time::error::Parse) -> Self {
118        return new(arg.to_string());
119    }
120}
121
122impl From<std::sync::mpsc::RecvError> for Error{
123    fn from(e: RecvError) -> Self {
124       return new(e.to_string());
125    }
126}
127
128impl <T>From<std::sync::mpsc::SendError<T>> for Error{
129    fn from(e: std::sync::mpsc::SendError<T>) -> Self {
130        return new(e.to_string());
131    }
132}