catbuffer_rust/
account_restriction_flags_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::IntoEnumIterator;
25use strum_macros::EnumIter;
26
27use super::generator_utils::*;
28
29/// Enumeration of account restriction flags.
30#[allow(non_camel_case_types)]
31#[repr(u16)]
32#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive, ToPrimitive, EnumIter)]
33pub enum AccountRestrictionFlagsDto {
34    /// Restriction type is an address.
35    ADDRESS = 1,
36
37    /// Restriction type is a mosaic identifier.
38    MOSAIC_ID = 2,
39
40    /// Restriction type is a transaction type.
41    TRANSACTION_TYPE = 4,
42
43    /// Restriction is interpreted as outgoing.
44    OUTGOING = 16384,
45
46    /// Restriction is interpreted as blocking (instead of allowing) operation.
47    BLOCK = 32768,
48
49}
50
51impl AccountRestrictionFlagsDto {
52    pub const LENGTH: usize = std::mem::size_of::<Self>();
53
54    /// Gets the size of the type.
55    ///
56    /// # Returns
57    ///
58    /// A usize.
59    pub fn get_size(&self) -> usize {
60        Self::LENGTH
61    }
62
63    /// Gets the value of the enum.
64    ///
65    /// # Returns
66    ///
67    /// A u16
68    pub fn get_value(&self) -> u16 {
69        self.to_u16().unwrap()
70    }
71
72
73    /// Converts a bit representation to a vec of AccountRestrictionFlagsDto.
74    ///
75    /// # Returns
76    ///
77    /// A vec to AccountRestrictionFlagsDto flags representing the int value.
78    pub fn bytes_to_flags(bit_mask_value: &[u8]) -> Vec<AccountRestrictionFlagsDto> {
79        use std::convert::TryFrom;
80        let bit_mask_value = <[u8; Self::LENGTH]>::try_from(&bit_mask_value[..Self::LENGTH]).unwrap();
81        Self::int_to_flags(u16::from_le_bytes(bit_mask_value))
82    }
83
84    /// Converts a bit representation to a vec of AccountRestrictionFlagsDto.
85    ///
86    /// # Returns
87    ///
88    /// A vec to AccountRestrictionFlagsDto flags representing the int value.
89    pub fn int_to_flags(bit_mask_value: u16) -> Vec<AccountRestrictionFlagsDto> {
90        let mut results: Vec<AccountRestrictionFlagsDto> = vec![];
91        for flag in AccountRestrictionFlagsDto::iter() {
92            if 0 != flag.get_value() & bit_mask_value {
93                results.push(flag);
94            }
95        }
96        results
97    }
98
99    /// Converts a vec of AccountRestrictionFlagsDto to a bit representation.
100    ///
101    /// # Returns
102    ///
103    /// A u16 value of the vec of flags
104    pub fn flags_to_int(flags: Vec<AccountRestrictionFlagsDto>) -> u16 {
105        let mut result: u16 = 0;
106        for flag in AccountRestrictionFlagsDto::iter() {
107            if flags.iter().any(|&i| i == flag) {
108                result += flag.get_value();
109            }
110        }
111        result
112    }
113
114    /// Creates an `AccountRestrictionFlagsDto` from a slice.
115    ///
116    /// # Returns
117    ///
118    /// A `AccountRestrictionFlagsDto`.
119    pub fn from_binary(src: &[u8]) -> Self {
120        // assert_eq!(src.len(), Self::LENGTH);
121        let buf = fixed_bytes::<{ Self::LENGTH }>(src);
122        Self::from_u16(u16::from_le_bytes(buf)).unwrap()
123    }
124
125    /// Serializes an type to bytes.
126    ///
127    /// # Returns
128    ///
129    /// A Serialized bytes.
130    pub fn serializer(&self) -> Vec<u8> {
131        self.get_value().to_le_bytes().to_vec()
132    }
133}