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

use crate::NoteView;
use crate::Output;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "pbt::OutputView", into = "pbt::OutputView")]
#[allow(clippy::large_enum_variant)]
pub enum OutputView {
    Visible {
        output: Output,
        note: NoteView,
        payload_key: PayloadKey,
    },
    Opaque {
        output: Output,
    },
}

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

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

    fn try_from(v: pbt::OutputView) -> Result<Self, Self::Error> {
        match v
            .output_view
            .ok_or_else(|| anyhow::anyhow!("missing output field"))?
        {
            pbt::output_view::OutputView::Visible(x) => Ok(OutputView::Visible {
                output: x
                    .output
                    .ok_or_else(|| anyhow::anyhow!("missing output field"))?
                    .try_into()?,
                note: x
                    .note
                    .ok_or_else(|| anyhow::anyhow!("missing note field"))?
                    .try_into()?,
                payload_key: x
                    .payload_key
                    .ok_or_else(|| anyhow::anyhow!("missing payload key field"))?
                    .inner
                    .as_slice()
                    .try_into()?,
            }),
            pbt::output_view::OutputView::Opaque(x) => Ok(OutputView::Opaque {
                output: x
                    .output
                    .ok_or_else(|| anyhow::anyhow!("missing output field"))?
                    .try_into()?,
            }),
        }
    }
}

impl From<OutputView> for pbt::OutputView {
    fn from(v: OutputView) -> Self {
        use pbt::output_view as ov;
        match v {
            OutputView::Visible {
                output,
                note,
                payload_key,
            } => Self {
                output_view: Some(ov::OutputView::Visible(ov::Visible {
                    output: Some(output.into()),
                    note: Some(note.into()),
                    payload_key: Some(payload_key.into()),
                })),
            },
            OutputView::Opaque { output } => Self {
                output_view: Some(ov::OutputView::Opaque(ov::Opaque {
                    output: Some(output.into()),
                })),
            },
        }
    }
}

impl From<OutputView> for Output {
    fn from(v: OutputView) -> Self {
        match v {
            OutputView::Visible {
                output,
                payload_key: _,
                note: _,
            } => output,
            OutputView::Opaque { output } => output,
        }
    }
}