mib-rs 0.8.0

SNMP MIB parser and resolver
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Enumerations for SMI concepts.
//!
//! Defines the core enums used throughout the MIB parsing and resolution pipeline:
//! severity levels, node kinds, access levels, status values, base types, and
//! configuration knobs for resolver strictness and diagnostic reporting.

use std::fmt;

macro_rules! impl_display {
    ($ty:ident { $($variant:ident => $s:literal),* $(,)? }) => {
        impl fmt::Display for $ty {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(match self {
                    $($ty::$variant => $s),*
                })
            }
        }
    }
}

/// Severity indicates how serious a diagnostic issue is (libsmi-compatible).
/// Lower values are more severe.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Severity {
    /// Unrecoverable failure that halts processing.
    Fatal = 0,
    /// Serious issue that likely produces incorrect results.
    Severe = 1,
    /// Standard error in the MIB definition.
    Error = 2,
    /// Minor issue that may indicate a problem.
    Minor = 3,
    /// Stylistic deviation from best practice.
    Style = 4,
    /// Potential issue worth noting.
    Warning = 5,
    /// Informational message.
    Info = 6,
}

impl Severity {
    /// Reports whether this severity is at least as severe as `threshold`.
    pub fn at_least(self, threshold: Severity) -> bool {
        self <= threshold
    }
}

impl_display!(Severity {
    Fatal => "fatal",
    Severe => "severe",
    Error => "error",
    Minor => "minor",
    Style => "style",
    Warning => "warning",
    Info => "info",
});

/// Controls resolver fallback behavior when resolving cross-module references.
///
/// Ordered from strictest (fewest fallbacks) to most permissive.
/// See also [`ReportingLevel`] which controls diagnostic output separately.
///
/// All levels support direct import resolution, import forwarding (following
/// re-exports declared in the source module's own IMPORTS), partial import
/// resolution, ASN.1 primitive type fallback, and well-known OID roots.
///
/// See the crate-level docs for a detailed breakdown of behaviors per level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum ResolverStrictness {
    /// Minimal fallbacks. Only deterministic strategies that don't guess
    /// the source module (direct imports, import forwarding, ASN.1
    /// primitives, well-known OID roots).
    Strict = 0,
    /// Constrained fallbacks: module name aliases, unimported SMI/TC type
    /// lookup, SMI global OID root fallback, TRAP-TYPE enterprise lookup.
    Normal = 1,
    /// All fallbacks, including global symbol search across all loaded
    /// modules for objects, group members, and compliance targets.
    Permissive = 2,
}

impl ResolverStrictness {
    /// Reports whether tier-2 constrained fallbacks are enabled (Normal+).
    pub fn allow_constrained_fallbacks(self) -> bool {
        self != ResolverStrictness::Strict
    }

    /// Reports whether tier-3 global fallbacks are enabled (Permissive only).
    pub fn allow_global_fallbacks(self) -> bool {
        self == ResolverStrictness::Permissive
    }
}

impl_display!(ResolverStrictness {
    Strict => "strict",
    Normal => "normal",
    Permissive => "permissive",
});

/// Controls diagnostic reporting verbosity.
///
/// See also [`ResolverStrictness`] which controls resolver fallback behavior separately.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum ReportingLevel {
    /// Suppress all diagnostics except fatal errors.
    Silent = 0,
    /// Report errors and above only.
    Quiet = 1,
    /// Report minor issues and above.
    Default = 2,
    /// Report all diagnostics including style and info.
    Verbose = 3,
}

impl_display!(ReportingLevel {
    Silent => "silent",
    Quiet => "quiet",
    Default => "default",
    Verbose => "verbose",
});

/// Identifies what an OID node represents in the MIB tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum Kind {
    /// Kind not yet determined.
    #[default]
    Unknown = 0,
    /// Synthetic internal node (e.g. root of the OID tree).
    Internal = 1,
    /// Plain OID registration (OBJECT IDENTIFIER value assignment).
    Node = 2,
    /// Scalar OBJECT-TYPE (single-instance managed object).
    Scalar = 3,
    /// Table OBJECT-TYPE (SEQUENCE OF).
    Table = 4,
    /// Row OBJECT-TYPE (conceptual row / SEQUENCE entry).
    Row = 5,
    /// Column OBJECT-TYPE (leaf within a row).
    Column = 6,
    /// NOTIFICATION-TYPE or TRAP-TYPE definition.
    Notification = 7,
    /// OBJECT-GROUP or NOTIFICATION-GROUP.
    Group = 8,
    /// MODULE-COMPLIANCE definition.
    Compliance = 9,
    /// AGENT-CAPABILITIES definition.
    Capability = 10,
    /// MODULE-IDENTITY definition.
    ModuleIdentity = 11,
    /// OBJECT-IDENTITY definition.
    ObjectIdentity = 12,
}

impl Kind {
    /// Reports whether this is a scalar/table/row/column.
    pub fn is_object_type(self) -> bool {
        matches!(self, Kind::Scalar | Kind::Table | Kind::Row | Kind::Column)
    }

    /// Reports whether this is a group/compliance/capabilities node.
    pub fn is_conformance(self) -> bool {
        matches!(self, Kind::Group | Kind::Compliance | Kind::Capability)
    }

    /// Reports whether this is a plain node-like kind (node, module-identity, object-identity).
    pub fn is_node_like(self) -> bool {
        matches!(
            self,
            Kind::Node | Kind::ModuleIdentity | Kind::ObjectIdentity
        )
    }
}

impl_display!(Kind {
    Unknown => "unknown",
    Internal => "internal",
    Node => "node",
    Scalar => "scalar",
    Table => "table",
    Row => "row",
    Column => "column",
    Notification => "notification",
    Group => "group",
    Compliance => "compliance",
    Capability => "capabilities",
    ModuleIdentity => "module-identity",
    ObjectIdentity => "object-identity",
});

/// Access level for OBJECT-TYPE definitions.
///
/// Covers both SMIv1 ACCESS and SMIv2 MAX-ACCESS values.
/// See [`AccessKeyword`] for which keyword was used in the source.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum Access {
    /// Object cannot be read or written.
    #[default]
    NotAccessible = 0,
    /// Object is only accessible via notifications.
    AccessibleForNotify = 1,
    /// Object can be read but not written.
    ReadOnly = 2,
    /// Object can be read and written.
    ReadWrite = 3,
    /// Object can be read, written, and used in row creation.
    ReadCreate = 4,
    /// Object can only be written (SMIv1 only, deprecated in SMIv2).
    WriteOnly = 5,
    /// Object is not implemented (AGENT-CAPABILITIES variation).
    NotImplemented = 6,
}

impl_display!(Access {
    NotAccessible => "not-accessible",
    AccessibleForNotify => "accessible-for-notify",
    ReadOnly => "read-only",
    ReadWrite => "read-write",
    ReadCreate => "read-create",
    WriteOnly => "write-only",
    NotImplemented => "not-implemented",
});

/// Lifecycle state of a MIB definition.
///
/// SMIv2 uses `Current`, `Deprecated`, and `Obsolete`. SMIv1 additionally uses
/// `Mandatory` and `Optional`. Values are not normalized across versions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum Status {
    /// Active and valid (SMIv2).
    #[default]
    Current = 0,
    /// Still usable but being phased out.
    Deprecated = 1,
    /// No longer in use.
    Obsolete = 2,
    /// Required for compliance (SMIv1 only).
    Mandatory = 3,
    /// Not required (SMIv1 only).
    Optional = 4,
}

impl Status {
    /// Reports whether this is an SMIv1-specific status value.
    pub fn is_smiv1(self) -> bool {
        matches!(self, Status::Mandatory | Status::Optional)
    }
}

impl_display!(Status {
    Current => "current",
    Deprecated => "deprecated",
    Obsolete => "obsolete",
    Mandatory => "mandatory",
    Optional => "optional",
});

/// SMI language version of a MIB module.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum Language {
    /// Version not yet determined.
    #[default]
    Unknown = 0,
    /// RFC 1155/1212 (Structure of Management Information v1).
    SMIv1 = 1,
    /// RFC 2578 (Structure of Management Information v2).
    SMIv2 = 2,
    /// RFC 3159 (Structure of Policy Provisioning Information).
    SPPI = 3,
}

impl_display!(Language {
    Unknown => "unknown",
    SMIv1 => "SMIv1",
    SMIv2 => "SMIv2",
    SPPI => "SPPI",
});

/// Fundamental SMI type that a textual convention or [`Kind::Scalar`]/[`Kind::Column`]
/// object ultimately resolves to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum BaseType {
    /// Base type not yet resolved.
    #[default]
    Unknown = 0,
    /// 32-bit signed integer (INTEGER, Integer32).
    Integer32 = 1,
    /// 32-bit unsigned integer (Unsigned32).
    Unsigned32 = 2,
    /// 32-bit monotonically increasing counter.
    Counter32 = 3,
    /// 64-bit monotonically increasing counter.
    Counter64 = 4,
    /// 32-bit non-negative integer that can increase or decrease.
    Gauge32 = 5,
    /// Hundredths of a second since an epoch.
    TimeTicks = 6,
    /// IPv4 address (4 octets).
    IpAddress = 7,
    /// Arbitrary binary or text data.
    OctetString = 8,
    /// ASN.1 OBJECT IDENTIFIER value.
    ObjectIdentifier = 9,
    /// Named bit set.
    Bits = 10,
    /// Opaque data (wraps arbitrary ASN.1).
    Opaque = 11,
    /// SEQUENCE type used for table row definitions.
    Sequence = 12,
    /// 64-bit signed integer (SPPI).
    Integer64 = 13,
    /// 64-bit unsigned integer (SPPI).
    Unsigned64 = 14,
}

impl_display!(BaseType {
    Unknown => "unknown",
    Integer32 => "Integer32",
    Unsigned32 => "Unsigned32",
    Counter32 => "Counter32",
    Counter64 => "Counter64",
    Gauge32 => "Gauge32",
    TimeTicks => "TimeTicks",
    IpAddress => "IpAddress",
    OctetString => "OCTET STRING",
    ObjectIdentifier => "OBJECT IDENTIFIER",
    Bits => "BITS",
    Opaque => "Opaque",
    Sequence => "SEQUENCE",
    Integer64 => "Integer64",
    Unsigned64 => "Unsigned64",
});

/// How an INDEX component maps to instance-identifier sub-identifiers (RFC 2578, Section 7.7).
///
/// When a table row is identified by its index values, those values are
/// encoded as OID sub-identifiers appended to the column OID. The
/// encoding strategy depends on the index object's [`BaseType`] and
/// constraints:
///
/// - Integer types use a single sub-identifier containing the value.
/// - Fixed-length strings (with a single-value SIZE constraint like
///   `SIZE (6)`) use one sub-identifier per octet, with no length prefix.
/// - Variable-length strings are length-prefixed: one sub-identifier
///   for the length, followed by one per octet.
/// - The `IMPLIED` keyword omits the length prefix, but can only be
///   used on the last index component since there is no way to tell
///   where it ends otherwise.
///
/// This encoding matters when constructing or parsing instance OIDs
/// programmatically (e.g. building an SNMP GET request for a specific
/// table row).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum IndexEncoding {
    /// Encoding not yet determined.
    #[default]
    Unknown = 0,
    /// Single sub-identifier for integer-valued indexes.
    Integer = 1,
    /// Fixed number of sub-identifiers (SIZE-constrained OCTET STRING).
    /// No length prefix; the number of sub-identifiers equals the fixed SIZE.
    FixedString = 2,
    /// Length prefix followed by that many sub-identifiers.
    /// Used for variable-length OCTET STRING and OBJECT IDENTIFIER indexes.
    LengthPrefixed = 3,
    /// No length prefix; the index value extends to the end of the OID.
    /// Only valid for the last index component (uses the `IMPLIED` keyword).
    Implied = 4,
    /// Four sub-identifiers encoding an IPv4 address (one per octet).
    IpAddress = 5,
}

impl_display!(IndexEncoding {
    Unknown => "unknown",
    Integer => "integer",
    FixedString => "fixed-string",
    LengthPrefixed => "length-prefixed",
    Implied => "implied",
    IpAddress => "ip-address",
});

/// Records which access keyword was used in the source MIB.
///
/// SMIv1 uses `ACCESS`, SMIv2 uses `MAX-ACCESS`, and compliance statements use `MIN-ACCESS`.
/// The resolved access value is stored separately as [`Access`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum AccessKeyword {
    /// SMIv1 `ACCESS` clause.
    #[default]
    Access = 0,
    /// SMIv2 `MAX-ACCESS` clause.
    MaxAccess = 1,
    /// `MIN-ACCESS` clause in MODULE-COMPLIANCE refinements.
    MinAccess = 2,
}

impl_display!(AccessKeyword {
    Access => "ACCESS",
    MaxAccess => "MAX-ACCESS",
    MinAccess => "MIN-ACCESS",
});

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn severity_ordering() {
        assert!(Severity::Fatal <= Severity::Info);
        assert!(Severity::Fatal <= Severity::Fatal);
        assert!(Severity::Info > Severity::Fatal);
    }

    #[test]
    fn severity_display() {
        assert_eq!(Severity::Fatal.to_string(), "fatal");
        assert_eq!(Severity::Info.to_string(), "info");
    }

    #[test]
    fn kind_classification() {
        assert!(Kind::Scalar.is_object_type());
        assert!(Kind::Table.is_object_type());
        assert!(Kind::Row.is_object_type());
        assert!(Kind::Column.is_object_type());
        assert!(!Kind::Node.is_object_type());
        assert!(!Kind::Notification.is_object_type());

        assert!(Kind::Group.is_conformance());
        assert!(Kind::Compliance.is_conformance());
        assert!(Kind::Capability.is_conformance());
        assert!(!Kind::Scalar.is_conformance());
    }

    #[test]
    fn status_smiv1() {
        assert!(Status::Mandatory.is_smiv1());
        assert!(Status::Optional.is_smiv1());
        assert!(!Status::Current.is_smiv1());
        assert!(!Status::Deprecated.is_smiv1());
    }
}