-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstructs.rs
More file actions
95 lines (85 loc) · 2.39 KB
/
structs.rs
File metadata and controls
95 lines (85 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::collections::HashMap;
use std::sync::Arc;
use axum::body::Body;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use moka::future::Cache;
use serde::{Deserialize, Serialize};
use serde_json::{Number, Value};
use tokio::sync::{oneshot, RwLock};
pub type MokaCache = Cache<u64, R>;
#[derive(Serialize, Deserialize)]
pub struct JsonRpcRequest {
pub method: String,
pub params: Vec<Value>,
pub id: Option<u32>,
}
#[derive(Deserialize, Debug)]
pub struct JsonRpcResponse {
pub result: Option<Value>,
pub error: Option<Value>,
pub id: u32,
}
#[derive(Serialize, Clone, Debug)]
pub struct R {
pub success: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub response: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub health: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cache: Option<bool>,
}
impl R {
pub fn ok(payload: Value) -> Self {
Self {
success: true,
response: Some(payload),
code: None,
message: None,
health: None,
cache: None,
}
}
pub fn error(code: i32, message: String) -> Self {
Self {
success: false,
response: None,
code: Some(Value::Number(Number::from(code))),
message: Some(Value::String(message)),
health: None,
cache: None,
}
}
pub fn health(health: bool) -> Self {
Self {
success: true,
response: None,
code: None,
message: None,
health: Some(health),
cache: None,
}
}
}
pub type Callbacks = Arc<RwLock<HashMap<u32, oneshot::Sender<JsonRpcResponse>>>>;
pub struct AppError(anyhow::Error);
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let value = R::error(-1, self.0.to_string());
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(serde_json::to_string(&value).unwrap()))
.unwrap()
}
}
impl IntoResponse for R {
fn into_response(self) -> Response {
Json(self).into_response()
}
}