tendermint/v0_34/abci/
response.rs

1pub use crate::abci::response::{
2    ApplySnapshotChunk, BeginBlock, CheckTx, Commit, DeliverTx, Echo, EndBlock, Exception, Info,
3    InitChain, ListSnapshots, LoadSnapshotChunk, OfferSnapshot, Query, SetOption,
4};
5use crate::Error;
6
7/// All possible ABCI responses for this protocol version.
8#[derive(Clone, PartialEq, Eq, Debug)]
9pub enum Response {
10    #[doc = include_str!("../../abci/doc/response-exception.md")]
11    Exception(Exception),
12    #[doc = include_str!("../../abci/doc/response-echo.md")]
13    Echo(Echo),
14    #[doc = include_str!("../../abci/doc/response-flush.md")]
15    Flush,
16    #[doc = include_str!("../../abci/doc/response-info.md")]
17    Info(Info),
18    #[doc = include_str!("../../abci/doc/response-setoption.md")]
19    SetOption(SetOption),
20    #[doc = include_str!("../../abci/doc/response-initchain.md")]
21    InitChain(InitChain),
22    #[doc = include_str!("../../abci/doc/response-query.md")]
23    Query(Query),
24    #[doc = include_str!("../../abci/doc/response-beginblock.md")]
25    BeginBlock(BeginBlock),
26    #[doc = include_str!("../../abci/doc/response-checktx.md")]
27    CheckTx(CheckTx),
28    #[doc = include_str!("../../abci/doc/response-delivertx.md")]
29    DeliverTx(DeliverTx),
30    #[doc = include_str!("../../abci/doc/response-endblock.md")]
31    EndBlock(EndBlock),
32    #[doc = include_str!("../../abci/doc/response-commit.md")]
33    Commit(Commit),
34    #[doc = include_str!("../../abci/doc/response-listsnapshots.md")]
35    ListSnapshots(ListSnapshots),
36    #[doc = include_str!("../../abci/doc/response-offersnapshot.md")]
37    OfferSnapshot(OfferSnapshot),
38    #[doc = include_str!("../../abci/doc/response-loadsnapshotchunk.md")]
39    LoadSnapshotChunk(LoadSnapshotChunk),
40    #[doc = include_str!("../../abci/doc/response-applysnapshotchunk.md")]
41    ApplySnapshotChunk(ApplySnapshotChunk),
42}
43
44/// The consensus category of ABCI responses.
45#[derive(Clone, PartialEq, Eq, Debug)]
46pub enum ConsensusResponse {
47    #[doc = include_str!("../../abci/doc/response-initchain.md")]
48    InitChain(InitChain),
49    #[doc = include_str!("../../abci/doc/response-beginblock.md")]
50    BeginBlock(BeginBlock),
51    #[doc = include_str!("../../abci/doc/response-delivertx.md")]
52    DeliverTx(DeliverTx),
53    #[doc = include_str!("../../abci/doc/response-endblock.md")]
54    EndBlock(EndBlock),
55    #[doc = include_str!("../../abci/doc/response-commit.md")]
56    Commit(Commit),
57}
58
59/// The mempool category of ABCI responses.
60#[derive(Clone, PartialEq, Eq, Debug)]
61pub enum MempoolResponse {
62    #[doc = include_str!("../../abci/doc/response-checktx.md")]
63    CheckTx(CheckTx),
64}
65
66/// The info category of ABCI responses.
67#[derive(Clone, PartialEq, Eq, Debug)]
68pub enum InfoResponse {
69    #[doc = include_str!("../../abci/doc/response-echo.md")]
70    Echo(Echo),
71    #[doc = include_str!("../../abci/doc/response-info.md")]
72    Info(Info),
73    #[doc = include_str!("../../abci/doc/response-query.md")]
74    Query(Query),
75    #[doc = include_str!("../../abci/doc/response-setoption.md")]
76    SetOption(SetOption),
77}
78
79/// The snapshot category of ABCI responses.
80#[derive(Clone, PartialEq, Eq, Debug)]
81pub enum SnapshotResponse {
82    #[doc = include_str!("../../abci/doc/response-listsnapshots.md")]
83    ListSnapshots(ListSnapshots),
84    #[doc = include_str!("../../abci/doc/response-offersnapshot.md")]
85    OfferSnapshot(OfferSnapshot),
86    #[doc = include_str!("../../abci/doc/response-loadsnapshotchunk.md")]
87    LoadSnapshotChunk(LoadSnapshotChunk),
88    #[doc = include_str!("../../abci/doc/response-applysnapshotchunk.md")]
89    ApplySnapshotChunk(ApplySnapshotChunk),
90}
91
92impl From<ConsensusResponse> for Response {
93    fn from(req: ConsensusResponse) -> Self {
94        match req {
95            ConsensusResponse::InitChain(x) => Self::InitChain(x),
96            ConsensusResponse::BeginBlock(x) => Self::BeginBlock(x),
97            ConsensusResponse::DeliverTx(x) => Self::DeliverTx(x),
98            ConsensusResponse::EndBlock(x) => Self::EndBlock(x),
99            ConsensusResponse::Commit(x) => Self::Commit(x),
100        }
101    }
102}
103
104impl TryFrom<Response> for ConsensusResponse {
105    type Error = Error;
106    fn try_from(req: Response) -> Result<Self, Self::Error> {
107        match req {
108            Response::InitChain(x) => Ok(Self::InitChain(x)),
109            Response::BeginBlock(x) => Ok(Self::BeginBlock(x)),
110            Response::DeliverTx(x) => Ok(Self::DeliverTx(x)),
111            Response::EndBlock(x) => Ok(Self::EndBlock(x)),
112            Response::Commit(x) => Ok(Self::Commit(x)),
113            _ => Err(Error::invalid_abci_response_type()),
114        }
115    }
116}
117
118impl From<MempoolResponse> for Response {
119    fn from(req: MempoolResponse) -> Self {
120        match req {
121            MempoolResponse::CheckTx(x) => Self::CheckTx(x),
122        }
123    }
124}
125
126impl TryFrom<Response> for MempoolResponse {
127    type Error = Error;
128    fn try_from(req: Response) -> Result<Self, Self::Error> {
129        match req {
130            Response::CheckTx(x) => Ok(Self::CheckTx(x)),
131            _ => Err(Error::invalid_abci_response_type()),
132        }
133    }
134}
135
136impl From<InfoResponse> for Response {
137    fn from(req: InfoResponse) -> Self {
138        match req {
139            InfoResponse::Echo(x) => Self::Echo(x),
140            InfoResponse::Info(x) => Self::Info(x),
141            InfoResponse::Query(x) => Self::Query(x),
142            InfoResponse::SetOption(x) => Self::SetOption(x),
143        }
144    }
145}
146
147impl TryFrom<Response> for InfoResponse {
148    type Error = Error;
149    fn try_from(req: Response) -> Result<Self, Self::Error> {
150        match req {
151            Response::Echo(x) => Ok(Self::Echo(x)),
152            Response::Info(x) => Ok(Self::Info(x)),
153            Response::Query(x) => Ok(Self::Query(x)),
154            Response::SetOption(x) => Ok(Self::SetOption(x)),
155            _ => Err(Error::invalid_abci_response_type()),
156        }
157    }
158}
159
160impl From<SnapshotResponse> for Response {
161    fn from(req: SnapshotResponse) -> Self {
162        match req {
163            SnapshotResponse::ListSnapshots(x) => Self::ListSnapshots(x),
164            SnapshotResponse::OfferSnapshot(x) => Self::OfferSnapshot(x),
165            SnapshotResponse::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x),
166            SnapshotResponse::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x),
167        }
168    }
169}
170
171impl TryFrom<Response> for SnapshotResponse {
172    type Error = Error;
173    fn try_from(req: Response) -> Result<Self, Self::Error> {
174        match req {
175            Response::ListSnapshots(x) => Ok(Self::ListSnapshots(x)),
176            Response::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)),
177            Response::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)),
178            Response::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)),
179            _ => Err(Error::invalid_abci_response_type()),
180        }
181    }
182}
183
184// =============================================================================
185// Protobuf conversions
186// =============================================================================
187
188use tendermint_proto::v0_34::abci as pb;
189use tendermint_proto::Protobuf;
190
191impl From<Response> for pb::Response {
192    fn from(response: Response) -> pb::Response {
193        use pb::response::Value;
194        let value = match response {
195            Response::Exception(x) => Some(Value::Exception(x.into())),
196            Response::Echo(x) => Some(Value::Echo(x.into())),
197            Response::Flush => Some(Value::Flush(Default::default())),
198            Response::Info(x) => Some(Value::Info(x.into())),
199            Response::SetOption(x) => Some(Value::SetOption(x.into())),
200            Response::InitChain(x) => Some(Value::InitChain(x.into())),
201            Response::Query(x) => Some(Value::Query(x.into())),
202            Response::BeginBlock(x) => Some(Value::BeginBlock(x.into())),
203            Response::CheckTx(x) => Some(Value::CheckTx(x.into())),
204            Response::DeliverTx(x) => Some(Value::DeliverTx(x.into())),
205            Response::EndBlock(x) => Some(Value::EndBlock(x.into())),
206            Response::Commit(x) => Some(Value::Commit(x.into())),
207            Response::ListSnapshots(x) => Some(Value::ListSnapshots(x.into())),
208            Response::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())),
209            Response::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())),
210            Response::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())),
211        };
212        pb::Response { value }
213    }
214}
215
216impl TryFrom<pb::Response> for Response {
217    type Error = Error;
218
219    fn try_from(response: pb::Response) -> Result<Self, Self::Error> {
220        use pb::response::Value;
221        match response.value {
222            Some(Value::Exception(x)) => Ok(Response::Exception(x.try_into()?)),
223            Some(Value::Echo(x)) => Ok(Response::Echo(x.try_into()?)),
224            Some(Value::Flush(_)) => Ok(Response::Flush),
225            Some(Value::Info(x)) => Ok(Response::Info(x.try_into()?)),
226            Some(Value::SetOption(x)) => Ok(Response::SetOption(x.try_into()?)),
227            Some(Value::InitChain(x)) => Ok(Response::InitChain(x.try_into()?)),
228            Some(Value::Query(x)) => Ok(Response::Query(x.try_into()?)),
229            Some(Value::BeginBlock(x)) => Ok(Response::BeginBlock(x.try_into()?)),
230            Some(Value::CheckTx(x)) => Ok(Response::CheckTx(x.try_into()?)),
231            Some(Value::DeliverTx(x)) => Ok(Response::DeliverTx(x.try_into()?)),
232            Some(Value::EndBlock(x)) => Ok(Response::EndBlock(x.try_into()?)),
233            Some(Value::Commit(x)) => Ok(Response::Commit(x.try_into()?)),
234            Some(Value::ListSnapshots(x)) => Ok(Response::ListSnapshots(x.try_into()?)),
235            Some(Value::OfferSnapshot(x)) => Ok(Response::OfferSnapshot(x.try_into()?)),
236            Some(Value::LoadSnapshotChunk(x)) => Ok(Response::LoadSnapshotChunk(x.try_into()?)),
237            Some(Value::ApplySnapshotChunk(x)) => Ok(Response::ApplySnapshotChunk(x.try_into()?)),
238            None => Err(Error::missing_data()),
239        }
240    }
241}
242
243impl Protobuf<pb::Response> for Response {}