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
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::types::error::Error;

use bee_common::packable::{Packable, Read, Write};
use bee_message::{output, payload::milestone::MilestoneId};

/// Records the creation of a treasury output.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TreasuryOutput {
    inner: output::TreasuryOutput,
    milestone_id: MilestoneId,
}

impl TreasuryOutput {
    /// Creates a new `TreasuryOutput`.
    pub fn new(inner: output::TreasuryOutput, milestone_id: MilestoneId) -> Self {
        Self { inner, milestone_id }
    }

    /// Returns the inner output of a `TreasuryOutput`.
    pub fn inner(&self) -> &output::TreasuryOutput {
        &self.inner
    }

    /// Returns the id of the milestone that created the `TreasuryOutput`.
    pub fn milestone_id(&self) -> &MilestoneId {
        &self.milestone_id
    }
}

impl Packable for TreasuryOutput {
    type Error = Error;

    fn packed_len(&self) -> usize {
        self.inner.packed_len() + self.milestone_id.packed_len()
    }

    fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
        self.inner.pack(writer)?;
        self.milestone_id.pack(writer)?;

        Ok(())
    }

    fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
        let inner = output::TreasuryOutput::unpack_inner::<R, CHECK>(reader)?;
        let milestone_id = MilestoneId::unpack_inner::<R, CHECK>(reader)?;

        Ok(Self::new(inner, milestone_id))
    }
}