rust_cutil/
meta_config.rs

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