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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::serialization::map_names::VotingProposalIndexNames;
use crate::serialization::struct_checks::{check_len_indefinite, serialize_and_check_index};
use crate::*;
use num_traits::{FromPrimitive, ToPrimitive};
use std::io::{Seek, SeekFrom};

impl Serialize for VotingProposal {
    fn serialize<'se, W: Write>(
        &self,
        serializer: &'se mut Serializer<W>,
    ) -> cbor_event::Result<&'se mut Serializer<W>> {
        match &self.0 {
            VotingProposalEnum::ParameterChangeProposal(x) => x.serialize(serializer),
            VotingProposalEnum::HardForkInitiationProposal(x) => x.serialize(serializer),
            VotingProposalEnum::TreasuryWithdrawalsProposal(x) => x.serialize(serializer),
            VotingProposalEnum::NoConfidenceProposal(x) => x.serialize(serializer),
            VotingProposalEnum::NewCommitteeProposal(x) => x.serialize(serializer),
            VotingProposalEnum::NewConstitutionProposal(x) => x.serialize(serializer),
            VotingProposalEnum::InfoProposal(_) => {
                let index = VotingProposalIndexNames::InfoAction.to_u64();
                serialize_and_check_index(serializer, index, "VotingProposalEnum::InfoProposal")
            }
        }
    }
}

impl Deserialize for VotingProposal {
    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
        (|| -> Result<_, DeserializeError> {
            if let Ok(index) = raw.unsigned_integer() {
                let expected_index = VotingProposalIndexNames::InfoAction.to_u64().ok_or(
                    DeserializeFailure::CustomError(
                        "unknown index of VotingProposalEnum::InfoProposal".to_string(),
                    ),
                )?;
                if index != expected_index {
                    return Err(DeserializeFailure::FixedValueMismatch {
                        found: Key::Uint(index),
                        expected: Key::Uint(expected_index),
                    }
                    .into());
                }

                return Ok(Self(VotingProposalEnum::InfoProposal(InfoProposal::new())));
            }

            let len = raw.array()?;
            let ret = Self::deserialize_as_embedded_group(raw, len);
            check_len_indefinite(raw, len)?;
            ret
        })()
        .map_err(|e| e.annotate("VotingProposal"))
    }
}

impl DeserializeEmbeddedGroup for VotingProposal {
    fn deserialize_as_embedded_group<R: BufRead + Seek>(
        raw: &mut Deserializer<R>,
        len: cbor_event::Len,
    ) -> Result<Self, DeserializeError> {
        let cert_index = get_proposal_index(raw)?;
        let index_enum =
            VotingProposalIndexNames::from_u64(cert_index).ok_or(DeserializeError::new(
                "VotingProposal",
                DeserializeFailure::UnknownKey(Key::Uint(cert_index)),
            ))?;

        let proposal_enum = match index_enum {
            VotingProposalIndexNames::ParameterChangeAction => {
                Ok::<VotingProposalEnum, DeserializeError>(
                    VotingProposalEnum::ParameterChangeProposal(
                        ParameterChangeProposal::deserialize_as_embedded_group(raw, len)?,
                    ),
                )
            }
            VotingProposalIndexNames::HardForkInitiationAction => {
                Ok(VotingProposalEnum::HardForkInitiationProposal(
                    HardForkInitiationProposal::deserialize_as_embedded_group(raw, len)?,
                ))
            }
            VotingProposalIndexNames::TreasuryWithdrawalsAction => {
                Ok(VotingProposalEnum::TreasuryWithdrawalsProposal(
                    TreasuryWithdrawalsProposal::deserialize_as_embedded_group(raw, len)?,
                ))
            }
            VotingProposalIndexNames::NoConfidenceAction => {
                Ok(VotingProposalEnum::NoConfidenceProposal(
                    NoConfidenceProposal::deserialize_as_embedded_group(raw, len)?,
                ))
            }
            VotingProposalIndexNames::NewCommitteeAction => {
                Ok(VotingProposalEnum::NewCommitteeProposal(
                    NewCommitteeProposal::deserialize_as_embedded_group(raw, len)?,
                ))
            }
            VotingProposalIndexNames::NewConstitutionAction => {
                Ok(VotingProposalEnum::NewConstitutionProposal(
                    NewConstitutionProposal::deserialize_as_embedded_group(raw, len)?,
                ))
            }
            VotingProposalIndexNames::InfoAction => {
                Ok(VotingProposalEnum::InfoProposal(InfoProposal::new()))
            }
        }?;

        Ok(Self(proposal_enum))
    }
}

fn get_proposal_index<R: BufRead + Seek>(
    raw: &mut Deserializer<R>,
) -> Result<u64, DeserializeError> {
    let initial_position = raw
        .as_mut_ref()
        .seek(SeekFrom::Current(0))
        .map_err(|err| DeserializeFailure::IoError(err.to_string()))?;
    let index = raw.unsigned_integer()?;
    raw.as_mut_ref()
        .seek(SeekFrom::Start(initial_position))
        .map_err(|err| DeserializeFailure::IoError(err.to_string()))?;
    Ok(index)
}