mybatis_sql/
error.rs

1//! Errorand Result types.
2use std::error::Error as StdError;
3use std::fmt::{self, Debug, Display};
4use std::io;
5
6use serde::de::Visitor;
7use serde::ser::{Serialize, Serializer};
8use serde::{Deserialize, Deserializer};
9
10pub type Result<T> = std::result::Result<T, Error>;
11
12/// A generic error that represents all the ways a method can fail inside of rexpr::core.
13#[derive(Debug)]
14#[non_exhaustive]
15pub enum Error {
16    /// Default Error
17    E(String),
18}
19
20impl Display for Error {
21    // IntellijRust does not understand that [non_exhaustive] applies only for downstream crates
22    // noinspection RsMatchCheck
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Error::E(error) => write!(f, "{}", error),
26        }
27    }
28}
29
30impl StdError for Error {}
31
32impl From<io::Error> for Error {
33    #[inline]
34    fn from(err: io::Error) -> Self {
35        Error::from(err.to_string())
36    }
37}
38
39impl From<&str> for Error {
40    fn from(arg: &str) -> Self {
41        return Error::E(arg.to_string());
42    }
43}
44
45impl From<std::string::String> for Error {
46    fn from(arg: String) -> Self {
47        return Error::E(arg);
48    }
49}
50
51impl From<&dyn std::error::Error> for Error {
52    fn from(arg: &dyn std::error::Error) -> Self {
53        return Error::E(arg.to_string());
54    }
55}
56
57impl Clone for Error {
58    fn clone(&self) -> Self {
59        Error::from(self.to_string())
60    }
61
62    fn clone_from(&mut self, source: &Self) {
63        *self = Self::from(source.to_string());
64    }
65}
66
67// This is what #[derive(Serialize)] would generate.
68impl Serialize for Error {
69    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
70    where
71        S: Serializer,
72    {
73        serializer.serialize_str(self.to_string().as_str())
74    }
75}
76
77struct ErrorVisitor;
78
79impl<'de> Visitor<'de> for ErrorVisitor {
80    type Value = String;
81
82    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
83        formatter.write_str("a string")
84    }
85
86    fn visit_string<E>(self, v: String) -> std::result::Result<Self::Value, E>
87    where
88        E: std::error::Error,
89    {
90        Ok(v)
91    }
92
93    fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
94    where
95        E: std::error::Error,
96    {
97        Ok(v.to_string())
98    }
99}
100
101impl<'de> Deserialize<'de> for Error {
102    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
103    where
104        D: Deserializer<'de>,
105    {
106        let r = deserializer.deserialize_string(ErrorVisitor)?;
107        return Ok(Error::from(r));
108    }
109}
110
111pub trait OptionToResult<T> {
112    fn to_result(self, error_str: &str) -> Result<T>;
113}
114
115impl<T> OptionToResult<T> for Option<T> {
116    fn to_result(self, error_str: &str) -> Result<T> {
117        if self.is_some() {
118            Ok(self.unwrap())
119        } else {
120            Err(Error::from(error_str))
121        }
122    }
123}