1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::*;
use std::collections::BTreeMap;
use crate::serialization::utils::is_break_tag;

impl cbor_event::se::Serialize for VotingProcedures {
    fn serialize<'se, W: Write>(
        &self,
        serializer: &'se mut Serializer<W>,
    ) -> cbor_event::Result<&'se mut Serializer<W>> {
        serializer.write_map(cbor_event::Len::Len(self.0.len() as u64))?;
        for (voter, votes) in &self.0 {
            if votes.is_empty() {
                continue;
            }
            voter.serialize(serializer)?;
            serializer.write_map(cbor_event::Len::Len(votes.len() as u64))?;
            for (governance_action_id, voting_procedure) in votes {
                governance_action_id.serialize(serializer)?;
                voting_procedure.serialize(serializer)?;
            }
        }
        Ok(serializer)
    }
}

impl Deserialize for VotingProcedures {
    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
        let mut voter_to_vote = BTreeMap::new();
        (|| -> Result<_, DeserializeError> {
            let len = raw.map()?;
            let mut total = 0;
            while match len {
                cbor_event::Len::Len(n) => total < n,
                cbor_event::Len::Indefinite => true,
            } {
                if is_break_tag(raw, "voting_procedure map")? {
                    break;
                }

                let key = Voter::deserialize(raw).map_err(|e| e.annotate("voter"))?;

                let value = deserialize_internal_map(raw)
                    .map_err(|e| e.annotate("voting_procedure map"))?;

                if voter_to_vote.insert(key.clone(), value).is_some() {
                    return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
                        "some complicated/unsupported type",
                    )))
                    .into());
                }
                total += 1;
            }
            Ok(Self(voter_to_vote))
        })()
        .map_err(|e| e.annotate("VotingProcedures"))
    }
}

fn deserialize_internal_map<R: BufRead + Seek>(
    raw: &mut Deserializer<R>,
) -> Result<BTreeMap<GovernanceActionId, VotingProcedure>, DeserializeError> {
    let mut gov_act_id_to_vote = BTreeMap::new();
    (|| -> Result<_, DeserializeError> {
        let len = raw.map()?;
        let mut total = 0;
        while match len {
            cbor_event::Len::Len(n) => total < n,
            cbor_event::Len::Indefinite => true,
        } {
            if is_break_tag(raw, "gov_act_id_to_vote map")? {
                break;
            }

            let key = GovernanceActionId::deserialize(raw).map_err(|e| e.annotate("gov_act_id"))?;

            let value =
                VotingProcedure::deserialize(raw).map_err(|e| e.annotate("voting_procedure"))?;

            if gov_act_id_to_vote.insert(key.clone(), value).is_some() {
                return Err(DeserializeFailure::DuplicateKey(Key::Str(String::from(
                    "some complicated/unsupported type",
                )))
                .into());
            }
            total += 1;
        }
        Ok(gov_act_id_to_vote)
    })()
    .map_err(|e| e.annotate("VotingProcedures (gov_act_id to vote_procedure map)"))
}