casper_client/json_rpc/
success_response.rs

1use std::fmt::{self, Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5use super::JsonRpcId;
6
7const JSON_RPC_VERSION: &str = "2.0";
8
9/// A successful response to a JSON-RPC request.
10#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
11pub struct SuccessResponse<T> {
12    /// A String specifying the version of the JSON-RPC protocol.
13    pub jsonrpc: String,
14    /// The same identifier as provided in the corresponding request's `id` field.
15    pub id: JsonRpcId,
16    /// The returned result.
17    pub result: T,
18}
19
20impl<T> SuccessResponse<T> {
21    /// Constructs a new `SuccessResponse`.
22    pub fn new(id: JsonRpcId, result: T) -> Self {
23        SuccessResponse {
24            jsonrpc: JSON_RPC_VERSION.to_string(),
25            id,
26            result,
27        }
28    }
29}
30
31impl<T: Serialize> Display for SuccessResponse<T> {
32    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
33        write!(
34            formatter,
35            "{}",
36            serde_json::to_string(self).map_err(|_| fmt::Error)?
37        )
38    }
39}