dpp/voting/contender_structs/
mod.rs1mod contender;
2
3use crate::data_contract::document_type::DocumentTypeRef;
4use crate::document::serialization_traits::DocumentPlatformConversionMethodsV0;
5use crate::document::Document;
6use crate::voting::vote_choices::resource_vote_choice::ResourceVoteChoice;
7use crate::ProtocolError;
8use bincode::{Decode, Encode};
9use platform_value::Identifier;
10use platform_version::version::PlatformVersion;
11use std::fmt;
12
13pub use contender::v0::{ContenderV0, ContenderWithSerializedDocumentV0};
14pub use contender::{Contender, ContenderWithSerializedDocument};
15
16#[derive(Debug, PartialEq, Eq, Clone, Default)]
22pub struct FinalizedContenderWithSerializedDocument {
23 pub identity_id: Identifier,
25 pub serialized_document: Vec<u8>,
27 pub final_vote_tally: u32,
29}
30
31#[derive(Debug, PartialEq, Eq, Clone, Default, Encode, Decode)]
37pub struct FinalizedResourceVoteChoicesWithVoterInfo {
38 pub resource_vote_choice: ResourceVoteChoice,
40 pub voters: Vec<(Identifier, u8)>,
42}
43impl fmt::Display for FinalizedResourceVoteChoicesWithVoterInfo {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 let voters_str: Vec<String> = self
46 .voters
47 .iter()
48 .map(|(id, strength)| format!("{}:{}", id, strength))
49 .collect();
50 write!(
51 f,
52 "FinalizedResourceVoteChoicesWithVoterInfo {{ resource_vote_choice: {}, voters: [{}] }}",
53 self.resource_vote_choice,
54 voters_str.join(", ")
55 )
56 }
57}
58
59#[derive(Debug, PartialEq, Clone)]
65pub struct FinalizedContender {
66 pub identity_id: Identifier,
68 pub document: Document,
70 pub serialized_document: Vec<u8>,
72 pub final_vote_tally: u32,
74}
75
76impl FinalizedContender {
77 pub fn try_from_contender_with_serialized_document(
79 value: FinalizedContenderWithSerializedDocument,
80 document_type: DocumentTypeRef,
81 platform_version: &PlatformVersion,
82 ) -> Result<Self, ProtocolError> {
83 let FinalizedContenderWithSerializedDocument {
84 identity_id,
85 serialized_document,
86 final_vote_tally,
87 } = value;
88
89 Ok(FinalizedContender {
90 identity_id,
91 document: Document::from_bytes(&serialized_document, document_type, platform_version)?,
92 serialized_document,
93 final_vote_tally,
94 })
95 }
96}
97
98impl TryFrom<ContenderWithSerializedDocument> for FinalizedContenderWithSerializedDocument {
99 type Error = ProtocolError;
100
101 fn try_from(value: ContenderWithSerializedDocument) -> Result<Self, Self::Error> {
102 let (identity_id, serialized_document, vote_tally) = match value {
103 ContenderWithSerializedDocument::V0(v0) => {
104 let ContenderWithSerializedDocumentV0 {
105 identity_id,
106 serialized_document,
107 vote_tally,
108 } = v0;
109 (identity_id, serialized_document, vote_tally)
110 }
111 };
112
113 Ok(FinalizedContenderWithSerializedDocument {
114 identity_id,
115 serialized_document: serialized_document.ok_or(
116 ProtocolError::CorruptedCodeExecution("expected serialized document".to_string()),
117 )?,
118 final_vote_tally: vote_tally.ok_or(ProtocolError::CorruptedCodeExecution(
119 "expected vote tally".to_string(),
120 ))?,
121 })
122 }
123}