casper_client/json_rpc/
id.rs

1use std::fmt::{self, Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5/// An identifier for a JSON-RPC, provided by the client and returned in the response.
6///
7/// The JSON-RPC spec allows for NULL to be used as the RPC-ID to imply a client notification, but
8/// the Casper node JSON-RPC server does not provide any such notification endpoints, so this is
9/// not supported here.
10#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
11#[serde(untagged)]
12pub enum Id {
13    /// A numeric identifier.
14    Number(i64),
15    /// A text identifier.
16    String(String),
17}
18
19impl From<i64> for Id {
20    fn from(value: i64) -> Self {
21        Id::Number(value)
22    }
23}
24
25impl From<String> for Id {
26    fn from(value: String) -> Self {
27        Id::String(value)
28    }
29}
30
31impl From<&Id> for jsonrpc_lite::Id {
32    fn from(rpc_id: &Id) -> Self {
33        match rpc_id {
34            Id::String(string_id) => jsonrpc_lite::Id::Str(string_id.clone()),
35            Id::Number(number_id) => jsonrpc_lite::Id::Num(*number_id),
36        }
37    }
38}
39
40impl Display for Id {
41    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
42        match self {
43            Id::Number(number) => write!(formatter, "{}", number),
44            Id::String(string) => write!(formatter, "{}", string),
45        }
46    }
47}