catbuffer_rust/
link_action_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 link actions.
29#[allow(non_camel_case_types)]
30#[repr(u8)]
31#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive, ToPrimitive, EnumIter)]
32pub enum LinkActionDto {
33    /// Unlink account.
34    UNLINK = 0,
35
36    /// Link account.
37    LINK = 1,
38
39}
40
41impl LinkActionDto {
42    pub const LENGTH: usize = std::mem::size_of::<Self>();
43
44    /// Gets the size of the type.
45    ///
46    /// # Returns
47    ///
48    /// A usize.
49    pub fn get_size(&self) -> usize {
50        Self::LENGTH
51    }
52
53    /// Gets the value of the enum.
54    ///
55    /// # Returns
56    ///
57    /// A u8
58    pub fn get_value(&self) -> u8 {
59        self.to_u8().unwrap()
60    }
61
62
63    /// Creates an `LinkActionDto` from a slice.
64    ///
65    /// # Returns
66    ///
67    /// A `LinkActionDto`.
68    pub fn from_binary(src: &[u8]) -> Self {
69        // assert_eq!(src.len(), Self::LENGTH);
70        let buf = fixed_bytes::<{ Self::LENGTH }>(src);
71        Self::from_u8(u8::from_le_bytes(buf)).unwrap()
72    }
73
74    /// Serializes an type to bytes.
75    ///
76    /// # Returns
77    ///
78    /// A Serialized bytes.
79    pub fn serializer(&self) -> Vec<u8> {
80        self.get_value().to_le_bytes().to_vec()
81    }
82}