rpc_router/request/
request.rs1use crate::RequestParsingError;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5#[derive(Deserialize, Serialize, Clone)]
7pub struct Request {
8 pub id: Value,
9 pub method: String,
10 pub params: Option<Value>,
11}
12
13impl Request {
14 pub fn from_value(mut value: Value) -> Result<Request, RequestParsingError> {
18 let Some(version) = value.get_mut("jsonrpc").map(Value::take) else {
25 let (id, method) = take_rpc_ref(value);
26 return Err(RequestParsingError::VersionMissing { id, method });
27 };
28 if version.as_str().unwrap_or_default() != "2.0" {
30 let (id, method) = take_rpc_ref(value);
31 return Err(RequestParsingError::VersionInvalid { id, method, version });
32 }
33
34 if value.get("id").is_none() {
36 return Err(RequestParsingError::IdMissing {
37 method: get_method(value),
38 });
39 }
40
41 let Some(method) = value.get("method") else {
43 let id = value.get_mut("id").map(Value::take);
44 return Err(RequestParsingError::MethodMissing { id });
45 };
46
47 if method.as_str().is_none() {
49 let id = value.get_mut("id").map(Value::take);
50 let method = value.get_mut("method").map(Value::take).unwrap_or_default();
53 return Err(RequestParsingError::MethodInvalidType { id, method });
54 }
55
56 let res = serde_json::from_value(value).map_err(RequestParsingError::Parse)?;
58
59 Ok(res)
60 }
61}
62
63fn take_rpc_ref(mut value: Value) -> (Option<Value>, Option<String>) {
65 let id = value.get_mut("id").map(Value::take);
66 let method = value.get_mut("method").and_then(|v| v.as_str().map(|s| s.to_string()));
67 (id, method)
68}
69
70fn get_method(value: Value) -> Option<String> {
71 value.get("method").and_then(|v| v.as_str().map(|s| s.to_string()))
72}
73
74impl TryFrom<Value> for Request {
77 type Error = RequestParsingError;
78 fn try_from(value: Value) -> Result<Request, RequestParsingError> {
79 Request::from_value(value)
80 }
81}