rust_cutil/cutil/
meta_config.rs

1use std::error::Error;
2use std::num::ParseIntError;
3use std::string::FromUtf16Error;
4
5use axum::http::header::InvalidHeaderValue;
6use axum_extra::extract::multipart::MultipartError;
7use sea_orm::DbErr;
8use serde_json::to_string;
9
10use crate::cutil::meta::Meta;
11
12impl<T> Into<Result<T, Meta>> for Meta {
13  fn into(self) -> Result<T, Meta> {
14    Err(self)
15  }
16}
17
18impl axum::response::IntoResponse for Meta {
19  fn into_response(self) -> axum::response::Response {
20    let status_code = match self.name.as_str() {
21      "unauthorized" => http::StatusCode::UNAUTHORIZED,
22      _ => http::StatusCode::INTERNAL_SERVER_ERROR,
23    }; 
24    let text = to_string(&self).unwrap_or_default();
25    (status_code, text).into_response()
26  }
27}
28
29impl From<&'_ (dyn Error + 'static)> for Meta {
30  fn from(error: &'_ (dyn Error + 'static)) -> Self {
31    Meta {
32      name: "error".to_string(),
33      message: error.to_string(),
34      data: None,
35    }
36  }
37}
38
39impl From<Box<dyn Error + 'static>> for Meta {
40  fn from(error: Box<dyn Error + 'static>) -> Self {
41    Meta::from(&*error)
42  }
43}
44
45impl From<serde_json::Error> for Meta {
46  fn from(error: serde_json::Error) -> Self {
47    Meta {
48      name: "serde_json_error".to_string(),
49      message: error.to_string(),
50      data: None,
51    }
52  }
53}
54
55impl From<ParseIntError> for Meta {
56  fn from(error: ParseIntError) -> Self {
57    Meta {
58      name: "parse_int_error".to_string(),
59      message: error.to_string(),
60      data: None,
61    }
62  }
63}
64
65impl From<InvalidHeaderValue> for Meta {
66  fn from(error: InvalidHeaderValue) -> Self {
67    Meta {
68      name: "invalid_header_value".to_string(),
69      message: error.to_string(),
70      data: None,
71    }
72  }
73}
74
75impl From<std::io::Error> for Meta {
76  fn from(error: std::io::Error) -> Self {
77    Meta {
78      name: "io_error".to_string(),
79      message: error.to_string(),
80      data: None,
81    }
82  }
83}
84
85impl From<MultipartError> for Meta {
86  fn from(error: MultipartError) -> Self {
87    Meta {
88      name: "multipart_error".to_string(),
89      message: error.to_string(),
90      data: None,
91    }
92  }
93}
94
95impl From<FromUtf16Error> for Meta {
96  fn from(error: FromUtf16Error) -> Self {
97    Meta {
98      name: "from_utf16_error".to_string(),
99      message: error.to_string(),
100      data: None,
101    }
102  }
103}
104
105impl From<rumqttc::ClientError> for Meta {
106  fn from(error: rumqttc::ClientError) -> Self {
107    Meta {
108      name: "rumqttc_client_error".to_string(),
109      message: error.to_string(),
110      data: None,
111    }
112  }
113}
114
115impl From<rumqttc::v5::ClientError> for Meta {
116  fn from(error: rumqttc::v5::ClientError) -> Self {
117    Meta {
118      name: "rumqttc_client_error".to_string(),
119      message: error.to_string(),
120      data: None,
121    }
122  }
123}
124
125impl From<tokio::sync::watch::error::SendError<bool>> for Meta {
126  fn from(error: tokio::sync::watch::error::SendError<bool>) -> Self {
127    Meta {
128      name: "sync_watch_send_error".to_string(),
129      message: error.to_string(),
130      data: None,
131    }
132  }
133}
134
135impl From<reqwest::Error> for Meta {
136  fn from(error: reqwest::Error) -> Self {
137    Meta {
138      name: "reqwest_error".to_string(),
139      message: error.to_string(),
140      data: None,
141    }
142  }
143}
144
145impl From<DbErr> for Meta {
146  fn from(error: DbErr) -> Self {
147    Meta {
148      name: "db_err".to_string(),
149      message: error.to_string(),
150      data: None,
151    }
152  }
153}
154
155impl From<(&str, &str)> for Meta {
156  fn from(error: (&str, &str)) -> Self {
157    Meta {
158      name: error.0.to_string(),
159      message: error.1.to_string(),
160      data: None,
161    }
162  }
163}
164
165impl From<anyhow::Error> for Meta {
166  fn from(error: anyhow::Error) -> Self {
167    Meta {
168      name: "anyhow_error".to_string(),
169      message: error.to_string(),
170      data: None,
171    }
172  }
173}