1use core::{
4 fmt::{self, Display},
5 str::FromStr,
6};
7
8use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
9
10use crate::{prelude::*, Error};
11
12#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
16pub enum Method {
17 AbciInfo,
19
20 AbciQuery,
22
23 Block,
25
26 BlockByHash,
28
29 BlockResults,
31
32 BlockSearch,
34
35 Blockchain,
37
38 BroadcastTxAsync,
40
41 BroadcastTxSync,
43
44 BroadcastTxCommit,
46
47 Commit,
49
50 ConsensusParams,
52
53 ConsensusState,
55
56 Genesis,
58
59 Header,
61
62 HeaderByHash,
64
65 Health,
67
68 NetInfo,
70
71 Status,
73
74 Tx,
76
77 TxSearch,
79
80 Validators,
82
83 Subscribe,
85
86 Unsubscribe,
88
89 BroadcastEvidence,
91}
92
93impl Method {
94 pub fn as_str(self) -> &'static str {
96 match self {
97 Method::AbciInfo => "abci_info",
98 Method::AbciQuery => "abci_query",
99 Method::Block => "block",
100 Method::BlockByHash => "block_by_hash",
101 Method::BlockResults => "block_results",
102 Method::BlockSearch => "block_search",
103 Method::Blockchain => "blockchain",
104 Method::BroadcastEvidence => "broadcast_evidence",
105 Method::BroadcastTxAsync => "broadcast_tx_async",
106 Method::BroadcastTxSync => "broadcast_tx_sync",
107 Method::BroadcastTxCommit => "broadcast_tx_commit",
108 Method::Commit => "commit",
109 Method::ConsensusParams => "consensus_params",
110 Method::ConsensusState => "consensus_state",
111 Method::Genesis => "genesis",
112 Method::Header => "header",
113 Method::HeaderByHash => "header_by_hash",
114 Method::Health => "health",
115 Method::NetInfo => "net_info",
116 Method::Status => "status",
117 Method::Subscribe => "subscribe",
118 Method::Tx => "tx",
119 Method::TxSearch => "tx_search",
120 Method::Unsubscribe => "unsubscribe",
121 Method::Validators => "validators",
122 }
123 }
124}
125
126impl FromStr for Method {
127 type Err = Error;
128
129 fn from_str(s: &str) -> Result<Self, Error> {
130 Ok(match s {
131 "abci_info" => Method::AbciInfo,
132 "abci_query" => Method::AbciQuery,
133 "block" => Method::Block,
134 "block_by_hash" => Method::BlockByHash,
135 "block_results" => Method::BlockResults,
136 "header" => Method::Header,
137 "header_by_hash" => Method::HeaderByHash,
138 "block_search" => Method::BlockSearch,
139 "blockchain" => Method::Blockchain,
140 "broadcast_evidence" => Method::BroadcastEvidence,
141 "broadcast_tx_async" => Method::BroadcastTxAsync,
142 "broadcast_tx_sync" => Method::BroadcastTxSync,
143 "broadcast_tx_commit" => Method::BroadcastTxCommit,
144 "commit" => Method::Commit,
145 "consensus_params" => Method::ConsensusParams,
146 "consensus_state" => Method::ConsensusState,
147 "genesis" => Method::Genesis,
148 "health" => Method::Health,
149 "net_info" => Method::NetInfo,
150 "status" => Method::Status,
151 "subscribe" => Method::Subscribe,
152 "tx" => Method::Tx,
153 "tx_search" => Method::TxSearch,
154 "unsubscribe" => Method::Unsubscribe,
155 "validators" => Method::Validators,
156 other => return Err(Error::method_not_found(other.to_string())),
157 })
158 }
159}
160
161impl Display for Method {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 write!(f, "{}", self.as_str())
164 }
165}
166
167impl Serialize for Method {
168 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
169 self.as_str().serialize(serializer)
170 }
171}
172
173impl<'de> Deserialize<'de> for Method {
174 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
175 Self::from_str(&String::deserialize(deserializer)?)
176 .map_err(|e| D::Error::custom(format!("{e}")))
177 }
178}