acn_protocol/
flags.rs

1use crate::error::AcnError;
2use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
3
4#[derive(Copy, Clone, Debug, Default, PartialEq)]
5pub struct Flags(u8);
6
7impl Flags {
8    /// If bit 8 is set, the length is extended by an extra byte.
9    pub const EXTENDED_LENGTH_BIT_MASK: u8 = 0b10000000;
10    /// If bit 7 is set, the pdu contains a vector segment
11    pub const INCLUDES_VECTOR_BIT_MASK: u8 = 0b01000000;
12    /// If bit 6 is set, the pdu contains a header segment
13    pub const INCLUDES_HEADER_BIT_MASK: u8 = 0b00100000;
14    /// If bit 5 is set, the pdu contains a data segment
15    pub const INCLUDES_DATA_BIT_MASK: u8 = 0b00010000;
16
17    pub const ALL_BIT_MASK: u8 = Self::EXTENDED_LENGTH_BIT_MASK
18        | Self::INCLUDES_VECTOR_BIT_MASK
19        | Self::INCLUDES_HEADER_BIT_MASK
20        | Self::INCLUDES_DATA_BIT_MASK;
21
22    pub const EXTENDED_LENGTH: Self = Self(Self::EXTENDED_LENGTH_BIT_MASK);
23
24    pub const INCLUDES_VECTOR: Self = Self(Self::INCLUDES_VECTOR_BIT_MASK);
25
26    pub const INCLUDES_HEADER: Self = Self(Self::INCLUDES_HEADER_BIT_MASK);
27
28    pub const INCLUDES_DATA: Self = Self(Self::INCLUDES_DATA_BIT_MASK);
29
30    pub const ALL: Self = Self(Self::ALL_BIT_MASK);
31
32    pub const fn new(flags: u8) -> Self {
33        Self(flags & Self::ALL_BIT_MASK)
34    }
35
36    pub const fn empty() -> Self {
37        Self(0)
38    }
39
40    pub const fn bits(&self) -> u8 {
41        self.0
42    }
43
44    pub const fn is_extended_length(&self) -> bool {
45        self.0 & Self::EXTENDED_LENGTH_BIT_MASK != 0
46    }
47
48    pub const fn is_includes_vector(&self) -> bool {
49        self.0 & Self::INCLUDES_VECTOR_BIT_MASK != 0
50    }
51
52    pub const fn is_includes_header(&self) -> bool {
53        self.0 & Self::INCLUDES_HEADER_BIT_MASK != 0
54    }
55
56    pub const fn is_includes_data(&self) -> bool {
57        self.0 & Self::INCLUDES_DATA_BIT_MASK != 0
58    }
59
60    pub const fn is_all(&self) -> bool {
61        self.0 == Self::ALL_BIT_MASK
62    }
63
64    pub fn encode(&self, buf: &mut [u8]) -> Result<usize, AcnError> {
65        if buf.is_empty() {
66            return Err(AcnError::InvalidBufferLength {
67                actual: 0,
68                expected: 1,
69            });
70        }
71
72        buf[0] |= self.bits();
73
74        Ok(1)
75    }
76
77    pub fn decode(buf: &[u8]) -> Result<Self, AcnError> {
78        if buf.is_empty() {
79            return Err(AcnError::InvalidBufferLength {
80                actual: 0,
81                expected: 1,
82            });
83        }
84
85        Ok(Self::new(buf[0]))
86    }
87}
88
89impl BitAnd for Flags {
90    type Output = Self;
91
92    fn bitand(self, rhs: Self) -> Self::Output {
93        Self::new(self.bits() & rhs.bits())
94    }
95}
96
97impl BitAnd<u8> for Flags {
98    type Output = Self;
99
100    fn bitand(self, rhs: u8) -> Self::Output {
101        Self::new(self.bits() & rhs)
102    }
103}
104
105impl BitAnd<Flags> for u8 {
106    type Output = Flags;
107
108    fn bitand(self, rhs: Flags) -> Self::Output {
109        Flags::new(self & rhs.bits())
110    }
111}
112
113impl BitAndAssign for Flags {
114    fn bitand_assign(&mut self, rhs: Self) {
115        self.0 &= rhs.bits();
116    }
117}
118
119impl BitAndAssign<u8> for Flags {
120    fn bitand_assign(&mut self, rhs: u8) {
121        self.0 &= rhs;
122    }
123}
124
125impl BitAndAssign<Flags> for u8 {
126    fn bitand_assign(&mut self, rhs: Flags) {
127        *self &= rhs.bits();
128    }
129}
130
131impl BitOr for Flags {
132    type Output = Self;
133
134    fn bitor(self, rhs: Self) -> Self::Output {
135        Self::new(self.bits() | rhs.bits())
136    }
137}
138
139impl BitOr<u8> for Flags {
140    type Output = Self;
141
142    fn bitor(self, rhs: u8) -> Self::Output {
143        Self::new(self.bits() | rhs)
144    }
145}
146
147impl BitOr<Flags> for u8 {
148    type Output = Flags;
149
150    fn bitor(self, rhs: Flags) -> Self::Output {
151        Flags::new(self | rhs.bits())
152    }
153}
154
155impl BitOrAssign for Flags {
156    fn bitor_assign(&mut self, rhs: Self) {
157        self.0 |= rhs.bits();
158    }
159}
160
161impl BitOrAssign<u8> for Flags {
162    fn bitor_assign(&mut self, rhs: u8) {
163        self.0 |= rhs;
164    }
165}
166
167impl BitOrAssign<Flags> for u8 {
168    fn bitor_assign(&mut self, rhs: Flags) {
169        *self |= rhs.bits();
170    }
171}
172
173impl From<u8> for Flags {
174    fn from(flags: u8) -> Self {
175        Self::new(flags)
176    }
177}
178
179impl From<Flags> for u8 {
180    fn from(flags: Flags) -> Self {
181        flags.bits()
182    }
183}