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

use core::str::FromStr;

use derive_more::{Deref, From};

use crate::block::{payload::milestone::MilestoneId, Error};

/// [`TreasuryInput`] is an input which references a milestone which generated a
/// [`TreasuryOutput`](crate::block::output::TreasuryOutput).
#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, From, Deref, packable::Packable)]
pub struct TreasuryInput(MilestoneId);

impl TreasuryInput {
    /// The input kind of a [`TreasuryInput`].
    pub const KIND: u8 = 1;

    /// Creates a new [`TreasuryInput`].
    pub fn new(id: MilestoneId) -> Self {
        Self(id)
    }

    /// Returns the milestones id of a [`TreasuryInput`].
    pub fn milestone_id(&self) -> &MilestoneId {
        &self.0
    }
}

#[cfg(feature = "serde")]
string_serde_impl!(TreasuryInput);

impl FromStr for TreasuryInput {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(TreasuryInput(MilestoneId::from_str(s)?))
    }
}

impl core::fmt::Display for TreasuryInput {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl core::fmt::Debug for TreasuryInput {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "TreasuryInput({})", self.0)
    }
}

#[cfg(feature = "dto")]
#[allow(missing_docs)]
pub mod dto {
    use serde::{Deserialize, Serialize};

    use super::*;
    use crate::block::error::dto::DtoError;

    /// Describes an input which references an unspent treasury output to consume.
    #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
    pub struct TreasuryInputDto {
        #[serde(rename = "type")]
        pub kind: u8,
        #[serde(rename = "milestoneId")]
        pub milestone_id: String,
    }

    impl From<&TreasuryInput> for TreasuryInputDto {
        fn from(value: &TreasuryInput) -> Self {
            TreasuryInputDto {
                kind: TreasuryInput::KIND,
                milestone_id: value.milestone_id().to_string(),
            }
        }
    }

    impl TryFrom<&TreasuryInputDto> for TreasuryInput {
        type Error = DtoError;

        fn try_from(value: &TreasuryInputDto) -> Result<Self, Self::Error> {
            Ok(TreasuryInput::new(
                value
                    .milestone_id
                    .parse::<MilestoneId>()
                    .map_err(|_| DtoError::InvalidField("milestoneId"))?,
            ))
        }
    }
}