catbuffer_rust/
account_type_dto.rs

1/*
2 * // Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
3 * // Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
4 * // All rights reserved.
5 * //
6 * // This file is part of Catapult.
7 * //
8 * // Catapult is free software: you can redistribute it and/or modify
9 * // it under the terms of the GNU Lesser General Public License as published by
10 * // the Free Software Foundation, either version 3 of the License, or
11 * // (at your option) any later version.
12 * //
13 * // Catapult is distributed in the hope that it will be useful,
14 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * // GNU Lesser General Public License for more details.
17 * //
18 * // You should have received a copy of the GNU Lesser General Public License
19 * // along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22use num_derive::{FromPrimitive, ToPrimitive};
23use num_traits::{FromPrimitive, ToPrimitive};
24use strum_macros::EnumIter;
25
26use super::generator_utils::*;
27
28/// Enumeration of account types.
29#[allow(non_camel_case_types)]
30#[repr(u8)]
31#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive, ToPrimitive, EnumIter)]
32pub enum AccountTypeDto {
33    /// Account is not linked to another account.
34    UNLINKED = 0,
35
36    /// Account is a balance-holding account that is linked to a remote harvester account.
37    MAIN = 1,
38
39    /// Account is a remote harvester account that is linked to a balance-holding account.
40    REMOTE = 2,
41
42    /// Account is a remote harvester eligible account that is unlinked \note this allows an account that has previously been used as remote to be reused as a remote.
43    REMOTE_UNLINKED = 3,
44
45}
46
47impl AccountTypeDto {
48    pub const LENGTH: usize = std::mem::size_of::<Self>();
49
50    /// Gets the size of the type.
51    ///
52    /// # Returns
53    ///
54    /// A usize.
55    pub fn get_size(&self) -> usize {
56        Self::LENGTH
57    }
58
59    /// Gets the value of the enum.
60    ///
61    /// # Returns
62    ///
63    /// A u8
64    pub fn get_value(&self) -> u8 {
65        self.to_u8().unwrap()
66    }
67
68
69    /// Creates an `AccountTypeDto` from a slice.
70    ///
71    /// # Returns
72    ///
73    /// A `AccountTypeDto`.
74    pub fn from_binary(src: &[u8]) -> Self {
75        // assert_eq!(src.len(), Self::LENGTH);
76        let buf = fixed_bytes::<{ Self::LENGTH }>(src);
77        Self::from_u8(u8::from_le_bytes(buf)).unwrap()
78    }
79
80    /// Serializes an type to bytes.
81    ///
82    /// # Returns
83    ///
84    /// A Serialized bytes.
85    pub fn serializer(&self) -> Vec<u8> {
86        self.get_value().to_le_bytes().to_vec()
87    }
88}