bee_block/input/
mod.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4mod treasury;
5mod utxo;
6
7use core::ops::RangeInclusive;
8
9use derive_more::From;
10
11pub use self::{treasury::TreasuryInput, utxo::UtxoInput};
12use crate::Error;
13
14/// The maximum number of inputs of a transaction.
15pub const INPUT_COUNT_MAX: u16 = 128;
16/// The range of valid numbers of inputs of a transaction.
17pub const INPUT_COUNT_RANGE: RangeInclusive<u16> = 1..=INPUT_COUNT_MAX; // [1..128]
18/// The maximum index of inputs of a transaction.
19pub const INPUT_INDEX_MAX: u16 = INPUT_COUNT_MAX - 1; // 127
20/// The range of valid indices of inputs of a transaction.
21pub const INPUT_INDEX_RANGE: RangeInclusive<u16> = 0..=INPUT_INDEX_MAX; // [0..127]
22
23/// A generic input supporting different input kinds.
24#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, From, packable::Packable)]
25#[cfg_attr(
26    feature = "serde",
27    derive(serde::Serialize, serde::Deserialize),
28    serde(tag = "type", content = "data")
29)]
30#[packable(unpack_error = Error)]
31#[packable(tag_type = u8, with_error = Error::InvalidInputKind)]
32pub enum Input {
33    /// A UTXO input.
34    #[packable(tag = UtxoInput::KIND)]
35    Utxo(UtxoInput),
36    /// A treasury input.
37    #[packable(tag = TreasuryInput::KIND)]
38    Treasury(TreasuryInput),
39}
40
41impl Input {
42    /// Returns the input kind of an `Input`.
43    pub fn kind(&self) -> u8 {
44        match self {
45            Self::Utxo(_) => UtxoInput::KIND,
46            Self::Treasury(_) => TreasuryInput::KIND,
47        }
48    }
49}
50
51#[cfg(feature = "dto")]
52#[allow(missing_docs)]
53pub mod dto {
54    use serde::{Deserialize, Serialize};
55
56    use super::*;
57    pub use super::{treasury::dto::TreasuryInputDto, utxo::dto::UtxoInputDto};
58    use crate::error::dto::DtoError;
59
60    /// Describes all the different input types.
61    #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, From)]
62    #[serde(untagged)]
63    pub enum InputDto {
64        Utxo(UtxoInputDto),
65        Treasury(TreasuryInputDto),
66    }
67
68    impl From<&Input> for InputDto {
69        fn from(value: &Input) -> Self {
70            match value {
71                Input::Utxo(u) => InputDto::Utxo(u.into()),
72                Input::Treasury(t) => InputDto::Treasury(t.into()),
73            }
74        }
75    }
76
77    impl TryFrom<&InputDto> for Input {
78        type Error = DtoError;
79
80        fn try_from(value: &InputDto) -> Result<Self, Self::Error> {
81            match value {
82                InputDto::Utxo(u) => Ok(Input::Utxo(u.try_into()?)),
83                InputDto::Treasury(t) => Ok(Input::Treasury(t.try_into()?)),
84            }
85        }
86    }
87}