1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Deserialize)]
7pub struct RpcRequest {
8 pub jsonrpc: String,
10 pub method: String,
12 pub params: serde_json::Value,
14 pub id: Option<String>,
16}
17
18#[derive(Debug, Serialize)]
20pub struct RpcResponse {
21 pub jsonrpc: String,
23 pub result: Option<serde_json::Value>,
25 pub error: Option<RpcError>,
27 pub id: Option<String>,
29}
30
31#[derive(Debug, Serialize)]
33pub struct RpcError {
34 pub code: i32,
36 pub message: String,
38 pub data: Option<serde_json::Value>,
40}
41
42impl RpcResponse {
43 #[must_use]
45 pub fn success(id: Option<String>, result: serde_json::Value) -> Self {
46 Self {
47 jsonrpc: "2.0".to_string(),
48 result: Some(result),
49 error: None,
50 id,
51 }
52 }
53
54 #[must_use]
56 pub fn error(id: Option<String>, code: i32, message: impl Into<String>) -> Self {
57 Self {
58 jsonrpc: "2.0".to_string(),
59 result: None,
60 error: Some(RpcError {
61 code,
62 message: message.into(),
63 data: None,
64 }),
65 id,
66 }
67 }
68}
69
70pub const PARSE_ERROR: i32 = -32700;
73pub const INVALID_REQUEST: i32 = -32600;
75pub const METHOD_NOT_FOUND: i32 = -32601;
77pub const INVALID_PARAMS: i32 = -32602;
79pub const INTERNAL_ERROR: i32 = -32603;
81
82pub const UNAUTHORIZED: i32 = -32001;
85pub const FORBIDDEN: i32 = -32002;
87pub const NOT_FOUND: i32 = -32003;