bee_tangle/
flags.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use bee_common::packable::{Packable, Read, Write};
5
6use bitflags::bitflags;
7use serde::Serialize;
8
9bitflags! {
10    /// Flags representing the state of a message.
11    #[derive(Default, Serialize)]
12    pub struct Flags: u8 {
13        /// The message is solid.
14        const SOLID = 0b0000_0001;
15        /// The message is a milestone.
16        const MILESTONE = 0b0000_0010;
17        /// The message has been referenced by a milestone.
18        const REFERENCED = 0b0000_0100;
19        /// The message is valid.
20        const VALID = 0b0000_1000;
21        /// The message was requested.
22        const REQUESTED = 0b0001_0000;
23    }
24}
25
26impl Flags {
27    /// Return whether the flags indicate that the message is solid.
28    pub fn is_solid(&self) -> bool {
29        self.contains(Flags::SOLID)
30    }
31
32    /// Set the solid flag for this message.
33    pub fn set_solid(&mut self, is_solid: bool) {
34        self.set(Flags::SOLID, is_solid);
35    }
36
37    /// Return whether the flags indicate that the message is a milestone.
38    pub fn is_milestone(&self) -> bool {
39        self.contains(Flags::MILESTONE)
40    }
41
42    /// Set the milestone flag for this message.
43    pub fn set_milestone(&mut self, is_milestone: bool) {
44        self.set(Flags::MILESTONE, is_milestone);
45    }
46
47    /// Return whether the flags indicate that the message is referenced.
48    pub fn is_referenced(&self) -> bool {
49        self.contains(Flags::REFERENCED)
50    }
51
52    /// Set the referenced flag for this message.
53    pub fn set_referenced(&mut self, is_referenced: bool) {
54        self.set(Flags::REFERENCED, is_referenced);
55    }
56
57    /// Return whether the flags indicate that the message is valid.
58    pub fn is_valid(&self) -> bool {
59        self.contains(Flags::VALID)
60    }
61
62    /// Set the valid flag for this message.
63    pub fn set_valid(&mut self, is_valid: bool) {
64        self.set(Flags::VALID, is_valid);
65    }
66
67    /// Return whether the flags indicate that the message was requested.
68    pub fn was_requested(&self) -> bool {
69        self.contains(Flags::REQUESTED)
70    }
71
72    /// Set the valid flag for this message.
73    pub fn set_requested(&mut self, was_requested: bool) {
74        self.set(Flags::REQUESTED, was_requested);
75    }
76}
77
78impl Packable for Flags {
79    type Error = std::io::Error;
80
81    fn packed_len(&self) -> usize {
82        self.bits().packed_len()
83    }
84
85    fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
86        self.bits().pack(writer)
87    }
88
89    fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
90        // Flags are only expected to be unpacked from a trusted storage source.
91        Ok(unsafe { Self::from_bits_unchecked(u8::unpack_inner::<R, CHECK>(reader)?) })
92    }
93}