From a051fe7c4ae0aad9a1f7651235f475547e9328a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Y=C4=B1lmaz?= <mail@emreyilmaz.me> Date: Tue, 16 Jan 2024 12:43:29 +0100 Subject: [PATCH] support both int, and string in the ID field --- src/main.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9e9ffcb..41edd77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,11 +47,20 @@ enum APICall { Batch(Vec<APIRequest>), } +// Enum for id in JSONRPC body. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +enum ID { + Str(String), + Int(u32), +} + + // Structure for API calls. #[derive(Serialize, Deserialize, Debug)] struct APIRequest { jsonrpc: String, - id: String, + id: ID, method: String, #[serde(skip_serializing_if = "Option::is_none")] params: Option<Value>, @@ -79,7 +88,7 @@ impl Serialize for ErrorField { #[derive(Serialize, Deserialize, Debug)] struct ErrorStructure { jsonrpc: String, - id: String, + id: ID, code: i32, message: String, error: ErrorField, @@ -95,7 +104,7 @@ enum Endpoints { struct APICallResponse { jsonrpc: String, result: Value, - id: String, + id: ID, cached: bool, } @@ -315,7 +324,7 @@ async fn api_call( Err(_) => { return HttpResponse::InternalServerError().json(ErrorStructure { jsonrpc: "2.0".to_string(), - id: "0".to_string(), + id: ID::Int(0), code: -32000, message: "Internal Server Error".to_string(), error: ErrorField::Message("Invalid Cloudflare Proxy Header.".to_string()), @@ -343,7 +352,7 @@ async fn api_call( if requests.len() > 100 { return HttpResponse::InternalServerError().json(ErrorStructure { jsonrpc: "2.0".to_string(), - id: "0".to_string(), + id: ID::Int(0), code: -32600, message: "Request parameter error.".to_string(), error: ErrorField::Message( -- GitLab