inspect-core 0.1.0

Core types and traits for the inspect-rs introspection system
Documentation
//! Value representation and traversal.

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};

use crate::{Capability, FieldInfo, Kind, Sensitivity, TypeInfo, VariantInfo};

/// A borrowed reference to an inspected value.
///
/// This is the core data structure returned by [`Inspect::inspect`](crate::Inspect::inspect).
/// It provides access to the value's kind, type information, and children without
/// forcing eager allocation or traversal.
#[derive(Debug)]
pub struct ValueRef<'a> {
    kind: Kind<'a>,
    type_info: TypeInfo<'a>,
    children: Option<Children<'a>>,
    variant: Option<VariantInfo<'a>>,
    sensitivity: Sensitivity,
    capability: Capability,
}

impl<'a> ValueRef<'a> {
    /// Create a value reference with minimal information.
    pub fn with_type(kind: Kind<'a>, type_info: TypeInfo<'a>) -> Self {
        let capability = if kind.is_scalar() { Capability::INSPECT } else { Capability::READ_ALL };

        Self {
            kind,
            type_info,
            children: None,
            variant: None,
            sensitivity: Sensitivity::Normal,
            capability,
        }
    }

    /// Create a value reference with children.
    pub fn with_children(kind: Kind<'a>, type_info: TypeInfo<'a>, children: Children<'a>) -> Self {
        Self {
            kind,
            type_info,
            children: Some(children),
            variant: None,
            sensitivity: Sensitivity::Normal,
            capability: Capability::READ_ALL,
        }
    }

    /// Set the variant information for enum values.
    pub fn with_variant(mut self, variant: VariantInfo<'a>) -> Self {
        self.variant = Some(variant);
        self
    }

    /// Set the sensitivity level.
    pub fn with_sensitivity(mut self, sensitivity: Sensitivity) -> Self {
        self.sensitivity = sensitivity;
        self
    }

    /// Get the value's kind.
    pub fn kind(&self) -> &Kind<'a> {
        &self.kind
    }

    /// Get the value's type information.
    pub fn type_info(&self) -> &TypeInfo<'a> {
        &self.type_info
    }

    /// Get the value's children, if any.
    pub fn children(&self) -> Option<&Children<'a>> {
        self.children.as_ref()
    }

    /// Get the enum variant information, if applicable.
    pub fn variant(&self) -> Option<&VariantInfo<'a>> {
        self.variant.as_ref()
    }

    /// Get the value's sensitivity classification.
    pub fn sensitivity(&self) -> Sensitivity {
        self.sensitivity
    }

    /// Get the value's capabilities.
    pub fn capability(&self) -> Capability {
        self.capability
    }
}

/// Children of an inspected value.
///
/// This type represents the child elements of a structured value like a
/// struct, tuple, or collection.
#[derive(Debug)]
pub enum Children<'a> {
    /// A list of child values with their field information.
    Direct(Vec<(FieldInfo<'a>, ValueRef<'a>)>),
}

impl<'a> Children<'a> {
    /// Create children from a list of field/value pairs.
    pub fn direct(fields: Vec<(FieldInfo<'a>, ValueRef<'a>)>) -> Self {
        Self::Direct(fields)
    }

    /// Get the number of children.
    pub fn len(&self) -> usize {
        match self {
            Children::Direct(fields) => fields.len(),
        }
    }

    /// Check if there are any children.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}