bee_ledger/types/
treasury_output.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::types::error::Error;
5
6use bee_common::packable::{Packable, Read, Write};
7use bee_message::{output, payload::milestone::MilestoneId};
8
9/// Records the creation of a treasury output.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct TreasuryOutput {
12    inner: output::TreasuryOutput,
13    milestone_id: MilestoneId,
14}
15
16impl TreasuryOutput {
17    /// Creates a new `TreasuryOutput`.
18    pub fn new(inner: output::TreasuryOutput, milestone_id: MilestoneId) -> Self {
19        Self { inner, milestone_id }
20    }
21
22    /// Returns the inner output of a `TreasuryOutput`.
23    pub fn inner(&self) -> &output::TreasuryOutput {
24        &self.inner
25    }
26
27    /// Returns the id of the milestone that created the `TreasuryOutput`.
28    pub fn milestone_id(&self) -> &MilestoneId {
29        &self.milestone_id
30    }
31}
32
33impl Packable for TreasuryOutput {
34    type Error = Error;
35
36    fn packed_len(&self) -> usize {
37        self.inner.packed_len() + self.milestone_id.packed_len()
38    }
39
40    fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
41        self.inner.pack(writer)?;
42        self.milestone_id.pack(writer)?;
43
44        Ok(())
45    }
46
47    fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
48        let inner = output::TreasuryOutput::unpack_inner::<R, CHECK>(reader)?;
49        let milestone_id = MilestoneId::unpack_inner::<R, CHECK>(reader)?;
50
51        Ok(Self::new(inner, milestone_id))
52    }
53}