e_utils/
res.rs

1use std::{
2  borrow::Cow,
3  fmt::Display,
4  sync::{MutexGuard, PoisonError, RwLockReadGuard, RwLockWriteGuard},
5};
6
7/// web result
8pub type Result<T, E = Error> = std::result::Result<T, E>;
9/// Any Error
10pub type AnyError = Box<dyn std::error::Error>;
11/// Any Result
12pub type AnyResult<T> = std::result::Result<T, AnyError>;
13
14/// Http share error
15#[derive(thiserror::Error, Debug, Clone)]
16pub enum Error {
17  /// Decode
18  #[error("Decoding error: {0}")]
19  Decode(String),
20  /// RwLock read lock error
21  #[error("RwLock read error: {0}")]
22  RwLockReadError(String),
23  /// RwLock write lock error
24  #[error("RwLock write error: {0}")]
25  RwLockWriteError(String),
26  /// Mutex lock error
27  #[error("Mutex error: {0}")]
28  MutexError(String),
29  /// MPSC Recv Error
30  #[error("Mpsc Recv Error: {0}")]
31  RecvError(#[from] std::sync::mpsc::RecvError),
32  /// BadRequest `400 Bad Request`
33  #[error("400 Bad Request")]
34  BadRequest,
35  /// Already exists
36  #[error("Exists: {0}")]
37  Exists(Cow<'static, str>),
38  /// SerdeJson `400 Bad Request`
39  // #[cfg(feature = "serde_json")]
40  // #[error("400 SerdeJson error: {0}")]
41  // SerdeJson(#[from] serde_json::Error),
42  /// Result `400 Bad Request`
43  #[error("Str: {0}")]
44  Str(Cow<'static, str>),
45  ///
46  #[error("String: {0}")]
47  String(String),
48  /// Excel `400 Bad Request`
49  #[cfg(feature = "regex")]
50  #[error("regex error: {0}")]
51  Regex(#[from] regex::Error),
52  /// Return `401 Unauthorized`
53  #[error("authentication required: {0}")]
54  Unauthorized(&'static str),
55  /// Return `403 Forbidden`
56  #[error("user may not perform that action")]
57  Forbidden,
58  /// Return `404 Not Found`
59  #[error("request path not found: {0}")]
60  NotFound(Cow<'static, str>),
61  /// Return `404 Not Found`
62  #[error("Option<{0}>")]
63  Option(Cow<'static, str>),
64  /// Not supported
65  #[error("Not supported: {0}")]
66  Unsupport(Cow<'static, str>),
67  ///
68  #[error("TryFromSlice: {0}")]
69  TryFromSlice(#[from] std::array::TryFromSliceError),
70  // /// IO return `500 INTERNAL_SERVER_ERROR`
71  // #[error("an error occurred with the std io: {0}")]
72  // Io(#[from] std::io::Error),
73  /// Env
74  #[error("env var error : {0}")]
75  Env(#[from] std::env::VarError),
76  ///
77  #[error("Empty")]
78  Empty,
79  // /// Std error return `500 INTERNAL_SERVER_ERROR`
80  // #[error("an error occurred with the std Error: {0}")]
81  // Std(#[from] AnyError),
82  /// Parse int error return 400
83  #[error("an error occurred with the parse Error: {0}")]
84  ParseNumber(#[from] std::num::ParseIntError),
85  /// Parse float error return 400
86  #[error("an error occurred with the parse Error: {0}")]
87  ParseFloatError(#[from] std::num::ParseFloatError),
88  /// Return `502 Internal Server Error` on an `anyhow::Error`.
89  #[error("Log: {0}")]
90  Log(Cow<'static, str>),
91  /// Nul
92  #[error("CString: {0}")]
93  Nul(#[from] std::ffi::NulError),
94  /// Any
95  #[error("{0}")]
96  Any(String),
97}
98
99impl From<String> for Error {
100  fn from(s: String) -> Self {
101    Error::String(s)
102  }
103}
104impl From<&'static str> for Error {
105  fn from(s: &'static str) -> Self {
106    Error::Str(Cow::Borrowed(s))
107  }
108}
109impl<T: Send + Sync + 'static> From<PoisonError<RwLockReadGuard<'_, T>>> for Error {
110  fn from(err: PoisonError<RwLockReadGuard<'_, T>>) -> Self {
111    Error::RwLockReadError(err.to_string())
112  }
113}
114
115impl<T: Send + Sync + 'static> From<PoisonError<MutexGuard<'_, T>>> for Error {
116  fn from(err: PoisonError<MutexGuard<'_, T>>) -> Self {
117    Error::MutexError(err.to_string())
118  }
119}
120
121impl<T: Send + Sync + 'static> From<PoisonError<RwLockWriteGuard<'_, T>>> for Error {
122  fn from(err: PoisonError<RwLockWriteGuard<'_, T>>) -> Self {
123    Error::RwLockWriteError(err.to_string())
124  }
125}
126
127/// Any Result
128pub trait AnyRes<T, E> {
129  /// change  any
130  fn any(self) -> Result<T>;
131}
132
133impl<T, E: Display> AnyRes<T, E> for std::result::Result<T, E> {
134  fn any(self) -> Result<T> {
135    match self {
136      Ok(v) => Result::Ok(v),
137      Err(e) => Result::Err(Error::Any(e.to_string())),
138    }
139  }
140}