dpp/voting/contender_structs/contender/
mod.rs1pub mod v0;
2
3use crate::data_contract::document_type::DocumentTypeRef;
4use crate::data_contract::DataContract;
5use crate::document::Document;
6#[cfg(feature = "json-conversion")]
7use crate::serialization::JsonConvertible;
8#[cfg(feature = "value-conversion")]
9use crate::serialization::ValueConvertible;
10use crate::serialization::{PlatformDeserializable, PlatformSerializable};
11use crate::voting::contender_structs::contender::v0::ContenderV0;
12use crate::voting::contender_structs::ContenderWithSerializedDocumentV0;
13use crate::ProtocolError;
14use bincode::{Decode, Encode};
15use derive_more::From;
16use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
17use platform_value::Identifier;
18use platform_version::version::PlatformVersion;
19
20#[derive(Debug, PartialEq, Clone, From)]
25pub enum Contender {
26 V0(ContenderV0),
28}
29
30#[cfg_attr(
36 all(feature = "json-conversion", feature = "serde-conversion"),
37 derive(JsonConvertible)
38)]
39#[derive(
40 Debug, PartialEq, Eq, Clone, From, Encode, Decode, PlatformSerialize, PlatformDeserialize,
41)]
42#[cfg_attr(
43 feature = "serde-conversion",
44 derive(serde::Serialize, serde::Deserialize),
45 serde(tag = "$formatVersion")
46)]
47#[cfg_attr(feature = "value-conversion", derive(ValueConvertible))]
48#[platform_serialize(unversioned)]
49pub enum ContenderWithSerializedDocument {
50 #[cfg_attr(feature = "serde-conversion", serde(rename = "0"))]
52 V0(ContenderWithSerializedDocumentV0),
53}
54
55impl Contender {
56 pub fn identity_id(&self) -> Identifier {
57 match self {
58 Contender::V0(v0) => v0.identity_id,
59 }
60 }
61
62 pub fn identity_id_ref(&self) -> &Identifier {
63 match self {
64 Contender::V0(v0) => &v0.identity_id,
65 }
66 }
67
68 pub fn document(&self) -> &Option<Document> {
69 match self {
70 Contender::V0(v0) => &v0.document,
71 }
72 }
73
74 pub fn take_document(&mut self) -> Option<Document> {
75 match self {
76 Contender::V0(v0) => v0.document.take(),
77 }
78 }
79
80 pub fn vote_tally(&self) -> Option<u32> {
81 match self {
82 Contender::V0(v0) => v0.vote_tally,
83 }
84 }
85}
86
87impl ContenderWithSerializedDocument {
88 pub fn identity_id(&self) -> Identifier {
89 match self {
90 ContenderWithSerializedDocument::V0(v0) => v0.identity_id,
91 }
92 }
93
94 pub fn identity_id_ref(&self) -> &Identifier {
95 match self {
96 ContenderWithSerializedDocument::V0(v0) => &v0.identity_id,
97 }
98 }
99
100 pub fn serialized_document(&self) -> &Option<Vec<u8>> {
101 match self {
102 ContenderWithSerializedDocument::V0(v0) => &v0.serialized_document,
103 }
104 }
105
106 pub fn take_serialized_document(&mut self) -> Option<Vec<u8>> {
107 match self {
108 ContenderWithSerializedDocument::V0(v0) => v0.serialized_document.take(),
109 }
110 }
111
112 pub fn vote_tally(&self) -> Option<u32> {
113 match self {
114 ContenderWithSerializedDocument::V0(v0) => v0.vote_tally,
115 }
116 }
117}
118
119impl ContenderWithSerializedDocument {
120 pub fn try_into_contender(
121 self,
122 document_type_ref: DocumentTypeRef,
123 platform_version: &PlatformVersion,
124 ) -> Result<Contender, ProtocolError> {
125 match self {
126 ContenderWithSerializedDocument::V0(v0) => Ok(v0
127 .try_into_contender(document_type_ref, platform_version)?
128 .into()),
129 }
130 }
131
132 pub fn try_to_contender(
133 &self,
134 document_type_ref: DocumentTypeRef,
135 platform_version: &PlatformVersion,
136 ) -> Result<Contender, ProtocolError> {
137 match self {
138 ContenderWithSerializedDocument::V0(v0) => Ok(v0
139 .try_to_contender(document_type_ref, platform_version)?
140 .into()),
141 }
142 }
143}
144
145impl Contender {
146 pub fn try_into_contender_with_serialized_document(
147 self,
148 document_type_ref: DocumentTypeRef,
149 data_contract: &DataContract,
150 platform_version: &PlatformVersion,
151 ) -> Result<ContenderWithSerializedDocument, ProtocolError> {
152 match self {
153 Contender::V0(v0) => Ok(v0
154 .try_into_contender_with_serialized_document(
155 document_type_ref,
156 data_contract,
157 platform_version,
158 )?
159 .into()),
160 }
161 }
162
163 pub fn try_to_contender_with_serialized_document(
164 &self,
165 document_type_ref: DocumentTypeRef,
166 data_contract: &DataContract,
167 platform_version: &PlatformVersion,
168 ) -> Result<ContenderWithSerializedDocument, ProtocolError> {
169 match self {
170 Contender::V0(v0) => Ok(v0
171 .try_to_contender_with_serialized_document(
172 document_type_ref,
173 data_contract,
174 platform_version,
175 )?
176 .into()),
177 }
178 }
179
180 pub fn serialize(
181 &self,
182 document_type: DocumentTypeRef,
183 data_contract: &DataContract,
184 platform_version: &PlatformVersion,
185 ) -> Result<Vec<u8>, ProtocolError> {
186 self.try_to_contender_with_serialized_document(
187 document_type,
188 data_contract,
189 platform_version,
190 )?
191 .serialize_to_bytes()
192 }
193
194 pub fn serialize_consume(
195 self,
196 document_type: DocumentTypeRef,
197 data_contract: &DataContract,
198 platform_version: &PlatformVersion,
199 ) -> Result<Vec<u8>, ProtocolError> {
200 self.try_into_contender_with_serialized_document(
201 document_type,
202 data_contract,
203 platform_version,
204 )?
205 .serialize_to_bytes()
206 }
207
208 pub fn from_bytes(
209 serialized_contender: &[u8],
210 document_type: DocumentTypeRef,
211 platform_version: &PlatformVersion,
212 ) -> Result<Self, ProtocolError>
213 where
214 Self: Sized,
215 {
216 let serialized_contender =
217 ContenderWithSerializedDocument::deserialize_from_bytes(serialized_contender)?;
218 serialized_contender.try_into_contender(document_type, platform_version)
219 }
220}