Skip to main content

mib_rs/mib/
object.rs

1//! OBJECT-TYPE definitions and the shared entity base type.
2//!
3//! [`ObjectData`] holds all fields from an SMIv1 or SMIv2 OBJECT-TYPE
4//! definition, including the resolved type, access level, INDEX clause,
5//! DEFVAL, and effective (inherited) constraint values computed during
6//! resolution.
7//!
8//! [`EntityData`] is the shared base for all OID-bearing entity types
9//! (objects, notifications, groups, compliances, capabilities).
10//!
11//! For handle-oriented access, see [`Object`](super::handle::Object).
12
13use crate::types::{Access, Kind, Span};
14
15use super::types::*;
16
17/// Common fields shared by all OID-bearing entity definitions.
18///
19/// Embedded in [`ObjectData`], [`NotificationData`](super::notification::NotificationData),
20/// [`GroupData`](super::group::GroupData), [`ComplianceData`](super::compliance::ComplianceData),
21/// and [`CapabilityData`](super::capability::CapabilityData). Not accessed
22/// directly by callers; each containing type re-exposes the relevant fields
23/// through its own accessor methods.
24#[derive(Debug, Clone)]
25pub struct EntityData {
26    pub(crate) name: String,
27    pub(crate) span: Span,
28    pub(crate) node: Option<NodeId>,
29    pub(crate) module: Option<ModuleId>,
30    pub(crate) status: crate::types::Status,
31    pub(crate) description: String,
32    pub(crate) reference: String,
33    pub(crate) status_span: Span,
34    pub(crate) desc_span: Span,
35    pub(crate) ref_span: Span,
36    pub(crate) oid_refs: Vec<OidRef>,
37}
38
39impl EntityData {
40    pub(crate) fn new(name: String) -> Self {
41        Self {
42            name,
43            span: Span::SYNTHETIC,
44            node: None,
45            module: None,
46            status: crate::types::Status::Current,
47            description: String::new(),
48            reference: String::new(),
49            status_span: Span::SYNTHETIC,
50            desc_span: Span::SYNTHETIC,
51            ref_span: Span::SYNTHETIC,
52            oid_refs: Vec::new(),
53        }
54    }
55}
56
57/// An OBJECT-TYPE definition from an SMIv1 or SMIv2 module.
58///
59/// Contains both the raw definition fields and effective (inherited) values
60/// computed during resolution. Access through [`ObjectData`] methods or the
61/// [`Object`](super::handle::Object) handle type.
62#[derive(Debug, Clone)]
63pub struct ObjectData {
64    pub(crate) entity: EntityData,
65    pub(crate) typ: Option<TypeId>,
66    pub(crate) access: Access,
67    pub(crate) units: String,
68    pub(crate) def_val: Option<DefVal>,
69    pub(crate) augments: Option<ObjectId>,
70    pub(crate) augmented_by: Vec<ObjectId>,
71    pub(crate) syntax_span: Span,
72    pub(crate) access_span: Span,
73    pub(crate) units_span: Span,
74    pub(crate) augments_span: Span,
75    pub(crate) def_val_span: Span,
76    pub(crate) index: Vec<IndexEntry>,
77    pub(crate) hint: String,
78    pub(crate) sizes: Vec<Range>,
79    pub(crate) ranges: Vec<Range>,
80    pub(crate) enums: Vec<NamedValue>,
81    pub(crate) bits: Vec<NamedValue>,
82    pub(crate) sequence_type_name: String,
83}
84
85impl ObjectData {
86    pub(crate) fn new(name: String) -> Self {
87        Self {
88            entity: EntityData::new(name),
89            typ: None,
90            access: Access::NotAccessible,
91            units: String::new(),
92            def_val: None,
93            augments: None,
94            augmented_by: Vec::new(),
95            syntax_span: Span::SYNTHETIC,
96            access_span: Span::SYNTHETIC,
97            units_span: Span::SYNTHETIC,
98            augments_span: Span::SYNTHETIC,
99            def_val_span: Span::SYNTHETIC,
100            index: Vec::new(),
101            hint: String::new(),
102            sizes: Vec::new(),
103            ranges: Vec::new(),
104            enums: Vec::new(),
105            bits: Vec::new(),
106            sequence_type_name: String::new(),
107        }
108    }
109}
110
111/// Public accessor methods for [`ObjectData`].
112impl ObjectData {
113    /// Return the object name.
114    pub fn name(&self) -> &str {
115        &self.entity.name
116    }
117
118    /// Return the source span.
119    pub fn span(&self) -> Span {
120        self.entity.span
121    }
122
123    /// Return the OID tree node id, if resolved.
124    pub fn node(&self) -> Option<NodeId> {
125        self.entity.node
126    }
127
128    /// Return the defining module id.
129    pub fn module(&self) -> Option<ModuleId> {
130        self.entity.module
131    }
132
133    /// Return the status (current, deprecated, obsolete).
134    pub fn status(&self) -> crate::types::Status {
135        self.entity.status
136    }
137
138    /// Return the DESCRIPTION clause text.
139    pub fn description(&self) -> &str {
140        &self.entity.description
141    }
142
143    /// Return the REFERENCE clause text.
144    pub fn reference(&self) -> &str {
145        &self.entity.reference
146    }
147
148    /// Return symbolic OID references from the definition.
149    pub fn oid_refs(&self) -> &[OidRef] {
150        &self.entity.oid_refs
151    }
152
153    /// Return the resolved type id, if any.
154    pub fn type_id(&self) -> Option<TypeId> {
155        self.typ
156    }
157
158    /// Return the access level.
159    pub fn access(&self) -> Access {
160        self.access
161    }
162
163    /// Return the UNITS clause text.
164    pub fn units(&self) -> &str {
165        &self.units
166    }
167
168    /// Return the DEFVAL clause, if present.
169    pub fn default_value(&self) -> Option<&DefVal> {
170        self.def_val.as_ref()
171    }
172
173    /// Return the AUGMENTS target row id, if this row augments another.
174    pub fn augments(&self) -> Option<ObjectId> {
175        self.augments
176    }
177
178    /// Return rows that augment this row.
179    pub fn augmented_by(&self) -> &[ObjectId] {
180        &self.augmented_by
181    }
182
183    /// Return the node [`Kind`] by looking up the [`OidTree`](super::node::OidTree).
184    ///
185    /// Returns [`Kind::Unknown`] if the object's OID was not resolved.
186    /// Callers using the [`Object`](super::handle::Object) handle do not need
187    /// this method; use [`Object::kind`](super::handle::Object::kind) instead.
188    pub fn kind(&self, tree: &super::node::OidTree) -> Kind {
189        match self.entity.node {
190            Some(id) => tree.get(id).kind,
191            None => Kind::Unknown,
192        }
193    }
194
195    /// Return the effective display hint, inherited from the resolved type chain.
196    ///
197    /// This is pre-computed during resolution, so it does not require
198    /// walking the type chain at query time.
199    pub fn effective_display_hint(&self) -> &str {
200        &self.hint
201    }
202
203    /// Return the effective SIZE constraints, inherited from the resolved type chain.
204    pub fn effective_sizes(&self) -> &[Range] {
205        &self.sizes
206    }
207
208    /// Return the effective range constraints, inherited from the resolved type chain.
209    pub fn effective_ranges(&self) -> &[Range] {
210        &self.ranges
211    }
212
213    /// Return the effective enumeration values, inherited from the resolved type chain.
214    pub fn effective_enums(&self) -> &[NamedValue] {
215        &self.enums
216    }
217
218    /// Return the effective BITS definitions, inherited from the resolved type chain.
219    pub fn effective_bits(&self) -> &[NamedValue] {
220        &self.bits
221    }
222
223    /// Return the SEQUENCE type name from the table definition.
224    pub fn sequence_type_name(&self) -> &str {
225        &self.sequence_type_name
226    }
227
228    /// Return the INDEX clause entries.
229    pub fn index(&self) -> &[IndexEntry] {
230        &self.index
231    }
232
233    /// Return the source span of the SYNTAX clause.
234    pub fn syntax_span(&self) -> Span {
235        self.syntax_span
236    }
237
238    /// Return the source span of the ACCESS/MAX-ACCESS clause.
239    pub fn access_span(&self) -> Span {
240        self.access_span
241    }
242
243    /// Return the source span of the UNITS clause.
244    pub fn units_span(&self) -> Span {
245        self.units_span
246    }
247
248    /// Return the source span of the AUGMENTS clause.
249    pub fn augments_span(&self) -> Span {
250        self.augments_span
251    }
252
253    /// Return the source span of the DEFVAL clause.
254    pub fn default_value_span(&self) -> Span {
255        self.def_val_span
256    }
257
258    /// Look up an enumeration value by label name.
259    pub fn enum_by_label(&self, label: &str) -> Option<&NamedValue> {
260        find_named_value(&self.enums, label)
261    }
262
263    /// Look up a BITS value by label name.
264    pub fn bit_by_label(&self, label: &str) -> Option<&NamedValue> {
265        find_named_value(&self.bits, label)
266    }
267}