1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq)]
7pub enum TaggingMode {
8 Explicit,
10 Implicit,
12 Automatic,
14}
15
16#[derive(Debug, Clone, PartialEq)]
18pub struct Module {
19 pub name: String,
20 pub oid: Option<Vec<String>>,
22 pub tagging_mode: Option<TaggingMode>,
24 pub imports: Vec<Import>,
25 pub exports: Vec<String>,
26 pub definitions: Vec<Definition>,
27 pub values: Vec<ValueAssignment>,
28}
29
30#[derive(Debug, Clone, PartialEq)]
32pub struct Import {
33 pub symbols: Vec<String>,
35 pub module_name: String,
37}
38
39#[derive(Debug, Clone, PartialEq)]
41pub struct Definition {
42 pub name: String,
43 pub ty: Type,
44}
45
46#[derive(Debug, Clone, PartialEq)]
48pub struct ValueAssignment {
49 pub name: String,
50 pub ty: Type,
51 pub value: Value,
52}
53
54#[derive(Debug, Clone, PartialEq)]
56pub enum Value {
57 ObjectIdentifier(Vec<OidComponent>),
59 Integer(i64),
61 Boolean(bool),
63 String(String),
65}
66
67#[derive(Debug, Clone, PartialEq)]
69pub enum OidComponent {
70 Number(u32),
72 NamedRef(String),
74}
75
76#[derive(Debug, Clone, PartialEq)]
78pub struct NamedNumber {
79 pub name: String,
80 pub value: i64,
81}
82
83#[derive(Debug, Clone, PartialEq)]
88pub struct ClassField {
89 pub name: String,
91 pub unique: bool,
93 pub optional: bool,
95}
96
97#[derive(Debug, Clone, PartialEq)]
99pub enum Type {
100 Sequence(Vec<SequenceField>),
102 SequenceOf(Box<Type>, Option<SizeConstraint>),
104 Set(Vec<SequenceField>),
106 SetOf(Box<Type>, Option<SizeConstraint>),
108 Choice(Vec<ChoiceVariant>),
110 TypeRef(String),
112 Integer(Option<ValueConstraint>, Vec<NamedNumber>),
114 Enumerated(Vec<NamedNumber>),
116 Real,
118 Boolean,
120 OctetString(Option<SizeConstraint>),
122 BitString(Option<SizeConstraint>),
124 ObjectIdentifier,
126 Null,
128 Utf8String(Option<SizeConstraint>),
130 PrintableString(Option<SizeConstraint>),
132 IA5String(Option<SizeConstraint>),
134 TeletexString(Option<SizeConstraint>),
136 UniversalString(Option<SizeConstraint>),
138 BmpString(Option<SizeConstraint>),
140 GeneralString(Option<SizeConstraint>),
142 NumericString(Option<SizeConstraint>),
144 VisibleString(Option<SizeConstraint>),
146 UtcTime,
148 GeneralizedTime,
150 Tagged { tag: TagInfo, inner: Box<Type> },
152 Constrained {
154 base_type: Box<Type>,
155 constraint: Constraint,
156 },
157 Any,
159 AnyDefinedBy(String),
161 Class(Vec<ClassField>),
168}
169
170#[derive(Debug, Clone, PartialEq)]
172pub struct Constraint {
173 pub spec: ConstraintSpec,
174 pub exception: Option<ExceptionSpec>,
175}
176
177#[derive(Debug, Clone, PartialEq)]
179pub enum ConstraintSpec {
180 Subtype(SubtypeConstraint),
182 General(GeneralConstraint),
184}
185
186#[derive(Debug, Clone, PartialEq)]
188pub enum SubtypeConstraint {
189 SingleValue(ConstraintValue),
191
192 ValueRange {
194 min: ConstraintValue,
195 max: ConstraintValue,
196 },
197
198 SizeConstraint(Box<SubtypeConstraint>),
200
201 PermittedAlphabet(Vec<CharRange>),
203
204 Pattern(String),
206
207 ContainedSubtype(Box<Type>),
209
210 InnerType(Box<SubtypeConstraint>),
212
213 Union(Vec<SubtypeConstraint>),
215
216 Intersection(Vec<SubtypeConstraint>),
218
219 Complement(Box<SubtypeConstraint>),
221
222 NamedBitList(Vec<NamedNumber>),
224}
225
226#[derive(Debug, Clone, PartialEq)]
228pub enum ConstraintValue {
229 Integer(i64),
231 NamedValue(String),
233 Min,
235 Max,
237}
238
239#[derive(Debug, Clone, PartialEq)]
241pub struct CharRange {
242 pub min: char,
243 pub max: char,
244}
245
246#[derive(Debug, Clone, PartialEq)]
248pub enum GeneralConstraint {
249 ComponentRelation {
251 object_set: String,
252 component_refs: Vec<String>,
253 },
254 UserDefined(String),
256}
257
258#[derive(Debug, Clone, PartialEq)]
260pub enum ExceptionSpec {
261 Identifier(String),
263 Value(ConstraintValue),
265}
266
267#[derive(Debug, Clone, PartialEq)]
270pub enum ValueConstraint {
271 Single(i64),
273 Range(Option<i64>, Option<i64>),
275}
276
277#[derive(Debug, Clone, PartialEq)]
279pub enum SizeConstraint {
280 Fixed(u64),
282 Range(Option<u64>, Option<u64>),
284}
285
286#[derive(Debug, Clone, PartialEq)]
288pub struct SequenceField {
289 pub name: String,
290 pub ty: Type,
291 pub optional: bool,
292 pub default: Option<String>,
293}
294
295#[derive(Debug, Clone, PartialEq)]
297pub struct ChoiceVariant {
298 pub name: String,
299 pub ty: Type,
300}
301
302#[derive(Debug, Clone, PartialEq)]
304pub struct TagInfo {
305 pub class: TagClass,
306 pub number: u32,
307 pub tagging: Tagging,
308}
309
310#[derive(Debug, Clone, PartialEq)]
312pub enum TagClass {
313 Universal,
314 Application,
315 ContextSpecific,
316 Private,
317}
318
319#[derive(Debug, Clone, PartialEq)]
321pub enum Tagging {
322 Explicit,
323 Implicit,
324}
325
326impl fmt::Display for Type {
327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328 match self {
329 Type::Sequence(_) => write!(f, "SEQUENCE"),
330 Type::SequenceOf(inner, _) => write!(f, "SEQUENCE OF {}", inner),
331 Type::Set(_) => write!(f, "SET"),
332 Type::SetOf(inner, _) => write!(f, "SET OF {}", inner),
333 Type::Choice(_) => write!(f, "CHOICE"),
334 Type::TypeRef(name) => write!(f, "{}", name),
335 Type::Integer(_, _) => write!(f, "INTEGER"),
336 Type::Enumerated(_) => write!(f, "ENUMERATED"),
337 Type::Real => write!(f, "REAL"),
338 Type::Boolean => write!(f, "BOOLEAN"),
339 Type::OctetString(_) => write!(f, "OCTET STRING"),
340 Type::BitString(_) => write!(f, "BIT STRING"),
341 Type::ObjectIdentifier => write!(f, "OBJECT IDENTIFIER"),
342 Type::Null => write!(f, "NULL"),
343 Type::Utf8String(_) => write!(f, "UTF8String"),
344 Type::PrintableString(_) => write!(f, "PrintableString"),
345 Type::IA5String(_) => write!(f, "IA5String"),
346 Type::TeletexString(_) => write!(f, "TeletexString"),
347 Type::UniversalString(_) => write!(f, "UniversalString"),
348 Type::BmpString(_) => write!(f, "BMPString"),
349 Type::GeneralString(_) => write!(f, "GeneralString"),
350 Type::NumericString(_) => write!(f, "NumericString"),
351 Type::VisibleString(_) => write!(f, "VisibleString"),
352 Type::UtcTime => write!(f, "UTCTime"),
353 Type::GeneralizedTime => write!(f, "GeneralizedTime"),
354 Type::Tagged { tag, inner } => {
355 write!(f, "[{}] {:?} {}", tag.number, tag.tagging, inner)
356 }
357 Type::Constrained { base_type, .. } => {
358 write!(f, "{} (constrained)", base_type)
359 }
360 Type::Any => write!(f, "ANY"),
361 Type::AnyDefinedBy(field) => write!(f, "ANY DEFINED BY {}", field),
362 Type::Class(fields) => {
363 write!(f, "CLASS {{ ")?;
364 for (i, field) in fields.iter().enumerate() {
365 if i > 0 {
366 write!(f, ", ")?;
367 }
368 write!(f, "&{}", field.name)?;
369 if field.unique {
370 write!(f, " UNIQUE")?;
371 }
372 if field.optional {
373 write!(f, " OPTIONAL")?;
374 }
375 }
376 write!(f, " }}")
377 }
378 }
379 }
380}