tendermint_rpc/dialect/
v0_34.rs

1use tendermint::{abci, evidence};
2use tendermint_proto::v0_34::types::Evidence as RawEvidence;
3
4use crate::prelude::*;
5use crate::serializers::bytes::base64string;
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Default, Clone)]
10pub struct Dialect;
11
12impl crate::dialect::Dialect for Dialect {
13    type Event = Event;
14    type Evidence = Evidence;
15}
16
17#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
18pub struct Event {
19    #[serde(rename = "type")]
20    pub kind: String,
21    pub attributes: Vec<EventAttribute>,
22}
23
24impl From<Event> for abci::Event {
25    fn from(msg: Event) -> Self {
26        Self {
27            kind: msg.kind,
28            attributes: msg.attributes.into_iter().map(Into::into).collect(),
29        }
30    }
31}
32
33impl From<abci::Event> for Event {
34    fn from(msg: abci::Event) -> Self {
35        Self {
36            kind: msg.kind,
37            attributes: msg.attributes.into_iter().map(Into::into).collect(),
38        }
39    }
40}
41
42#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
43pub struct EventAttribute {
44    /// The event key.
45    #[serde(
46        serialize_with = "base64string::serialize",
47        deserialize_with = "base64string::deserialize"
48    )]
49    pub key: Vec<u8>,
50
51    /// The event value.
52    #[serde(
53        serialize_with = "base64string::serialize",
54        deserialize_with = "base64string::deserialize"
55    )]
56    pub value: Vec<u8>,
57
58    /// Whether Tendermint's indexer should index this event.
59    ///
60    /// **This field is nondeterministic**.
61    pub index: bool,
62}
63
64impl From<EventAttribute> for abci::EventAttribute {
65    fn from(msg: EventAttribute) -> Self {
66        Self::V034(abci::v0_34::EventAttribute {
67            key: msg.key,
68            value: msg.value,
69            index: msg.index,
70        })
71    }
72}
73
74impl From<abci::EventAttribute> for EventAttribute {
75    fn from(msg: abci::EventAttribute) -> Self {
76        Self {
77            key: msg.key_bytes().to_vec(),
78            value: msg.value_bytes().to_vec(),
79            index: msg.index(),
80        }
81    }
82}
83
84#[derive(Clone, Debug, Serialize, Deserialize)]
85#[serde(into = "RawEvidence", try_from = "RawEvidence")]
86pub struct Evidence(evidence::Evidence);
87
88impl From<Evidence> for RawEvidence {
89    fn from(evidence: Evidence) -> Self {
90        evidence.0.into()
91    }
92}
93
94impl TryFrom<RawEvidence> for Evidence {
95    type Error = <evidence::Evidence as TryFrom<RawEvidence>>::Error;
96
97    fn try_from(value: RawEvidence) -> Result<Self, Self::Error> {
98        Ok(Self(evidence::Evidence::try_from(value)?))
99    }
100}
101
102impl From<evidence::Evidence> for Evidence {
103    fn from(evidence: evidence::Evidence) -> Self {
104        Self(evidence)
105    }
106}