amaru_kernel/cardano/
ballot.rs1use crate::{Anchor, Vote, cbor};
16
17#[derive(Clone, Debug, PartialEq)]
18pub struct Ballot {
19 vote: Vote,
20
21 anchor: Option<Box<Anchor>>,
34}
35
36impl Ballot {
37 pub fn new(vote: Vote, anchor: Option<Anchor>) -> Self {
38 Self { vote, anchor: anchor.map(Box::new) }
39 }
40
41 pub fn vote(&self) -> &Vote {
42 &self.vote
43 }
44
45 pub fn anchor(&self) -> Option<&Anchor> {
46 self.anchor.as_deref()
47 }
48}
49
50impl<C> cbor::encode::Encode<C> for Ballot {
51 fn encode<W: cbor::encode::Write>(
52 &self,
53 e: &mut cbor::Encoder<W>,
54 ctx: &mut C,
55 ) -> Result<(), cbor::encode::Error<W::Error>> {
56 e.array(2)?;
57 e.encode_with(self.vote(), ctx)?;
58 e.encode_with(self.anchor(), ctx)?;
59 Ok(())
60 }
61}
62
63impl<'d, C> cbor::decode::Decode<'d, C> for Ballot {
64 fn decode(d: &mut cbor::Decoder<'d>, ctx: &mut C) -> Result<Self, cbor::decode::Error> {
65 cbor::heterogeneous_array(d, |d, assert_len| {
66 assert_len(2)?;
67 Ok(Self { vote: d.decode_with(ctx)?, anchor: d.decode_with(ctx)? })
68 })
69 }
70}
71
72#[cfg(any(test, feature = "test-utils"))]
73pub use tests::*;
74
75#[cfg(any(test, feature = "test-utils"))]
76mod tests {
77 use proptest::{option, prelude::*};
78
79 use super::Ballot;
80 use crate::{any_anchor, any_vote, prop_cbor_roundtrip};
81
82 prop_compose! {
83 pub fn any_ballot()(
84 vote in any_vote(),
85 anchor in option::of(any_anchor()),
86 ) -> Ballot {
87 Ballot::new(vote, anchor)
88 }
89 }
90
91 prop_cbor_roundtrip!(Ballot, any_ballot());
92}