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
//! AST types for each kind of MIB module definition.
//!
//! Each struct corresponds to a specific SMI macro invocation or assignment
//! form. The [`Definition`] enum wraps all of them into a single tagged union
//! for storage in [`super::Module::body`].

use super::common::{Ident, QuotedString};
use super::oid::OidAssignment;
use super::syntax::*;
use crate::types::Span;

/// A top-level construct in a MIB module body.
///
/// Each variant corresponds to an SMI macro invocation, type/value
/// assignment, or a recovered parse error.
#[derive(Debug, PartialEq, Eq)]
pub enum Definition {
    /// OBJECT-TYPE macro (SMIv1/v2).
    ObjectType(ObjectTypeDef),
    /// MODULE-IDENTITY macro (SMIv2).
    ModuleIdentity(ModuleIdentityDef),
    /// OBJECT-IDENTITY macro (SMIv2).
    ObjectIdentity(ObjectIdentityDef),
    /// NOTIFICATION-TYPE macro (SMIv2).
    NotificationType(NotificationTypeDef),
    /// TRAP-TYPE macro (SMIv1).
    TrapType(TrapTypeDef),
    /// TEXTUAL-CONVENTION definition (SMIv2).
    TextualConvention(TextualConventionDef),
    /// Plain type assignment (`TypeName ::= TypeSyntax`).
    TypeAssignment(TypeAssignmentDef),
    /// OID value assignment (`name OBJECT IDENTIFIER ::= { ... }`).
    ValueAssignment(ValueAssignmentDef),
    /// OBJECT-GROUP macro (SMIv2).
    ObjectGroup(ObjectGroupDef),
    /// NOTIFICATION-GROUP macro (SMIv2).
    NotificationGroup(NotificationGroupDef),
    /// MODULE-COMPLIANCE macro (SMIv2).
    ModuleCompliance(ModuleComplianceDef),
    /// AGENT-CAPABILITIES macro (SMIv2).
    AgentCapabilities(AgentCapabilitiesDef),
    /// MACRO definition (body skipped).
    MacroDefinition(MacroDefinitionDef),
    /// Recovered parse error placeholder.
    Error(ErrorDef),
}

macro_rules! delegate_def {
    (name: $($variant:ident),+ ; no_name: $($no_name:ident),+) => {
        impl Definition {
            /// Returns the definition's name, or `None` for error placeholders.
            pub fn name(&self) -> Option<&Ident> {
                match self {
                    $( Definition::$variant(d) => Some(&d.name), )+
                    $( Definition::$no_name(_) => None, )+
                }
            }
        }
    };
    (span: $($variant:ident),+) => {
        impl Definition {
            /// Returns the source span of this definition.
            pub fn span(&self) -> Span {
                match self {
                    $( Definition::$variant(d) => d.span, )+
                }
            }
        }
    };
}

delegate_def!(name:
    ObjectType, ModuleIdentity, ObjectIdentity, NotificationType,
    TrapType, TextualConvention, TypeAssignment, ValueAssignment,
    ObjectGroup, NotificationGroup, ModuleCompliance, AgentCapabilities,
    MacroDefinition;
    no_name: Error
);

delegate_def!(span:
    ObjectType, ModuleIdentity, ObjectIdentity, NotificationType,
    TrapType, TextualConvention, TypeAssignment, ValueAssignment,
    ObjectGroup, NotificationGroup, ModuleCompliance, AgentCapabilities,
    MacroDefinition, Error
);

/// OBJECT-TYPE macro invocation (SMIv1/v2).
#[derive(Debug, PartialEq, Eq)]
pub struct ObjectTypeDef {
    /// Object name (the identifier before `OBJECT-TYPE`).
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// SYNTAX clause.
    pub syntax: Option<SyntaxClause>,
    /// UNITS clause.
    pub units: Option<QuotedString>,
    /// ACCESS or MAX-ACCESS clause.
    pub access: Option<AccessClause>,
    /// STATUS clause.
    pub status: Option<StatusClause>,
    /// DESCRIPTION clause.
    pub description: Option<QuotedString>,
    /// REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// INDEX clause (mutually exclusive with `augments`).
    pub index: Option<IndexClause>,
    /// AUGMENTS clause (mutually exclusive with `index`).
    pub augments: Option<AugmentsClause>,
    /// DEFVAL clause.
    pub defval: Option<DefValClause>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// MODULE-IDENTITY macro invocation (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct ModuleIdentityDef {
    /// Object name (the identifier before `MODULE-IDENTITY`).
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// LAST-UPDATED clause (timestamp string).
    pub last_updated: QuotedString,
    /// ORGANIZATION clause.
    pub organization: QuotedString,
    /// CONTACT-INFO clause.
    pub contact_info: QuotedString,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// REVISION clauses (newest first by convention).
    pub revisions: Vec<RevisionClause>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// OBJECT-IDENTITY macro invocation (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct ObjectIdentityDef {
    /// Object name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// STATUS clause.
    pub status: StatusClause,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// NOTIFICATION-TYPE macro invocation (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct NotificationTypeDef {
    /// Notification name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// OBJECTS clause listing associated varbinds.
    pub objects: Vec<Ident>,
    /// STATUS clause.
    pub status: StatusClause,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// TRAP-TYPE macro invocation (SMIv1).
#[derive(Debug, PartialEq, Eq)]
pub struct TrapTypeDef {
    /// Trap name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// ENTERPRISE clause naming the enterprise OID.
    pub enterprise: Ident,
    /// VARIABLES clause listing associated varbinds.
    pub variables: Vec<Ident>,
    /// Optional DESCRIPTION clause.
    pub description: Option<QuotedString>,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// Numeric trap value from the `::= N` assignment.
    pub trap_number: u32,
}

/// TEXTUAL-CONVENTION definition (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct TextualConventionDef {
    /// Type name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// Optional DISPLAY-HINT clause.
    pub display_hint: Option<QuotedString>,
    /// STATUS clause.
    pub status: StatusClause,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// SYNTAX clause defining the underlying type.
    pub syntax: SyntaxClause,
}

/// Plain type assignment (`TypeName ::= TypeSyntax`).
#[derive(Debug, PartialEq, Eq)]
pub struct TypeAssignmentDef {
    /// Type name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// The type expression on the right-hand side.
    pub syntax: TypeSyntax,
}

/// OID value assignment (`name OBJECT IDENTIFIER ::= { ... }`).
#[derive(Debug, PartialEq, Eq)]
pub struct ValueAssignmentDef {
    /// Object name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// OID value assignment.
    pub oid: OidAssignment,
}

/// OBJECT-GROUP macro invocation (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct ObjectGroupDef {
    /// Group name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// OBJECTS clause listing member objects.
    pub objects: Vec<Ident>,
    /// STATUS clause.
    pub status: StatusClause,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// NOTIFICATION-GROUP macro invocation (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct NotificationGroupDef {
    /// Group name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// NOTIFICATIONS clause listing member notifications.
    pub notifications: Vec<Ident>,
    /// STATUS clause.
    pub status: StatusClause,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// MODULE-COMPLIANCE macro invocation (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct ModuleComplianceDef {
    /// Compliance statement name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// STATUS clause.
    pub status: StatusClause,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// MODULE clauses specifying compliance requirements.
    pub modules: Vec<ComplianceModule>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// A MODULE clause within [`ModuleComplianceDef`].
#[derive(Debug, PartialEq, Eq)]
pub struct ComplianceModule {
    /// Module name, or `None` for the current module.
    pub module_name: Option<Ident>,
    /// Optional OID identifying the module.
    pub module_oid: Option<OidAssignment>,
    /// MANDATORY-GROUPS clause.
    pub mandatory_groups: Vec<Ident>,
    /// GROUP and OBJECT refinements.
    pub compliances: Vec<Compliance>,
    /// Source span covering the entire MODULE clause.
    pub span: Span,
}

/// A GROUP or OBJECT refinement in a [`ComplianceModule`].
#[derive(Debug, PartialEq, Eq)]
pub enum Compliance {
    /// A conditionally required group.
    Group(ComplianceGroup),
    /// An object with refined syntax, access, or write-syntax.
    Object(ComplianceObject),
}

/// GROUP clause within MODULE-COMPLIANCE.
#[derive(Debug, PartialEq, Eq)]
pub struct ComplianceGroup {
    /// The group being referenced.
    pub group: Ident,
    /// DESCRIPTION clause explaining the condition.
    pub description: QuotedString,
    /// Source span covering the entire GROUP clause.
    pub span: Span,
}

/// OBJECT refinement within MODULE-COMPLIANCE.
#[derive(Debug, PartialEq, Eq)]
pub struct ComplianceObject {
    /// The object being refined.
    pub object: Ident,
    /// Optional refined SYNTAX.
    pub syntax: Option<SyntaxClause>,
    /// Optional refined WRITE-SYNTAX.
    pub write_syntax: Option<SyntaxClause>,
    /// Optional MIN-ACCESS clause.
    pub min_access: Option<AccessClause>,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Source span covering the entire OBJECT clause.
    pub span: Span,
}

/// AGENT-CAPABILITIES macro invocation (SMIv2).
#[derive(Debug, PartialEq, Eq)]
pub struct AgentCapabilitiesDef {
    /// Capabilities name.
    pub name: Ident,
    /// Source span of the entire definition.
    pub span: Span,
    /// PRODUCT-RELEASE clause.
    pub product_release: QuotedString,
    /// STATUS clause.
    pub status: StatusClause,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Optional REFERENCE clause.
    pub reference: Option<QuotedString>,
    /// SUPPORTS clauses listing supported modules.
    pub supports: Vec<SupportsModule>,
    /// OID value assignment (`::= { ... }`).
    pub oid: OidAssignment,
}

/// A SUPPORTS clause within [`AgentCapabilitiesDef`].
#[derive(Debug, PartialEq, Eq)]
pub struct SupportsModule {
    /// Name of the supported module.
    pub module_name: Ident,
    /// Optional OID identifying the module.
    pub module_oid: Option<OidAssignment>,
    /// INCLUDES clause listing supported groups.
    pub includes: Vec<Ident>,
    /// VARIATION clauses for individual objects.
    pub variations: Vec<Variation>,
    /// Source span covering the entire SUPPORTS clause.
    pub span: Span,
}

/// A VARIATION clause within [`SupportsModule`].
#[derive(Debug, PartialEq, Eq)]
pub struct Variation {
    /// Name of the object being varied.
    pub name: Ident,
    /// Optional refined SYNTAX.
    pub syntax: Option<SyntaxClause>,
    /// Optional refined WRITE-SYNTAX.
    pub write_syntax: Option<SyntaxClause>,
    /// Optional ACCESS override.
    pub access: Option<AccessClause>,
    /// CREATION-REQUIRES clause listing required columns.
    pub creation_requires: Vec<Ident>,
    /// Optional DEFVAL clause.
    pub defval: Option<DefValClause>,
    /// DESCRIPTION clause.
    pub description: QuotedString,
    /// Source span covering the entire VARIATION clause.
    pub span: Span,
}

/// A MACRO definition whose body was skipped by the lexer.
#[derive(Debug, PartialEq, Eq)]
pub struct MacroDefinitionDef {
    /// Macro name.
    pub name: Ident,
    /// Source span from the name through `END`.
    pub span: Span,
}

/// Placeholder for a definition that failed to parse.
///
/// Created when the parser encounters an error and recovers by
/// scanning forward to the next definition boundary.
#[derive(Debug, PartialEq, Eq)]
pub struct ErrorDef {
    /// Source span covering the skipped region.
    pub span: Span,
    /// Diagnostic message describing the parse failure.
    pub message: String,
}