Skip to main content

inspect_core/
capability.rs

1//! Capability flags for inspectable values.
2
3/// Capabilities that an inspectable value supports.
4///
5/// This allows consumers to discover what operations are available
6/// without attempting them and failing.
7#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
8pub struct Capability {
9    bits: u32,
10}
11
12impl Capability {
13    /// No capabilities.
14    pub const NONE: Self = Self { bits: 0 };
15
16    /// Value can be inspected (always true for Inspect implementations).
17    pub const INSPECT: Self = Self { bits: 1 << 0 };
18
19    /// Value has children that can be traversed.
20    pub const CHILDREN: Self = Self { bits: 1 << 1 };
21
22    /// Value supports field/index access.
23    pub const ACCESS: Self = Self { bits: 1 << 2 };
24
25    /// All read capabilities.
26    pub const READ_ALL: Self =
27        Self { bits: Self::INSPECT.bits | Self::CHILDREN.bits | Self::ACCESS.bits };
28
29    /// Check if this capability set contains another.
30    pub fn contains(self, other: Self) -> bool {
31        (self.bits & other.bits) == other.bits
32    }
33
34    /// Combine two capability sets.
35    pub fn union(self, other: Self) -> Self {
36        Self { bits: self.bits | other.bits }
37    }
38}
39
40impl Default for Capability {
41    fn default() -> Self {
42        Self::INSPECT
43    }
44}