bee_block/unlock/
alias.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{unlock::UnlockIndex, Error};
5
6/// Points to the unlock of a consumed alias output.
7#[derive(Clone, Debug, Eq, PartialEq, Hash, packable::Packable)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[packable(unpack_error = Error, with = Error::InvalidAliasIndex)]
10pub struct AliasUnlock(
11    /// Index of input and unlock corresponding to an [`AliasOutput`](crate::output::AliasOutput).
12    UnlockIndex,
13);
14
15impl TryFrom<u16> for AliasUnlock {
16    type Error = Error;
17
18    fn try_from(index: u16) -> Result<Self, Self::Error> {
19        Self::new(index)
20    }
21}
22
23impl AliasUnlock {
24    /// The [`Unlock`](crate::unlock::Unlock) kind of an [`AliasUnlock`].
25    pub const KIND: u8 = 2;
26
27    /// Creates a new [`AliasUnlock`].
28    #[inline(always)]
29    pub fn new(index: u16) -> Result<Self, Error> {
30        index.try_into().map(Self).map_err(Error::InvalidAliasIndex)
31    }
32
33    /// Return the index of an [`AliasUnlock`].
34    #[inline(always)]
35    pub fn index(&self) -> u16 {
36        self.0.get()
37    }
38}
39
40#[cfg(feature = "dto")]
41#[allow(missing_docs)]
42pub mod dto {
43    use serde::{Deserialize, Serialize};
44
45    /// Points to the unlock of a consumed alias output.
46    #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
47    pub struct AliasUnlockDto {
48        #[serde(rename = "type")]
49        pub kind: u8,
50        #[serde(rename = "reference")]
51        pub index: u16,
52    }
53}