inspect-core 0.1.0

Core types and traits for the inspect-rs introspection system
Documentation
//! Capability flags for inspectable values.

/// Capabilities that an inspectable value supports.
///
/// This allows consumers to discover what operations are available
/// without attempting them and failing.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Capability {
    bits: u32,
}

impl Capability {
    /// No capabilities.
    pub const NONE: Self = Self { bits: 0 };

    /// Value can be inspected (always true for Inspect implementations).
    pub const INSPECT: Self = Self { bits: 1 << 0 };

    /// Value has children that can be traversed.
    pub const CHILDREN: Self = Self { bits: 1 << 1 };

    /// Value supports field/index access.
    pub const ACCESS: Self = Self { bits: 1 << 2 };

    /// All read capabilities.
    pub const READ_ALL: Self =
        Self { bits: Self::INSPECT.bits | Self::CHILDREN.bits | Self::ACCESS.bits };

    /// Check if this capability set contains another.
    pub fn contains(self, other: Self) -> bool {
        (self.bits & other.bits) == other.bits
    }

    /// Combine two capability sets.
    pub fn union(self, other: Self) -> Self {
        Self { bits: self.bits | other.bits }
    }
}

impl Default for Capability {
    fn default() -> Self {
        Self::INSPECT
    }
}