catbuffer_rust/mosaic_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 mosaic property flags.
30#[allow(non_camel_case_types)]
31#[repr(u8)]
32#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive, ToPrimitive, EnumIter)]
33pub enum MosaicFlagsDto {
34 /// No flags present.
35 NONE = 0,
36
37 /// Mosaic supports supply changes even when mosaic owner owns partial supply.
38 SUPPLY_MUTABLE = 1,
39
40 /// Mosaic supports transfers between arbitrary accounts \note when not set, mosaic can only be transferred to and from mosaic owner.
41 TRANSFERABLE = 2,
42
43 /// Mosaic supports custom restrictions configured by mosaic owner.
44 RESTRICTABLE = 4,
45
46}
47
48impl MosaicFlagsDto {
49 pub const LENGTH: usize = std::mem::size_of::<Self>();
50
51 /// Gets the size of the type.
52 ///
53 /// # Returns
54 ///
55 /// A usize.
56 pub fn get_size(&self) -> usize {
57 Self::LENGTH
58 }
59
60 /// Gets the value of the enum.
61 ///
62 /// # Returns
63 ///
64 /// A u8
65 pub fn get_value(&self) -> u8 {
66 self.to_u8().unwrap()
67 }
68
69
70 /// Converts a bit representation to a vec of MosaicFlagsDto.
71 ///
72 /// # Returns
73 ///
74 /// A vec to MosaicFlagsDto flags representing the int value.
75 pub fn bytes_to_flags(bit_mask_value: &[u8]) -> Vec<MosaicFlagsDto> {
76 use std::convert::TryFrom;
77 let bit_mask_value = <[u8; Self::LENGTH]>::try_from(&bit_mask_value[..Self::LENGTH]).unwrap();
78 Self::int_to_flags(u8::from_le_bytes(bit_mask_value))
79 }
80
81 /// Converts a bit representation to a vec of MosaicFlagsDto.
82 ///
83 /// # Returns
84 ///
85 /// A vec to MosaicFlagsDto flags representing the int value.
86 pub fn int_to_flags(bit_mask_value: u8) -> Vec<MosaicFlagsDto> {
87 let mut results: Vec<MosaicFlagsDto> = vec![];
88 for flag in MosaicFlagsDto::iter() {
89 if 0 != flag.get_value() & bit_mask_value {
90 results.push(flag);
91 }
92 }
93 results
94 }
95
96 /// Converts a vec of MosaicFlagsDto to a bit representation.
97 ///
98 /// # Returns
99 ///
100 /// A u8 value of the vec of flags
101 pub fn flags_to_int(flags: Vec<MosaicFlagsDto>) -> u8 {
102 let mut result: u8 = 0;
103 for flag in MosaicFlagsDto::iter() {
104 if flags.iter().any(|&i| i == flag) {
105 result += flag.get_value();
106 }
107 }
108 result
109 }
110
111 /// Creates an `MosaicFlagsDto` from a slice.
112 ///
113 /// # Returns
114 ///
115 /// A `MosaicFlagsDto`.
116 pub fn from_binary(src: &[u8]) -> Self {
117 // assert_eq!(src.len(), Self::LENGTH);
118 let buf = fixed_bytes::<{ Self::LENGTH }>(src);
119 Self::from_u8(u8::from_le_bytes(buf)).unwrap()
120 }
121
122 /// Serializes an type to bytes.
123 ///
124 /// # Returns
125 ///
126 /// A Serialized bytes.
127 pub fn serializer(&self) -> Vec<u8> {
128 self.get_value().to_le_bytes().to_vec()
129 }
130}