cometbft_rpc/dialect/
v0_34.rs

1use cometbft::{abci, evidence};
2use cometbft_proto::types::v1beta1::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_to_string"
48    )]
49    pub key: String,
50    /// The event value.
51    #[serde(
52        serialize_with = "base64string::serialize",
53        deserialize_with = "base64string::deserialize_to_string"
54    )]
55    pub value: String,
56    /// Whether CometBFT's indexer should index this event.
57    ///
58    /// **This field is nondeterministic**.
59    pub index: bool,
60}
61
62impl From<EventAttribute> for abci::EventAttribute {
63    fn from(msg: EventAttribute) -> Self {
64        Self {
65            key: msg.key,
66            value: msg.value,
67            index: msg.index,
68        }
69    }
70}
71
72impl From<abci::EventAttribute> for EventAttribute {
73    fn from(msg: abci::EventAttribute) -> Self {
74        Self {
75            key: msg.key,
76            value: msg.value,
77            index: msg.index,
78        }
79    }
80}
81
82#[derive(Clone, Debug, Serialize, Deserialize)]
83#[serde(into = "RawEvidence", try_from = "RawEvidence")]
84pub struct Evidence(evidence::Evidence);
85
86impl From<Evidence> for RawEvidence {
87    fn from(evidence: Evidence) -> Self {
88        evidence.0.into()
89    }
90}
91
92impl TryFrom<RawEvidence> for Evidence {
93    type Error = <evidence::Evidence as TryFrom<RawEvidence>>::Error;
94
95    fn try_from(value: RawEvidence) -> Result<Self, Self::Error> {
96        Ok(Self(evidence::Evidence::try_from(value)?))
97    }
98}
99
100impl From<evidence::Evidence> for Evidence {
101    fn from(evidence: evidence::Evidence) -> Self {
102        Self(evidence)
103    }
104}