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(buf.len()));
67        }
68
69        buf[0] |= self.bits();
70
71        Ok(1)
72    }
73
74    pub fn decode(buf: &[u8]) -> Result<Self, AcnError> {
75        if buf.is_empty() {
76            return Err(AcnError::InvalidBufferLength(0));
77        }
78
79        Ok(Self::new(buf[0]))
80    }
81}
82
83impl BitAnd for Flags {
84    type Output = Self;
85
86    fn bitand(self, rhs: Self) -> Self::Output {
87        Self::new(self.bits() & rhs.bits())
88    }
89}
90
91impl BitAnd<u8> for Flags {
92    type Output = Self;
93
94    fn bitand(self, rhs: u8) -> Self::Output {
95        Self::new(self.bits() & rhs)
96    }
97}
98
99impl BitAnd<Flags> for u8 {
100    type Output = Flags;
101
102    fn bitand(self, rhs: Flags) -> Self::Output {
103        Flags::new(self & rhs.bits())
104    }
105}
106
107impl BitAndAssign for Flags {
108    fn bitand_assign(&mut self, rhs: Self) {
109        self.0 &= rhs.bits();
110    }
111}
112
113impl BitAndAssign<u8> for Flags {
114    fn bitand_assign(&mut self, rhs: u8) {
115        self.0 &= rhs;
116    }
117}
118
119impl BitAndAssign<Flags> for u8 {
120    fn bitand_assign(&mut self, rhs: Flags) {
121        *self &= rhs.bits();
122    }
123}
124
125impl BitOr for Flags {
126    type Output = Self;
127
128    fn bitor(self, rhs: Self) -> Self::Output {
129        Self::new(self.bits() | rhs.bits())
130    }
131}
132
133impl BitOr<u8> for Flags {
134    type Output = Self;
135
136    fn bitor(self, rhs: u8) -> Self::Output {
137        Self::new(self.bits() | rhs)
138    }
139}
140
141impl BitOr<Flags> for u8 {
142    type Output = Flags;
143
144    fn bitor(self, rhs: Flags) -> Self::Output {
145        Flags::new(self | rhs.bits())
146    }
147}
148
149impl BitOrAssign for Flags {
150    fn bitor_assign(&mut self, rhs: Self) {
151        self.0 |= rhs.bits();
152    }
153}
154
155impl BitOrAssign<u8> for Flags {
156    fn bitor_assign(&mut self, rhs: u8) {
157        self.0 |= rhs;
158    }
159}
160
161impl BitOrAssign<Flags> for u8 {
162    fn bitor_assign(&mut self, rhs: Flags) {
163        *self |= rhs.bits();
164    }
165}
166
167impl From<u8> for Flags {
168    fn from(flags: u8) -> Self {
169        Self::new(flags)
170    }
171}
172
173impl From<Flags> for u8 {
174    fn from(flags: Flags) -> Self {
175        flags.bits()
176    }
177}