use bitflags::bitflags;
#[derive(Debug, Copy, Clone)]
pub struct KeyPermissions(u32);
#[derive(Debug, Copy, Clone)]
pub struct KeyPermissionsBuilder(KeyPermissions);
bitflags! {
#[repr(transparent)]
pub struct Permission: u8 {
const VIEW = 0x1;
const READ = 0x2;
const WRITE = 0x4;
const SEARCH = 0x8;
const LINK = 0x10;
const SETATTR = 0x20;
const ALL = 0x3f;
}
}
impl Default for KeyPermissions {
fn default() -> Self {
Self::new()
}
}
impl KeyPermissions {
pub fn new() -> Self {
Self(0)
}
pub fn from_u32(raw: u32) -> Self {
Self(raw)
}
pub fn bits(&self) -> u32 {
self.0
}
pub fn set_posessor_perms(&mut self, perm: Permission) {
self.0 &= !(0xFF << 24);
self.0 += (perm.bits() as u32) << 24;
}
pub fn set_user_perms(&mut self, perm: Permission) {
self.0 &= !(0xFF << 16);
self.0 += (perm.bits() as u32) << 16;
}
pub fn set_group_perms(&mut self, perm: Permission) {
self.0 &= !(0xFF << 8);
self.0 += (perm.bits() as u32) << 8;
}
pub fn set_world_perms(&mut self, perm: Permission) {
self.0 &= !0xFF;
self.0 += perm.bits() as u32;
}
}
impl KeyPermissionsBuilder {
pub fn builder() -> Self {
Self(KeyPermissions::default())
}
pub fn posessor(mut self, perm: Permission) -> Self {
self.0.set_posessor_perms(perm);
self
}
pub fn user(mut self, perm: Permission) -> Self {
self.0.set_user_perms(perm);
self
}
pub fn group(mut self, perm: Permission) -> Self {
self.0.set_group_perms(perm);
self
}
pub fn world(mut self, perm: Permission) -> Self {
self.0.set_world_perms(perm);
self
}
pub fn build(self) -> KeyPermissions {
self.0
}
}
#[test]
fn test_posessor_perms() {
let mut perm = KeyPermissions::default();
perm.set_posessor_perms(Permission::ALL);
assert_eq!(perm.0, 0x3f000000);
perm.set_posessor_perms(Permission::SEARCH);
assert_eq!(perm.0, 0x08000000);
perm.set_posessor_perms(Permission::SEARCH | Permission::VIEW);
assert_eq!(perm.0, 0x09000000);
perm.set_posessor_perms(
Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
);
assert_eq!(perm.0, 0x27000000);
}
#[test]
fn test_user_perms() {
let mut perm = KeyPermissions::default();
perm.set_user_perms(Permission::ALL);
assert_eq!(perm.0, 0x003f0000);
perm.set_user_perms(Permission::SEARCH);
assert_eq!(perm.0, 0x00080000);
perm.set_user_perms(Permission::SEARCH | Permission::VIEW);
assert_eq!(perm.0, 0x00090000);
perm.set_user_perms(
Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
);
assert_eq!(perm.0, 0x00270000);
}
#[test]
fn test_group_perms() {
let mut perm = KeyPermissions::default();
perm.set_group_perms(Permission::ALL);
assert_eq!(perm.0, 0x00003f00);
perm.set_group_perms(Permission::SEARCH);
assert_eq!(perm.0, 0x00000800);
perm.set_group_perms(Permission::SEARCH | Permission::VIEW);
assert_eq!(perm.0, 0x00000900);
perm.set_group_perms(
Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
);
assert_eq!(perm.0, 0x00002700);
}
#[test]
fn test_world_perms() {
let mut perm = KeyPermissions::default();
perm.set_world_perms(Permission::ALL);
assert_eq!(perm.0, 0x0000003f);
perm.set_world_perms(Permission::SEARCH);
assert_eq!(perm.0, 0x00000008);
perm.set_world_perms(Permission::SEARCH | Permission::VIEW);
assert_eq!(perm.0, 0x00000009);
perm.set_world_perms(
Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
);
assert_eq!(perm.0, 0x00000027);
}
#[test]
fn test_combined_perms() {
let mut perm = KeyPermissions::default();
perm.set_posessor_perms(Permission::ALL);
assert_eq!(perm.0, 0x3f000000);
perm.set_user_perms(Permission::VIEW | Permission::READ | Permission::WRITE);
assert_eq!(perm.0, 0x3f070000);
perm.set_group_perms(Permission::SEARCH | Permission::VIEW);
assert_eq!(perm.0, 0x3f070900);
perm.set_world_perms(
Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
);
assert_eq!(perm.0, 0x3f070927);
}