Skip to main content

dpp/voting/vote_polls/
mod.rs

1#[cfg(feature = "json-conversion")]
2use crate::serialization::JsonConvertible;
3#[cfg(feature = "value-conversion")]
4use crate::serialization::ValueConvertible;
5use crate::voting::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePoll;
6use crate::ProtocolError;
7use bincode::{Decode, DecodeUntrusted, Encode};
8use derive_more::From;
9use platform_serialization_derive::{
10    PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize,
11};
12use platform_value::Identifier;
13#[cfg(feature = "serde-conversion")]
14use serde::{Deserialize, Serialize};
15use std::fmt;
16
17pub mod contested_document_resource_vote_poll;
18
19#[cfg_attr(
20    all(feature = "json-conversion", feature = "serde-conversion"),
21    derive(JsonConvertible)
22)]
23#[derive(
24    Debug,
25    Clone,
26    Encode,
27    Decode,
28    PlatformSerialize,
29    PlatformDeserializeTrusted,
30    PlatformDeserializeUntrusted,
31    PartialEq,
32    From,
33    DecodeUntrusted,
34)]
35#[cfg_attr(
36    feature = "serde-conversion",
37    derive(Serialize, Deserialize),
38    // Internally tagged with a `$type` discriminator; inner
39    // `ContestedDocumentResourceVotePoll` fields flatten at the same level.
40    serde(tag = "$type", rename_all = "camelCase")
41)]
42#[cfg_attr(feature = "value-conversion", derive(ValueConvertible))]
43#[platform_serialize(unversioned)]
44#[platform_serialize(limit = 100000)]
45pub enum VotePoll {
46    ContestedDocumentResourceVotePoll(ContestedDocumentResourceVotePoll),
47}
48
49impl fmt::Display for VotePoll {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            VotePoll::ContestedDocumentResourceVotePoll(poll) => {
53                write!(f, "ContestedDocumentResourceVotePoll({})", poll)
54            }
55        }
56    }
57}
58
59impl Default for VotePoll {
60    fn default() -> Self {
61        ContestedDocumentResourceVotePoll::default().into()
62    }
63}
64
65impl VotePoll {
66    pub fn specialized_balance_id(&self) -> Result<Option<Identifier>, ProtocolError> {
67        match self {
68            VotePoll::ContestedDocumentResourceVotePoll(contested_document_resource_vote_poll) => {
69                Ok(Some(
70                    contested_document_resource_vote_poll.specialized_balance_id()?,
71                ))
72            }
73        }
74    }
75
76    pub fn unique_id(&self) -> Result<Identifier, ProtocolError> {
77        match self {
78            VotePoll::ContestedDocumentResourceVotePoll(contested_document_resource_vote_poll) => {
79                contested_document_resource_vote_poll.unique_id()
80            }
81        }
82    }
83}
84
85#[cfg(all(
86    test,
87    feature = "json-conversion",
88    feature = "value-conversion",
89    feature = "serde-conversion"
90))]
91mod json_convertible_tests_votepoll {
92    use super::*;
93
94    #[test]
95    fn json_round_trip_votepoll() {
96        use crate::serialization::JsonConvertible;
97        let original = VotePoll::default();
98        let json = original.to_json().expect("to_json");
99        let recovered = VotePoll::from_json(json).expect("from_json");
100        assert_eq!(original, recovered);
101    }
102
103    #[test]
104    fn value_round_trip_votepoll() {
105        use crate::serialization::ValueConvertible;
106        let original = VotePoll::default();
107        let value = original.to_object().expect("to_object");
108        let recovered = VotePoll::from_object(value).expect("from_object");
109        assert_eq!(original, recovered);
110    }
111}