1#[cfg(test)]
4use internal_macros::EnumDebug;
5use std::vec;
6
7#[cfg(doc)]
8use crate::Backend;
9
10use super::{constraints::*, *};
11
12#[derive(Debug, Clone, PartialEq)]
14pub enum Optionality<T> {
15 Required,
17 Optional,
19 Default(T),
21}
22
23impl<T> Optionality<T> {
24 pub fn default(&self) -> Option<&T> {
26 match self {
27 Optionality::Required | Optionality::Optional => None,
28 Optionality::Default(d) => Some(d),
29 }
30 }
31
32 pub fn default_mut(&mut self) -> Option<&mut T> {
34 match self {
35 Optionality::Required | Optionality::Optional => None,
36 Optionality::Default(d) => Some(d),
37 }
38 }
39}
40
41pub trait IterNameTypes {
44 fn iter_name_types(&self) -> impl Iterator<Item = (&str, &ASN1Type)>;
45}
46
47pub trait MemberOrOption {
49 const IS_CHOICE_OPTION: bool;
50 fn name(&self) -> &str;
51 fn ty(&self) -> &ASN1Type;
52 fn constraints(&self) -> &[Constraint];
53 fn is_recursive(&self) -> bool;
54 fn tag(&self) -> Option<&AsnTag>;
55}
56
57pub trait Constrainable {
60 fn constraints(&self) -> &Vec<Constraint>;
62 fn constraints_mut(&mut self) -> &mut Vec<Constraint>;
64}
65
66macro_rules! constrainable {
67 ($typ:ty) => {
68 impl Constrainable for $typ {
69 fn constraints(&self) -> &Vec<Constraint> {
70 &self.constraints
71 }
72
73 fn constraints_mut(&mut self) -> &mut Vec<Constraint> {
74 &mut self.constraints
75 }
76 }
77 };
78}
79
80constrainable!(Boolean);
81constrainable!(Integer);
82constrainable!(BitString);
83constrainable!(OctetString);
84constrainable!(CharacterString);
85constrainable!(Real);
86constrainable!(SequenceOrSet);
87constrainable!(SequenceOrSetOf);
88constrainable!(Choice);
89constrainable!(Enumerated);
90constrainable!(DeclarationElsewhere);
91constrainable!(ObjectClassFieldType);
92constrainable!(Time);
93
94#[derive(Debug, Clone, PartialEq, Default)]
98pub struct Boolean {
99 pub constraints: Vec<Constraint>,
100}
101
102impl From<Option<Vec<Constraint>>> for Boolean {
103 fn from(value: Option<Vec<Constraint>>) -> Self {
104 Self {
105 constraints: value.unwrap_or_default(),
106 }
107 }
108}
109
110#[derive(Debug, Clone, PartialEq, Default)]
114pub struct Integer {
115 pub constraints: Vec<Constraint>,
116 pub distinguished_values: Option<Vec<DistinguishedValue>>,
117}
118
119impl Integer {
120 pub fn int_type(&self) -> IntegerType {
123 self.constraints
124 .iter()
125 .fold(IntegerType::Unbounded, |acc, c| {
126 c.integer_constraints().max_restrictive(acc)
127 })
128 }
129}
130
131impl From<(i128, i128, bool)> for Integer {
132 fn from(value: (i128, i128, bool)) -> Self {
133 Self {
134 constraints: vec![Constraint::Subtype(ElementSetSpecs {
135 set: ElementOrSetOperation::Element(SubtypeElements::ValueRange {
136 min: Some(ASN1Value::Integer(value.0)),
137 max: Some(ASN1Value::Integer(value.1)),
138 extensible: value.2,
139 }),
140 extensible: value.2,
141 })],
142 distinguished_values: None,
143 }
144 }
145}
146
147impl From<(Option<i128>, Option<i128>, bool)> for Integer {
148 fn from(value: (Option<i128>, Option<i128>, bool)) -> Self {
149 Self {
150 constraints: vec![Constraint::Subtype(ElementSetSpecs {
151 set: ElementOrSetOperation::Element(SubtypeElements::ValueRange {
152 min: value.0.map(ASN1Value::Integer),
153 max: value.1.map(ASN1Value::Integer),
154 extensible: value.2,
155 }),
156 extensible: value.2,
157 })],
158 distinguished_values: None,
159 }
160 }
161}
162
163impl
164 From<(
165 &str,
166 Option<Vec<DistinguishedValue>>,
167 Option<Vec<Constraint>>,
168 )> for Integer
169{
170 fn from(
171 value: (
172 &str,
173 Option<Vec<DistinguishedValue>>,
174 Option<Vec<Constraint>>,
175 ),
176 ) -> Self {
177 Self {
178 constraints: value.2.unwrap_or_default(),
179 distinguished_values: value.1,
180 }
181 }
182}
183
184#[derive(Debug, Clone, PartialEq)]
188pub struct Real {
189 pub constraints: Vec<Constraint>,
190}
191
192impl From<Option<Vec<Constraint>>> for Real {
193 fn from(value: Option<Vec<Constraint>>) -> Self {
194 Self {
195 constraints: value.unwrap_or_default(),
196 }
197 }
198}
199
200#[derive(Debug, Clone, PartialEq)]
204pub struct GeneralizedTime {
205 pub constraints: Vec<Constraint>,
206}
207
208#[derive(Debug, Clone, PartialEq)]
212pub struct UTCTime {
213 pub constraints: Vec<Constraint>,
214}
215
216#[derive(Debug, Clone, PartialEq)]
220pub struct OctetString {
221 pub constraints: Vec<Constraint>,
222}
223
224impl From<Option<Vec<Constraint>>> for OctetString {
225 fn from(value: Option<Vec<Constraint>>) -> Self {
226 OctetString {
227 constraints: value.unwrap_or_default(),
228 }
229 }
230}
231
232#[derive(Debug, Clone, PartialEq)]
237pub struct BitString {
238 pub constraints: Vec<Constraint>,
239 pub distinguished_values: Option<Vec<DistinguishedValue>>,
240}
241
242impl From<(Option<Vec<DistinguishedValue>>, Option<Vec<Constraint>>)> for BitString {
243 fn from(value: (Option<Vec<DistinguishedValue>>, Option<Vec<Constraint>>)) -> Self {
244 BitString {
245 constraints: value.1.unwrap_or_default(),
246 distinguished_values: value.0,
247 }
248 }
249}
250
251#[derive(Debug, Clone, PartialEq)]
255pub struct ObjectIdentifier {
256 pub constraints: Vec<Constraint>,
257}
258
259impl From<Option<Vec<Constraint>>> for ObjectIdentifier {
260 fn from(value: Option<Vec<Constraint>>) -> Self {
261 ObjectIdentifier {
262 constraints: value.unwrap_or_default(),
263 }
264 }
265}
266
267#[derive(Debug, Clone, PartialEq)]
271pub struct Time {
272 pub constraints: Vec<Constraint>,
273}
274
275impl From<Option<Vec<Constraint>>> for Time {
276 fn from(value: Option<Vec<Constraint>>) -> Self {
277 Time {
278 constraints: value.unwrap_or_default(),
279 }
280 }
281}
282
283#[derive(Debug, Clone, PartialEq)]
288pub struct CharacterString {
289 pub constraints: Vec<Constraint>,
290 pub ty: CharacterStringType,
291}
292
293impl From<(&str, Option<Vec<Constraint>>)> for CharacterString {
294 fn from(value: (&str, Option<Vec<Constraint>>)) -> Self {
295 CharacterString {
296 constraints: value.1.unwrap_or_default(),
297 ty: value.0.into(),
298 }
299 }
300}
301
302#[derive(Debug, Clone, PartialEq)]
310pub struct SequenceOrSetOf {
311 pub constraints: Vec<Constraint>,
312 pub element_type: Box<ASN1Type>,
326 pub element_tag: Option<AsnTag>,
327 pub is_recursive: bool,
328}
329
330impl From<(Option<Vec<Constraint>>, (Option<AsnTag>, ASN1Type))> for SequenceOrSetOf {
331 fn from(value: (Option<Vec<Constraint>>, (Option<AsnTag>, ASN1Type))) -> Self {
332 Self {
333 constraints: value.0.unwrap_or_default(),
334 element_type: Box::new(value.1 .1),
335 element_tag: value.1 .0,
336 is_recursive: false,
337 }
338 }
339}
340
341#[derive(Debug, Clone, PartialEq)]
349pub struct SequenceOrSet {
350 pub components_of: Vec<String>,
351 pub extensible: Option<usize>,
352 pub constraints: Vec<Constraint>,
353 pub members: Vec<SequenceOrSetMember>,
354}
355
356impl IterNameTypes for SequenceOrSet {
357 fn iter_name_types(&self) -> impl Iterator<Item = (&str, &ASN1Type)> {
358 self.members.iter().map(|m| (m.name.as_str(), &m.ty))
359 }
360}
361
362impl
363 From<(
364 (
365 Vec<SequenceComponent>,
366 Option<ExtensionMarker>,
367 Option<Vec<SequenceComponent>>,
368 ),
369 Option<Vec<Constraint>>,
370 )> for SequenceOrSet
371{
372 fn from(
373 mut value: (
374 (
375 Vec<SequenceComponent>,
376 Option<ExtensionMarker>,
377 Option<Vec<SequenceComponent>>,
378 ),
379 Option<Vec<Constraint>>,
380 ),
381 ) -> Self {
382 let index_of_first_extension = value.0 .0.len();
383 value.0 .0.append(&mut value.0 .2.unwrap_or_default());
384 let mut components_of = vec![];
385 let mut members = vec![];
386 for comp in value.0 .0 {
387 match comp {
388 SequenceComponent::Member(m) => members.push(m),
389 SequenceComponent::ComponentsOf(c) => components_of.push(c),
390 }
391 }
392 SequenceOrSet {
393 components_of,
394 constraints: value.1.unwrap_or_default(),
395 extensible: value.0 .1.map(|_| index_of_first_extension),
396 members,
397 }
398 }
399}
400
401impl
402 From<(
403 (
404 Vec<SequenceOrSetMember>,
405 Option<ExtensionMarker>,
406 Option<Vec<SequenceOrSetMember>>,
407 ),
408 Option<Vec<Constraint>>,
409 )> for SequenceOrSet
410{
411 fn from(
412 mut value: (
413 (
414 Vec<SequenceOrSetMember>,
415 Option<ExtensionMarker>,
416 Option<Vec<SequenceOrSetMember>>,
417 ),
418 Option<Vec<Constraint>>,
419 ),
420 ) -> Self {
421 let index_of_first_extension = value.0 .0.len();
422 value.0 .0.append(&mut value.0 .2.unwrap_or_default());
423 SequenceOrSet {
424 components_of: vec![],
425 constraints: value.1.unwrap_or_default(),
426 extensible: value.0 .1.map(|_| index_of_first_extension),
427 members: value.0 .0,
428 }
429 }
430}
431
432#[cfg_attr(test, derive(EnumDebug))]
446#[cfg_attr(not(test), derive(Debug))]
447#[allow(clippy::large_enum_variant)]
448#[derive(Clone, PartialEq)]
449pub enum SequenceComponent {
450 Member(SequenceOrSetMember),
451 ComponentsOf(String),
452}
453
454#[derive(Debug, Clone, PartialEq)]
493pub struct SequenceOrSetMember {
494 pub name: String,
495 pub tag: Option<AsnTag>,
496 pub ty: ASN1Type,
497 pub optionality: Optionality<ASN1Value>,
498 pub is_recursive: bool,
499 pub constraints: Vec<Constraint>,
500}
501
502impl MemberOrOption for SequenceOrSetMember {
503 fn name(&self) -> &str {
504 &self.name
505 }
506
507 fn ty(&self) -> &ASN1Type {
508 &self.ty
509 }
510
511 fn constraints(&self) -> &[Constraint] {
512 &self.constraints
513 }
514
515 fn is_recursive(&self) -> bool {
516 self.is_recursive
517 }
518
519 fn tag(&self) -> Option<&AsnTag> {
520 self.tag.as_ref()
521 }
522
523 const IS_CHOICE_OPTION: bool = false;
524}
525
526impl
527 From<(
528 &str,
529 Option<AsnTag>,
530 ASN1Type,
531 Option<Vec<Constraint>>,
532 Optionality<ASN1Value>,
533 )> for SequenceOrSetMember
534{
535 fn from(
536 value: (
537 &str,
538 Option<AsnTag>,
539 ASN1Type,
540 Option<Vec<Constraint>>,
541 Optionality<ASN1Value>,
542 ),
543 ) -> Self {
544 SequenceOrSetMember {
545 name: value.0.into(),
546 tag: value.1,
547 ty: value.2,
548 optionality: value.4,
549 is_recursive: false,
550 constraints: value.3.unwrap_or_default(),
551 }
552 }
553}
554
555#[derive(Debug, Clone, PartialEq)]
559pub struct Choice {
560 pub extensible: Option<usize>,
561 pub options: Vec<ChoiceOption>,
562 pub constraints: Vec<Constraint>,
563}
564
565impl IterNameTypes for Choice {
566 fn iter_name_types(&self) -> impl Iterator<Item = (&str, &ASN1Type)> {
567 self.options.iter().map(|o| (o.name.as_str(), &o.ty))
568 }
569}
570
571impl
572 From<(
573 Vec<ChoiceOption>,
574 Option<ExtensionMarker>,
575 Option<Vec<ChoiceOption>>,
576 )> for Choice
577{
578 fn from(
579 mut value: (
580 Vec<ChoiceOption>,
581 Option<ExtensionMarker>,
582 Option<Vec<ChoiceOption>>,
583 ),
584 ) -> Self {
585 let index_of_first_extension = value.0.len();
586 value.0.append(&mut value.2.unwrap_or_default());
587 Choice {
588 extensible: value.1.map(|_| index_of_first_extension),
589 options: value.0,
590 constraints: vec![],
591 }
592 }
593}
594
595#[derive(Debug, Clone, PartialEq)]
623pub struct ChoiceOption {
624 pub name: String,
625 pub tag: Option<AsnTag>,
626 pub ty: ASN1Type,
627 pub constraints: Vec<Constraint>,
628 pub is_recursive: bool,
629}
630
631impl MemberOrOption for ChoiceOption {
632 fn name(&self) -> &str {
633 &self.name
634 }
635
636 fn ty(&self) -> &ASN1Type {
637 &self.ty
638 }
639
640 fn constraints(&self) -> &[Constraint] {
641 &self.constraints
642 }
643
644 fn is_recursive(&self) -> bool {
645 self.is_recursive
646 }
647
648 fn tag(&self) -> Option<&AsnTag> {
649 self.tag.as_ref()
650 }
651
652 const IS_CHOICE_OPTION: bool = true;
653}
654
655impl From<(&str, Option<AsnTag>, ASN1Type, Option<Vec<Constraint>>)> for ChoiceOption {
656 fn from(value: (&str, Option<AsnTag>, ASN1Type, Option<Vec<Constraint>>)) -> Self {
657 ChoiceOption {
658 name: value.0.into(),
659 tag: value.1,
660 ty: value.2,
661 constraints: value.3.unwrap_or_default(),
662 is_recursive: false,
663 }
664 }
665}
666
667#[derive(Debug, Clone, PartialEq)]
671pub struct Enumerated {
672 pub members: Vec<Enumeral>,
673 pub extensible: Option<usize>,
674 pub constraints: Vec<Constraint>,
675}
676
677impl
678 From<(
679 Vec<Enumeral>,
680 Option<ExtensionMarker>,
681 Option<Vec<Enumeral>>,
682 )> for Enumerated
683{
684 fn from(
685 mut value: (
686 Vec<Enumeral>,
687 Option<ExtensionMarker>,
688 Option<Vec<Enumeral>>,
689 ),
690 ) -> Self {
691 let index_of_first_extension = value.0.len();
692 value.0.append(&mut value.2.unwrap_or_default());
693 Enumerated {
694 members: value.0,
695 extensible: value.1.map(|_| index_of_first_extension),
696 constraints: vec![],
697 }
698 }
699}
700
701#[derive(Debug, Clone, PartialEq)]
722pub struct Enumeral {
723 pub name: String,
724 pub description: Option<String>,
725 pub index: i128,
726}
727
728#[derive(Debug, Clone, PartialEq)]
732pub struct DistinguishedValue {
733 pub name: String,
734 pub value: i128,
735}
736
737impl From<(&str, i128)> for DistinguishedValue {
738 fn from(value: (&str, i128)) -> Self {
739 Self {
740 name: value.0.into(),
741 value: value.1,
742 }
743 }
744}
745
746#[derive(Debug, Clone, PartialEq)]
749pub struct ChoiceSelectionType {
750 pub choice_name: String,
751 pub selected_option: String,
752}
753
754impl From<(&str, &str)> for ChoiceSelectionType {
755 fn from(value: (&str, &str)) -> Self {
756 Self {
757 choice_name: value.1.into(),
758 selected_option: value.0.into(),
759 }
760 }
761}