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
// Copyright The pipewire-rs Contributors.
// SPDX-License-Identifier: MIT

use bitflags::bitflags;
use std::fmt;

bitflags! {
    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    pub struct PermissionFlags: u32 {
        const R = pw_sys::PW_PERM_R;
        const W = pw_sys::PW_PERM_W;
        const X = pw_sys::PW_PERM_X;
        const M = pw_sys::PW_PERM_M;
        #[cfg(feature = "v0_3_77")]
        const L = pw_sys::PW_PERM_L;
    }
}

#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Permission(pw_sys::pw_permission);

impl Permission {
    pub fn new(id: u32, flags: PermissionFlags) -> Self {
        Self(pw_sys::pw_permission {
            id,
            permissions: flags.bits(),
        })
    }

    pub fn id(&self) -> u32 {
        self.0.id
    }

    pub fn set_id(&mut self, id: u32) {
        self.0.id = id;
    }

    pub fn permission_flags(&self) -> PermissionFlags {
        PermissionFlags::from_bits_retain(self.0.permissions)
    }

    pub fn set_permission_flags(&mut self, flags: PermissionFlags) {
        self.0.permissions = flags.bits();
    }
}

impl fmt::Debug for Permission {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Permission")
            .field("id", &self.id())
            .field("permission_flags", &self.permission_flags())
            .finish()
    }
}