1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::{fmt};
use std::error::Error;
use std::fmt::Display;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum LogError {
    E(String)
}

impl From<&str> for LogError {
    fn from(arg: &str) -> Self {
        return LogError::E(arg.to_string());
    }
}

impl From<std::string::String> for LogError {
    fn from(arg: String) -> Self {
        return LogError::E(arg);
    }
}

impl Display for LogError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        return match self {
            LogError::E(ref err) => {
                write!(f, "Rbatis Error: {}", err)
            }
        };
    }
}

impl Error for LogError {
    fn description(&self) -> &str {
        return match self {
            LogError::E(data) => {
                data.as_str()
            }
        };
    }
}


pub trait AsStdResult<T> where T: Clone {
    fn as_std_result(&self) -> Result<T, Box<dyn std::error::Error>>;
}

impl<T> AsStdResult<T> for Result<T, LogError> where T: Clone {
    fn as_std_result(&self) -> Result<T, Box<dyn std::error::Error>> {
        return match self {
            Err(e) => {
                Err(Box::new(e.clone()))
            }
            Ok(o) => {
                Ok(o.clone())
            }
        };
    }
}


#[test]
pub fn test_error() {
    let e = e().err().unwrap();
    println!("{}", e);
}

fn e() -> Result<String, LogError> {
    return Err(LogError::from("e"));
}