selinux/policy/
access_vector.rs1use std::fmt::{Debug, Formatter, LowerHex};
6use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not, Sub, SubAssign};
7use std::str::FromStr;
8
9use fuchsia_rcu::RcuDroppable;
10use selinux_policy_derive::{Parse, Serialize, Validate};
11
12use super::PermissionId;
13use super::traits::PolicyId;
14
15#[derive(
17 Copy,
18 Clone,
19 Default,
20 Eq,
21 Hash,
22 PartialEq,
23 PartialOrd,
24 Ord,
25 RcuDroppable,
26 Parse,
27 Serialize,
28 Validate,
29)]
30pub struct AccessVector(u32);
31
32impl AccessVector {
33 pub const NONE: Self = Self(0);
34 pub const ALL: Self = Self(u32::MAX);
35
36 pub fn value(&self) -> u32 {
38 self.0
39 }
40}
41
42impl From<u32> for AccessVector {
43 fn from(value: u32) -> Self {
44 Self(value)
45 }
46}
47
48impl From<AccessVector> for u32 {
49 fn from(value: AccessVector) -> Self {
50 value.0
51 }
52}
53
54impl Debug for AccessVector {
55 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56 write!(f, "AccessVector({:0>8x})", self.0)
57 }
58}
59
60impl LowerHex for AccessVector {
61 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
62 LowerHex::fmt(&self.0, f)
63 }
64}
65
66impl FromStr for AccessVector {
67 type Err = <u32 as FromStr>::Err;
68
69 fn from_str(value: &str) -> Result<Self, Self::Err> {
70 Ok(Self(u32::from_str_radix(value, 16)?))
72 }
73}
74
75impl BitAnd for AccessVector {
76 type Output = Self;
77
78 fn bitand(self, rhs: Self) -> Self::Output {
79 Self(self.0 & rhs.0)
80 }
81}
82
83impl BitOr for AccessVector {
84 type Output = Self;
85
86 fn bitor(self, rhs: Self) -> Self::Output {
87 Self(self.0 | rhs.0)
88 }
89}
90
91impl BitAndAssign for AccessVector {
92 fn bitand_assign(&mut self, rhs: Self) {
93 self.0 &= rhs.0
94 }
95}
96
97impl BitOrAssign for AccessVector {
98 fn bitor_assign(&mut self, rhs: Self) {
99 self.0 |= rhs.0
100 }
101}
102
103impl SubAssign for AccessVector {
104 fn sub_assign(&mut self, rhs: Self) {
105 self.0 = self.0 ^ (self.0 & rhs.0);
106 }
107}
108
109impl Sub for AccessVector {
110 type Output = Self;
111
112 fn sub(self, rhs: Self) -> Self::Output {
113 Self(self.0 ^ (self.0 & rhs.0))
114 }
115}
116
117impl Not for AccessVector {
118 type Output = Self;
119
120 fn not(self) -> Self {
121 Self(!self.0)
122 }
123}
124
125impl From<PermissionId> for AccessVector {
126 fn from(id: PermissionId) -> Self {
127 Self((1 as u32) << (id.as_u32() - 1))
128 }
129}