dpp/voting/contender_structs/contender/v0/
mod.rs

1use crate::data_contract::document_type::DocumentTypeRef;
2use crate::data_contract::DataContract;
3use crate::document::serialization_traits::DocumentPlatformConversionMethodsV0;
4use crate::document::Document;
5#[cfg(feature = "json-conversion")]
6use crate::serialization::json_safe_fields;
7use crate::ProtocolError;
8use bincode::{Decode, Encode};
9use platform_value::Identifier;
10use platform_version::version::PlatformVersion;
11
12/// Represents a contender in the contested document vote poll.
13///
14/// This struct holds the identity ID of the contender, the serialized document,
15/// and the vote tally.
16#[derive(Debug, PartialEq, Clone, Default)]
17pub struct ContenderV0 {
18    /// The identity ID of the contender.
19    pub identity_id: Identifier,
20    /// The document associated with the contender.
21    pub document: Option<Document>,
22    /// The vote tally for the contender.
23    pub vote_tally: Option<u32>,
24}
25
26/// Represents a contender in the contested document vote poll.
27/// This is for internal use where the document is in serialized form
28///
29/// This struct holds the identity ID of the contender, the serialized document,
30/// and the vote tally.
31#[cfg_attr(feature = "json-conversion", json_safe_fields)]
32#[derive(Debug, PartialEq, Eq, Clone, Default, Encode, Decode)]
33#[cfg_attr(
34    feature = "serde-conversion",
35    derive(serde::Serialize, serde::Deserialize),
36    serde(rename_all = "camelCase")
37)]
38pub struct ContenderWithSerializedDocumentV0 {
39    /// The identity ID of the contender.
40    pub identity_id: Identifier,
41    /// The serialized document associated with the contender.
42    pub serialized_document: Option<Vec<u8>>,
43    /// The vote tally for the contender.
44    pub vote_tally: Option<u32>,
45}
46
47impl ContenderV0 {
48    pub fn try_into_contender_with_serialized_document(
49        self,
50        document_type_ref: DocumentTypeRef,
51        data_contract: &DataContract,
52        platform_version: &PlatformVersion,
53    ) -> Result<ContenderWithSerializedDocumentV0, ProtocolError> {
54        let ContenderV0 {
55            identity_id,
56            document,
57            vote_tally,
58        } = self;
59
60        Ok(ContenderWithSerializedDocumentV0 {
61            identity_id,
62            serialized_document: document
63                .map(|document| {
64                    document.serialize(document_type_ref, data_contract, platform_version)
65                })
66                .transpose()?,
67            vote_tally,
68        })
69    }
70
71    pub fn try_to_contender_with_serialized_document(
72        &self,
73        document_type_ref: DocumentTypeRef,
74        data_contract: &DataContract,
75        platform_version: &PlatformVersion,
76    ) -> Result<ContenderWithSerializedDocumentV0, ProtocolError> {
77        let ContenderV0 {
78            identity_id,
79            document,
80            vote_tally,
81        } = self;
82
83        Ok(ContenderWithSerializedDocumentV0 {
84            identity_id: *identity_id,
85            serialized_document: document
86                .as_ref()
87                .map(|document| {
88                    document.serialize(document_type_ref, data_contract, platform_version)
89                })
90                .transpose()?,
91            vote_tally: *vote_tally,
92        })
93    }
94}
95
96impl ContenderWithSerializedDocumentV0 {
97    pub fn try_into_contender(
98        self,
99        document_type_ref: DocumentTypeRef,
100        platform_version: &PlatformVersion,
101    ) -> Result<ContenderV0, ProtocolError> {
102        let ContenderWithSerializedDocumentV0 {
103            identity_id,
104            serialized_document,
105            vote_tally,
106        } = self;
107
108        Ok(ContenderV0 {
109            identity_id,
110            document: serialized_document
111                .map(|document| {
112                    Document::from_bytes(document.as_slice(), document_type_ref, platform_version)
113                })
114                .transpose()?,
115            vote_tally,
116        })
117    }
118
119    pub fn try_to_contender(
120        &self,
121        document_type_ref: DocumentTypeRef,
122        platform_version: &PlatformVersion,
123    ) -> Result<ContenderV0, ProtocolError> {
124        let ContenderWithSerializedDocumentV0 {
125            identity_id,
126            serialized_document,
127            vote_tally,
128        } = self;
129
130        Ok(ContenderV0 {
131            identity_id: *identity_id,
132            document: serialized_document
133                .as_ref()
134                .map(|document| {
135                    Document::from_bytes(document.as_slice(), document_type_ref, platform_version)
136                })
137                .transpose()?,
138            vote_tally: *vote_tally,
139        })
140    }
141}