1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use alloc::vec::Vec;
use bitflags::bitflags;
use crate::bytesrepr;
pub const ACCESS_RIGHTS_SERIALIZED_LENGTH: usize = 1;
bitflags! {
#[allow(clippy::derive_hash_xor_eq)]
pub struct AccessRights: u8 {
const NONE = 0;
const READ = 0b001;
const WRITE = 0b010;
const ADD = 0b100;
const READ_ADD = Self::READ.bits | Self::ADD.bits;
const READ_WRITE = Self::READ.bits | Self::WRITE.bits;
const ADD_WRITE = Self::ADD.bits | Self::WRITE.bits;
const READ_ADD_WRITE = Self::READ.bits | Self::ADD.bits | Self::WRITE.bits;
}
}
impl AccessRights {
pub fn is_readable(self) -> bool {
self & AccessRights::READ == AccessRights::READ
}
pub fn is_writeable(self) -> bool {
self & AccessRights::WRITE == AccessRights::WRITE
}
pub fn is_addable(self) -> bool {
self & AccessRights::ADD == AccessRights::ADD
}
pub fn is_none(self) -> bool {
self == AccessRights::NONE
}
}
impl core::fmt::Display for AccessRights {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match *self {
AccessRights::NONE => write!(f, "NONE"),
AccessRights::READ => write!(f, "READ"),
AccessRights::WRITE => write!(f, "WRITE"),
AccessRights::ADD => write!(f, "ADD"),
AccessRights::READ_ADD => write!(f, "READ_ADD"),
AccessRights::READ_WRITE => write!(f, "READ_WRITE"),
AccessRights::ADD_WRITE => write!(f, "ADD_WRITE"),
AccessRights::READ_ADD_WRITE => write!(f, "READ_ADD_WRITE"),
_ => write!(f, "UNKNOWN"),
}
}
}
impl bytesrepr::ToBytes for AccessRights {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
self.bits.to_bytes()
}
fn serialized_length(&self) -> usize {
ACCESS_RIGHTS_SERIALIZED_LENGTH
}
}
impl bytesrepr::FromBytes for AccessRights {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (id, rem) = u8::from_bytes(bytes)?;
match AccessRights::from_bits(id) {
Some(rights) => Ok((rights, rem)),
None => Err(bytesrepr::Error::Formatting),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_readable(right: AccessRights, is_true: bool) {
assert_eq!(right.is_readable(), is_true)
}
#[test]
fn test_is_readable() {
test_readable(AccessRights::READ, true);
test_readable(AccessRights::READ_ADD, true);
test_readable(AccessRights::READ_WRITE, true);
test_readable(AccessRights::READ_ADD_WRITE, true);
test_readable(AccessRights::ADD, false);
test_readable(AccessRights::ADD_WRITE, false);
test_readable(AccessRights::WRITE, false);
}
fn test_writable(right: AccessRights, is_true: bool) {
assert_eq!(right.is_writeable(), is_true)
}
#[test]
fn test_is_writable() {
test_writable(AccessRights::WRITE, true);
test_writable(AccessRights::READ_WRITE, true);
test_writable(AccessRights::ADD_WRITE, true);
test_writable(AccessRights::READ, false);
test_writable(AccessRights::ADD, false);
test_writable(AccessRights::READ_ADD, false);
test_writable(AccessRights::READ_ADD_WRITE, true);
}
fn test_addable(right: AccessRights, is_true: bool) {
assert_eq!(right.is_addable(), is_true)
}
#[test]
fn test_is_addable() {
test_addable(AccessRights::ADD, true);
test_addable(AccessRights::READ_ADD, true);
test_addable(AccessRights::READ_WRITE, false);
test_addable(AccessRights::ADD_WRITE, true);
test_addable(AccessRights::READ, false);
test_addable(AccessRights::WRITE, false);
test_addable(AccessRights::READ_ADD_WRITE, true);
}
}