Skip to main content

engram_core/
protocol.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Deserialize)]
4pub struct JsonRequest {
5    pub id: String,
6    pub method: String,
7    pub params: serde_json::Value,
8}
9
10#[derive(Serialize)]
11pub struct JsonResponse {
12    pub id: String,
13    pub ok: bool,
14    pub data: Option<serde_json::Value>,
15    pub error: Option<ErrorPayload>,
16}
17
18#[derive(Serialize)]
19pub struct ErrorPayload {
20    pub code: u32,
21    pub message: String,
22}
23
24impl JsonResponse {
25    pub fn success(id: String, data: serde_json::Value) -> Self {
26        Self {
27            id,
28            ok: true,
29            data: Some(data),
30            error: None,
31        }
32    }
33
34    pub fn error(id: String, code: u32, message: String) -> Self {
35        Self {
36            id,
37            ok: false,
38            data: None,
39            error: Some(ErrorPayload { code, message }),
40        }
41    }
42}