discord_webhook_proxy/api/
mod.rs1pub(super) mod structs;
2pub mod discord;
3pub mod webhook_queue;
4
5use rocket::{
6 http::Status,
7 response,
8 serde::{json::serde_json::json, Serialize},
9 Request, Response,
10};
11
12pub type ApiResult<R> = Result<R, ApiError>;
13
14#[derive(Serialize, Debug)]
15#[serde(crate = "rocket::serde")]
16pub struct ApiError {
17 code: u16,
18 message: String,
19}
20
21impl ApiError {
22 pub fn new<S>(status: Status, message: S) -> Self
23 where
24 S: Into<String>,
25 {
26 Self {
27 code: status.code,
28 message: message.into(),
29 }
30 }
31
32 pub fn message<S>(status: Status, message: S) -> Self
33 where
34 S: Into<String>,
35 {
36 Self::new(status, message)
37 }
38}
39
40impl<'a> response::Responder<'a, 'a> for ApiError {
41 fn respond_to(self, request: &'a Request<'_>) -> response::Result<'a> {
42 let status = Status::from_code(self.code).unwrap_or_default();
43 let error = json!({ "error": self });
44
45 Response::build_from(error.respond_to(request)?)
46 .status(status)
47 .ok()
48 }
49}