cardano_serialization_lib/serialization/governance/
voting_procedures.rs

1use crate::*;
2use std::collections::BTreeMap;
3use crate::serialization::utils::is_break_tag;
4
5impl cbor_event::se::Serialize for VotingProcedures {
6    fn serialize<'se, W: Write>(
7        &self,
8        serializer: &'se mut Serializer<W>,
9    ) -> cbor_event::Result<&'se mut Serializer<W>> {
10        serializer.write_map(cbor_event::Len::Len(self.0.len() as u64))?;
11        for (voter, votes) in &self.0 {
12            if votes.is_empty() {
13                continue;
14            }
15            voter.serialize(serializer)?;
16            serializer.write_map(cbor_event::Len::Len(votes.len() as u64))?;
17            for (governance_action_id, voting_procedure) in votes {
18                governance_action_id.serialize(serializer)?;
19                voting_procedure.serialize(serializer)?;
20            }
21        }
22        Ok(serializer)
23    }
24}
25
26impl Deserialize for VotingProcedures {
27    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
28        let mut voter_to_vote = BTreeMap::new();
29        (|| -> Result<_, DeserializeError> {
30            let len = raw.map()?;
31            let mut total = 0;
32            while match len {
33                cbor_event::Len::Len(n) => total < n,
34                cbor_event::Len::Indefinite => true,
35            } {
36                if is_break_tag(raw, "voting_procedure map")? {
37                    break;
38                }
39
40                let key = Voter::deserialize(raw).map_err(|e| e.annotate("voter"))?;
41
42                let value = deserialize_internal_map(raw)
43                    .map_err(|e| e.annotate("voting_procedure map"))?;
44
45                if voter_to_vote.insert(key.clone(), value).is_some() {
46                    return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
47                        "some complicated/unsupported type",
48                    )))
49                    .into());
50                }
51                total += 1;
52            }
53            Ok(Self(voter_to_vote))
54        })()
55        .map_err(|e| e.annotate("VotingProcedures"))
56    }
57}
58
59fn deserialize_internal_map<R: BufRead + Seek>(
60    raw: &mut Deserializer<R>,
61) -> Result<BTreeMap<GovernanceActionId, VotingProcedure>, DeserializeError> {
62    let mut gov_act_id_to_vote = BTreeMap::new();
63    (|| -> Result<_, DeserializeError> {
64        let len = raw.map()?;
65        let mut total = 0;
66        while match len {
67            cbor_event::Len::Len(n) => total < n,
68            cbor_event::Len::Indefinite => true,
69        } {
70            if is_break_tag(raw, "gov_act_id_to_vote map")? {
71                break;
72            }
73
74            let key = GovernanceActionId::deserialize(raw).map_err(|e| e.annotate("gov_act_id"))?;
75
76            let value =
77                VotingProcedure::deserialize(raw).map_err(|e| e.annotate("voting_procedure"))?;
78
79            if gov_act_id_to_vote.insert(key.clone(), value).is_some() {
80                return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
81                    "some complicated/unsupported type",
82                )))
83                .into());
84            }
85            total += 1;
86        }
87        Ok(gov_act_id_to_vote)
88    })()
89    .map_err(|e| e.annotate("VotingProcedures (gov_act_id to vote_procedure map)"))
90}