-
Notifications
You must be signed in to change notification settings - Fork 225
/
method.rs
178 lines (144 loc) · 4.89 KB
/
method.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! JSON-RPC request methods
use core::{
fmt::{self, Display},
str::FromStr,
};
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use crate::{prelude::*, Error};
/// JSON-RPC request methods.
///
/// Serialized as the "method" field of JSON-RPC/HTTP requests.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum Method {
/// Get ABCI info
AbciInfo,
/// Get ABCI query
AbciQuery,
/// Get block info
Block,
/// Get block info by hash
BlockByHash,
/// Get ABCI results for a particular block
BlockResults,
/// Search for blocks by their BeginBlock and EndBlock events
BlockSearch,
/// Get blockchain info
Blockchain,
/// Broadcast transaction asynchronously
BroadcastTxAsync,
/// Broadcast transaction synchronously
BroadcastTxSync,
/// Broadcast transaction commit
BroadcastTxCommit,
/// Get commit info for a block
Commit,
/// Get consensus parameters
ConsensusParams,
/// Get consensus state
ConsensusState,
/// Get genesis file
Genesis,
/// Get block header
Header,
/// Get block header by hash
HeaderByHash,
/// Get health info
Health,
/// Get network info
NetInfo,
/// Get node status
Status,
/// Find transaction by hash
Tx,
/// Search for transactions with their results
TxSearch,
/// Get validator info for a block
Validators,
/// Subscribe to events
Subscribe,
/// Unsubscribe from events
Unsubscribe,
/// Broadcast evidence
BroadcastEvidence,
}
impl Method {
/// Get a static string which represents this method name
pub fn as_str(self) -> &'static str {
match self {
Method::AbciInfo => "abci_info",
Method::AbciQuery => "abci_query",
Method::Block => "block",
Method::BlockByHash => "block_by_hash",
Method::BlockResults => "block_results",
Method::BlockSearch => "block_search",
Method::Blockchain => "blockchain",
Method::BroadcastEvidence => "broadcast_evidence",
Method::BroadcastTxAsync => "broadcast_tx_async",
Method::BroadcastTxSync => "broadcast_tx_sync",
Method::BroadcastTxCommit => "broadcast_tx_commit",
Method::Commit => "commit",
Method::ConsensusParams => "consensus_params",
Method::ConsensusState => "consensus_state",
Method::Genesis => "genesis",
Method::Header => "header",
Method::HeaderByHash => "header_by_hash",
Method::Health => "health",
Method::NetInfo => "net_info",
Method::Status => "status",
Method::Subscribe => "subscribe",
Method::Tx => "tx",
Method::TxSearch => "tx_search",
Method::Unsubscribe => "unsubscribe",
Method::Validators => "validators",
}
}
}
impl FromStr for Method {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Ok(match s {
"abci_info" => Method::AbciInfo,
"abci_query" => Method::AbciQuery,
"block" => Method::Block,
"block_by_hash" => Method::BlockByHash,
"block_results" => Method::BlockResults,
"header" => Method::Header,
"header_by_hash" => Method::HeaderByHash,
"block_search" => Method::BlockSearch,
"blockchain" => Method::Blockchain,
"broadcast_evidence" => Method::BroadcastEvidence,
"broadcast_tx_async" => Method::BroadcastTxAsync,
"broadcast_tx_sync" => Method::BroadcastTxSync,
"broadcast_tx_commit" => Method::BroadcastTxCommit,
"commit" => Method::Commit,
"consensus_params" => Method::ConsensusParams,
"consensus_state" => Method::ConsensusState,
"genesis" => Method::Genesis,
"health" => Method::Health,
"net_info" => Method::NetInfo,
"status" => Method::Status,
"subscribe" => Method::Subscribe,
"tx" => Method::Tx,
"tx_search" => Method::TxSearch,
"unsubscribe" => Method::Unsubscribe,
"validators" => Method::Validators,
other => return Err(Error::method_not_found(other.to_string())),
})
}
}
impl Display for Method {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl Serialize for Method {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_str().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Method {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::from_str(&String::deserialize(deserializer)?)
.map_err(|e| D::Error::custom(format!("{e}")))
}
}