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

use crate::{NoteView, Spend};

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

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

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

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

impl From<SpendView> for pbt::SpendView {
    fn from(v: SpendView) -> Self {
        use pbt::spend_view as sv;
        match v {
            SpendView::Visible { spend, note } => Self {
                spend_view: Some(sv::SpendView::Visible(sv::Visible {
                    spend: Some(spend.into()),
                    note: Some(note.into()),
                })),
            },
            SpendView::Opaque { spend } => Self {
                spend_view: Some(sv::SpendView::Opaque(sv::Opaque {
                    spend: Some(spend.into()),
                })),
            },
        }
    }
}

impl From<SpendView> for Spend {
    fn from(v: SpendView) -> Self {
        match v {
            SpendView::Visible { spend, note: _ } => spend,
            SpendView::Opaque { spend } => spend,
        }
    }
}