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