penumbra_governance/delegator_vote/
view.rs

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
use penumbra_proto::{core::component::governance::v1 as pbt, DomainType};
use penumbra_shielded_pool::NoteView;
use serde::{Deserialize, Serialize};

use crate::DelegatorVote;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "pbt::DelegatorVoteView", into = "pbt::DelegatorVoteView")]
#[allow(clippy::large_enum_variant)]
pub enum DelegatorVoteView {
    Visible {
        delegator_vote: DelegatorVote,
        note: NoteView,
    },
    Opaque {
        delegator_vote: DelegatorVote,
    },
}

impl DomainType for DelegatorVoteView {
    type Proto = pbt::DelegatorVoteView;
}

impl TryFrom<pbt::DelegatorVoteView> for DelegatorVoteView {
    type Error = anyhow::Error;

    fn try_from(v: pbt::DelegatorVoteView) -> Result<Self, Self::Error> {
        match v
            .delegator_vote
            .ok_or_else(|| anyhow::anyhow!("missing delegator vote field"))?
        {
            pbt::delegator_vote_view::DelegatorVote::Visible(x) => Ok(DelegatorVoteView::Visible {
                delegator_vote: x
                    .delegator_vote
                    .ok_or_else(|| anyhow::anyhow!("missing delegator vote field"))?
                    .try_into()?,
                note: x
                    .note
                    .ok_or_else(|| anyhow::anyhow!("missing note field"))?
                    .try_into()?,
            }),
            pbt::delegator_vote_view::DelegatorVote::Opaque(x) => Ok(DelegatorVoteView::Opaque {
                delegator_vote: x
                    .delegator_vote
                    .ok_or_else(|| anyhow::anyhow!("missing spend field"))?
                    .try_into()?,
            }),
        }
    }
}

impl From<DelegatorVoteView> for pbt::DelegatorVoteView {
    fn from(v: DelegatorVoteView) -> Self {
        use pbt::delegator_vote_view as dvv;
        match v {
            DelegatorVoteView::Visible {
                delegator_vote,
                note,
            } => Self {
                delegator_vote: Some(dvv::DelegatorVote::Visible(dvv::Visible {
                    delegator_vote: Some(delegator_vote.into()),
                    note: Some(note.into()),
                })),
            },
            DelegatorVoteView::Opaque { delegator_vote } => Self {
                delegator_vote: Some(dvv::DelegatorVote::Opaque(dvv::Opaque {
                    delegator_vote: Some(delegator_vote.into()),
                })),
            },
        }
    }
}

impl From<DelegatorVoteView> for DelegatorVote {
    fn from(v: DelegatorVoteView) -> Self {
        match v {
            DelegatorVoteView::Visible {
                delegator_vote,
                note: _,
            } => delegator_vote,
            DelegatorVoteView::Opaque { delegator_vote } => delegator_vote,
        }
    }
}