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 GenesisChunked,
61
62 Header,
64
65 HeaderByHash,
67
68 Health,
70
71 NetInfo,
73
74 Status,
76
77 Tx,
79
80 TxSearch,
82
83 Validators,
85
86 Subscribe,
88
89 Unsubscribe,
91
92 BroadcastEvidence,
94}
95
96impl Method {
97 pub fn as_str(self) -> &'static str {
99 match self {
100 Method::AbciInfo => "abci_info",
101 Method::AbciQuery => "abci_query",
102 Method::Block => "block",
103 Method::BlockByHash => "block_by_hash",
104 Method::BlockResults => "block_results",
105 Method::BlockSearch => "block_search",
106 Method::Blockchain => "blockchain",
107 Method::BroadcastEvidence => "broadcast_evidence",
108 Method::BroadcastTxAsync => "broadcast_tx_async",
109 Method::BroadcastTxSync => "broadcast_tx_sync",
110 Method::BroadcastTxCommit => "broadcast_tx_commit",
111 Method::Commit => "commit",
112 Method::ConsensusParams => "consensus_params",
113 Method::ConsensusState => "consensus_state",
114 Method::Genesis => "genesis",
115 Method::GenesisChunked => "genesis_chunked",
116 Method::Header => "header",
117 Method::HeaderByHash => "header_by_hash",
118 Method::Health => "health",
119 Method::NetInfo => "net_info",
120 Method::Status => "status",
121 Method::Subscribe => "subscribe",
122 Method::Tx => "tx",
123 Method::TxSearch => "tx_search",
124 Method::Unsubscribe => "unsubscribe",
125 Method::Validators => "validators",
126 }
127 }
128}
129
130impl FromStr for Method {
131 type Err = Error;
132
133 fn from_str(s: &str) -> Result<Self, Error> {
134 Ok(match s {
135 "abci_info" => Method::AbciInfo,
136 "abci_query" => Method::AbciQuery,
137 "block" => Method::Block,
138 "block_by_hash" => Method::BlockByHash,
139 "block_results" => Method::BlockResults,
140 "header" => Method::Header,
141 "header_by_hash" => Method::HeaderByHash,
142 "block_search" => Method::BlockSearch,
143 "blockchain" => Method::Blockchain,
144 "broadcast_evidence" => Method::BroadcastEvidence,
145 "broadcast_tx_async" => Method::BroadcastTxAsync,
146 "broadcast_tx_sync" => Method::BroadcastTxSync,
147 "broadcast_tx_commit" => Method::BroadcastTxCommit,
148 "commit" => Method::Commit,
149 "consensus_params" => Method::ConsensusParams,
150 "consensus_state" => Method::ConsensusState,
151 "genesis" => Method::Genesis,
152 "health" => Method::Health,
153 "net_info" => Method::NetInfo,
154 "status" => Method::Status,
155 "subscribe" => Method::Subscribe,
156 "tx" => Method::Tx,
157 "tx_search" => Method::TxSearch,
158 "unsubscribe" => Method::Unsubscribe,
159 "validators" => Method::Validators,
160 other => return Err(Error::method_not_found(other.to_string())),
161 })
162 }
163}
164
165impl Display for Method {
166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 write!(f, "{}", self.as_str())
168 }
169}
170
171impl Serialize for Method {
172 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
173 self.as_str().serialize(serializer)
174 }
175}
176
177impl<'de> Deserialize<'de> for Method {
178 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
179 Self::from_str(&String::deserialize(deserializer)?)
180 .map_err(|e| D::Error::custom(format!("{e}")))
181 }
182}