Skip to main content

inspect_core/
value.rs

1//! Value representation and traversal.
2
3#[cfg(not(feature = "std"))]
4use alloc::{boxed::Box, vec::Vec};
5
6use crate::{Capability, FieldInfo, Kind, Sensitivity, TypeInfo, VariantInfo};
7
8/// A borrowed reference to an inspected value.
9///
10/// This is the core data structure returned by [`Inspect::inspect`](crate::Inspect::inspect).
11/// It provides access to the value's kind, type information, and children without
12/// forcing eager allocation or traversal.
13#[derive(Debug)]
14pub struct ValueRef<'a> {
15    kind: Kind<'a>,
16    type_info: TypeInfo<'a>,
17    children: Option<Children<'a>>,
18    variant: Option<VariantInfo<'a>>,
19    sensitivity: Sensitivity,
20    capability: Capability,
21}
22
23impl<'a> ValueRef<'a> {
24    /// Create a value reference with minimal information.
25    pub fn with_type(kind: Kind<'a>, type_info: TypeInfo<'a>) -> Self {
26        let capability = if kind.is_scalar() { Capability::INSPECT } else { Capability::READ_ALL };
27
28        Self {
29            kind,
30            type_info,
31            children: None,
32            variant: None,
33            sensitivity: Sensitivity::Normal,
34            capability,
35        }
36    }
37
38    /// Create a value reference with children.
39    pub fn with_children(kind: Kind<'a>, type_info: TypeInfo<'a>, children: Children<'a>) -> Self {
40        Self {
41            kind,
42            type_info,
43            children: Some(children),
44            variant: None,
45            sensitivity: Sensitivity::Normal,
46            capability: Capability::READ_ALL,
47        }
48    }
49
50    /// Set the variant information for enum values.
51    pub fn with_variant(mut self, variant: VariantInfo<'a>) -> Self {
52        self.variant = Some(variant);
53        self
54    }
55
56    /// Set the sensitivity level.
57    pub fn with_sensitivity(mut self, sensitivity: Sensitivity) -> Self {
58        self.sensitivity = sensitivity;
59        self
60    }
61
62    /// Get the value's kind.
63    pub fn kind(&self) -> &Kind<'a> {
64        &self.kind
65    }
66
67    /// Get the value's type information.
68    pub fn type_info(&self) -> &TypeInfo<'a> {
69        &self.type_info
70    }
71
72    /// Get the value's children, if any.
73    pub fn children(&self) -> Option<&Children<'a>> {
74        self.children.as_ref()
75    }
76
77    /// Get the enum variant information, if applicable.
78    pub fn variant(&self) -> Option<&VariantInfo<'a>> {
79        self.variant.as_ref()
80    }
81
82    /// Get the value's sensitivity classification.
83    pub fn sensitivity(&self) -> Sensitivity {
84        self.sensitivity
85    }
86
87    /// Get the value's capabilities.
88    pub fn capability(&self) -> Capability {
89        self.capability
90    }
91}
92
93/// Children of an inspected value.
94///
95/// This type represents the child elements of a structured value like a
96/// struct, tuple, or collection.
97#[derive(Debug)]
98pub enum Children<'a> {
99    /// A list of child values with their field information.
100    Direct(Vec<(FieldInfo<'a>, ValueRef<'a>)>),
101}
102
103impl<'a> Children<'a> {
104    /// Create children from a list of field/value pairs.
105    pub fn direct(fields: Vec<(FieldInfo<'a>, ValueRef<'a>)>) -> Self {
106        Self::Direct(fields)
107    }
108
109    /// Get the number of children.
110    pub fn len(&self) -> usize {
111        match self {
112            Children::Direct(fields) => fields.len(),
113        }
114    }
115
116    /// Check if there are any children.
117    pub fn is_empty(&self) -> bool {
118        self.len() == 0
119    }
120}