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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
//! Specification of the Autosar arxml file format in the form of rust data structures
//!
//! This crate exists to support the autosar-data crate.
//!
//! The Autosar data model is originally specified as .xsd files - one for each version of the standard.
//! All these separate xsd files were parsed into data structures and combined; this crate contains the
//! combined specification data of all 19 Autosar 4 standard revisions.
//!
//! ## Supported standards:
//!
//! | xsd filename      | description               |
//! |-------------------|---------------------------|
//! | AUTOSAR_4-0-1.xsd | AUTOSAR 4.0.1             |
//! | AUTOSAR_4-0-2.xsd | AUTOSAR 4.0.2             |
//! | AUTOSAR_4-0-3.xsd | AUTOSAR 4.0.3             |
//! | AUTOSAR_4-1-1.xsd | AUTOSAR 4.1.1             |
//! | AUTOSAR_4-1-2.xsd | AUTOSAR 4.1.2             |
//! | AUTOSAR_4-1-3.xsd | AUTOSAR 4.1.3             |
//! | AUTOSAR_4-2-1.xsd | AUTOSAR 4.2.1             |
//! | AUTOSAR_4-2-2.xsd | AUTOSAR 4.2.2             |
//! | AUTOSAR_4-3-0.xsd | AUTOSAR 4.3.0             |
//! | AUTOSAR_00042.xsd | AUTOSAR Adaptive 17-03    |
//! | AUTOSAR_00043.xsd | AUTOSAR Adaptive 17-10    |
//! | AUTOSAR_00044.xsd | AUTOSAR Classic 4.3.1     |
//! | AUTOSAR_00045.xsd | AUTOSAR Adaptive 18-03    |
//! | AUTOSAR_00046.xsd | AUTOSAR Classic 4.4.0 / Adaptive 18-10 |
//! | AUTOSAR_00047.xsd | AUTOSAR Adaptive 19-03    |
//! | AUTOSAR_00048.xsd | AUTOSAR 4.5.0             |
//! | AUTOSAR_00049.xsd | AUTOSAR R20-11            |
//! | AUTOSAR_00050.xsd | AUTOSAR R21-11            |
//! | AUTOSAR_00051.xsd | AUTOSAR R22-11            |
//!
//! ## Using the crate
//!
//! The main datatype is the [ElementType]. The type of the <AUTOSAR> element at the root of every arxml file is
//! available as ElementType::ROOT.
//!
//! ## Note
//!
//! It is not possible to directly convert between [ElementName]s and [ElementType]s, since this is an n:m mapping.
//! If the content of two differently named elements is structurally identical, then they have the same [ElementType];
//! on the other side there are several elements that have different content depending in the context in which they appear.
//!
//! ## Example
//!
//! ```
//! # use autosar_data_specification::*;
//! # use std::str::FromStr;
//! let root_type = ElementType::ROOT;
//!
//! // parsing an element
//! let element_name_text = "AR-PACKAGES";
//! let element_name = ElementName::from_str(element_name_text).unwrap();
//! assert_eq!(element_name, ElementName::ArPackages);
//!
//! let version_mask = AutosarVersion::Autosar_4_3_0 as u32;
//! if let Some((element_type, index_list)) = root_type.find_sub_element(
//!     element_name,
//!     version_mask
//! ) {
//!     // parsing an attribute
//!     let attribute_name = AttributeName::from_str("UUID").unwrap();
//!     if let Some(attribute_spec) = element_type.find_attribute_spec(attribute_name) {
//!         // ...
//!     }
//!
//!     // ...
//! }
//! ```

mod attributename;
mod autosarversion;
mod elementname;
mod enumitem;
mod regex;
mod specification;

use std::ops::BitXor;

pub use attributename::AttributeName;
pub use autosarversion::AutosarVersion;
pub use elementname::ElementName;
pub use enumitem::EnumItem;
use specification::*;

/// ElementMultiplicity specifies how often a single child element may occur within its parent
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ElementMultiplicity {
    ZeroOrOne,
    One,
    Any,
}

/// The ContentMode specifies what content may occur inside an element
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ContentMode {
    /// Sequence: an ordered sequence of elements
    Sequence,
    /// Choice: a single element must be chosen from multiple options.
    /// If the multiplicity of the chosen element is `Any` then it may repeat so there might still be more than one sub element
    Choice,
    /// Bag: From a list of choices, choose a sub element any number of times.
    /// In this ContentMode all allowed sub elements may occur any number of times and in any order
    Bag,
    /// Characters: no sub elements are permitted, there can only be character content
    Characters,
    /// Mixed: both characters content and sub elements are allowed, in any order. It's basically like HTML
    Mixed,
}

/// Specifies the data type and restrictions of the character data in an element or attribute
pub enum CharacterDataSpec {
    /// The character data is an enum value; valid values are given in items and the character data must match one of these
    Enum {
        items: &'static [(EnumItem, u32)],
    },
    /// The character data is restricted to match a regex, which is given in text form in the filed regex.
    /// The check_fn is a function that validates input according to the regex.
    /// If a max_length is given, then it restricts the length (in bytes).
    Pattern {
        check_fn: fn(&[u8]) -> bool,
        regex: &'static str,
        max_length: Option<usize>,
    },
    /// An arbitrary string; if preserve whitepace is set, then whitespace should be preserved during parsing (see the XML standard)
    String {
        preserve_whitespace: bool,
        max_length: Option<usize>,
    },
    UnsignedInteger,
    Double,
}

/// specification of an attribute
pub struct AttributeSpec {
    /// data type of the attribute content
    pub spec: &'static CharacterDataSpec,
    /// is the attribute required to be present in it's containing element
    pub required: bool,
    /// in which autosar version(s) is this attribute valid. This field is a bitmask.
    pub version: u32,
}

/// ElementType is an abstraction over element types in the specification.
///
/// It provides no public fields, but it has methods to get all the info needed to parse an arxml element.
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
pub struct ElementType(usize);

enum SubElement {
    Element {
        name: ElementName,
        elemtype: usize,
        multiplicity: ElementMultiplicity,
    },
    Group {
        groupid: usize,
    },
}

struct ElementSpec {
    sub_elements: (u16, u16),
    sub_element_ver: u16,
    attributes: (u16, u16),
    attributes_ver: u16,
    character_data: Option<u16>,
    mode: ContentMode,
    ordered: bool,
    splittable: u32,
    ref_by: &'static [EnumItem],
}

impl AutosarVersion {
    pub fn compatible(&self, version_mask: u32) -> bool {
        version_mask & *self as u32 != 0
    }
}

impl ElementType {
    fn get_sub_elements(&self) -> &'static [SubElement] {
        let (idx_start, idx_end) = self.get_sub_element_idx();
        &SUBELEMENTS[idx_start..idx_end]
    }

    fn get_sub_element_idx(&self) -> (usize, usize) {
        let (start, end) = DATATYPES[self.0].sub_elements;
        (start as usize, end as usize)
    }

    fn get_sub_element_ver(&self) -> usize {
        DATATYPES[self.0].sub_element_ver as usize
    }

    fn get_attributes_idx(&self) -> (usize, usize) {
        let (start, end) = DATATYPES[self.0].attributes;
        (start as usize, end as usize)
    }

    fn get_attributes_ver(&self) -> usize {
        DATATYPES[self.0].attributes_ver as usize
    }

    /// get the spec of a sub element from the index list
    fn get_sub_element_spec<'a>(&self, element_indices: &[usize]) -> Option<(&'a SubElement, u32)> {
        let spec = self.get_sub_elements();
        let ver_list_start = self.get_sub_element_ver();
        if !element_indices.is_empty() {
            let mut current_spec = spec;
            let mut current_ver_list_start = ver_list_start;
            // go through the hierarchy of groups: only the final index in element_indices can refer to a SubElement::Element
            for idx in 0..(element_indices.len() - 1) {
                match &current_spec[element_indices[idx]] {
                    SubElement::Element { .. } => {
                        // elements are not allowed here
                        return None;
                    }
                    SubElement::Group { groupid } => {
                        current_spec = ElementType(*groupid).get_sub_elements();
                        current_ver_list_start = ElementType(*groupid).get_sub_element_ver();
                    }
                }
            }

            let last_idx = *element_indices.last().unwrap();
            Some((&current_spec[last_idx], VERSION_INFO[current_ver_list_start + last_idx]))
        } else {
            None
        }
    }

    /// get the version mask of a sub element
    pub fn get_sub_element_version_mask(&self, element_indices: &[usize]) -> Option<u32> {
        match self.get_sub_element_spec(element_indices) {
            Some((_, version_mask)) => Some(version_mask),
            _ => None,
        }
    }

    /// get the multiplicity of a sub element within the current ElementType
    ///
    /// The sub element is identified by an indx list, as returned by `find_sub_element()`
    pub fn get_sub_element_multiplicity(&self, element_indices: &[usize]) -> Option<ElementMultiplicity> {
        match self.get_sub_element_spec(element_indices) {
            Some((SubElement::Element { multiplicity, .. }, _)) => Some(*multiplicity),
            _ => None,
        }
    }

    /// get the ContentMode of the container of a sub element of the current ElementType
    ///
    /// The sub element is identified by an index list, as returned by `find_sub_element()`
    pub fn get_sub_element_container_mode(&self, element_indices: &[usize]) -> ContentMode {
        if element_indices.len() < 2 {
            // length == 1: this element is a direct sub element, without any groups;
            DATATYPES[self.0].mode
        } else {
            let len = element_indices.len() - 1;
            if let Some((SubElement::Group { groupid }, _)) = self.get_sub_element_spec(&element_indices[..len]) {
                DATATYPES[*groupid].mode
            } else {
                panic!("impossible: element container is not a group");
            }
        }
    }

    /// find a sub element in the specification of the current ElementType
    ///
    /// Note: Version here is NOT an AutosarVersion, it is a u32. it is a bitmask which can contain multiple AutosarVersions, or any version by using u32::MAX
    ///
    /// In almost all cases this is simple: there is a flat list of sub elements that either contains the target_name or not.
    /// The result in those simple cases is a vec with one entry which is the index of the element in the list.
    /// There are a handfull of complicated situations though, where the list of sub elements contains groups of
    /// elements that have a different ContentMode than the other elements.
    ///
    /// For example:
    /// ```text
    ///     PRM-CHAR (Sequence)
    ///      -> Element: COND
    ///      -> Group (Choice)
    ///         -> Group (Sequence)
    ///             -> Group (Choice)
    ///                 -> Group (Sequence)
    ///                     -> Element: ABS
    ///                     -> Element: TOL
    ///                 -> Group (Sequence)
    ///                     -> Element: MIN
    ///                     -> Element: TYP
    ///                     -> Element: MAX
    ///             -> Element: PRM-UNIT
    ///         -> Element: TEXT
    ///      -> Element: REMARK
    /// ```
    /// When searching for TOL in PRM-CHAR, the result should be Some(vec![1, 0, 0, 0, 1])!
    pub fn find_sub_element(&self, target_name: ElementName, version: u32) -> Option<(ElementType, Vec<usize>)> {
        let spec = self.get_sub_elements();
        for (cur_pos, sub_element) in spec.iter().enumerate() {
            match sub_element {
                SubElement::Element { name, elemtype, .. } => {
                    let ver_info_start = self.get_sub_element_ver();
                    let version_mask = VERSION_INFO[ver_info_start + cur_pos];
                    if (*name == target_name) && (version & version_mask != 0) {
                        return Some((ElementType(*elemtype), vec![cur_pos]));
                    }
                }
                SubElement::Group { groupid } => {
                    let group = ElementType(*groupid);
                    if let Some((elemtype, mut indices)) = group.find_sub_element(target_name, version) {
                        indices.insert(0, cur_pos);
                        return Some((elemtype, indices));
                    }
                }
            }
        }
        None
    }

    /// find the commmon group of two subelements of the current ElementType
    ///
    /// The subelements are identified by their index lists, returned by find_sub_element().
    ///
    /// In simple cases without sub-groups of elements, the "common group" is simply the current ElementType.
    pub fn find_common_group(&self, element_indices: &[usize], element_indices2: &[usize]) -> ElementType {
        let mut result = self.0;
        let mut prefix_len = 0;
        while element_indices.len() > prefix_len
            && element_indices2.len() > prefix_len
            && element_indices[prefix_len] == element_indices2[prefix_len]
        {
            let sub_elem = &ElementType(result).get_sub_elements()[element_indices[prefix_len]];
            match sub_elem {
                SubElement::Element { .. } => return ElementType(result),
                SubElement::Group { groupid } => {
                    result = *groupid;
                }
            }
            prefix_len += 1;
        }

        ElementType(result)
    }

    /// are elements of this ElementType named in any Autosar version
    pub fn is_named(&self) -> bool {
        self.short_name_version_mask().is_some()
    }

    pub(crate) fn short_name_version_mask(&self) -> Option<u32> {
        let (start_idx, end_idx) = DATATYPES[self.0].sub_elements;
        if start_idx != end_idx {
            if let SubElement::Element {
                name: ElementName::ShortName,
                ..
            } = SUBELEMENTS[start_idx as usize]
            {
                Some(VERSION_INFO[DATATYPES[self.0].sub_element_ver as usize])
            } else {
                None
            }
        } else {
            None
        }
    }

    /// are elements of this elementType named in the given Autosar version
    ///
    /// Named elements must have a SHORT-NAME sub element. For some elements this
    /// depends on the Autosar version.
    ///
    /// One example of this is END-2-END-METHOD-PROTECTION-PROPS, which was first
    /// defined in Autosar_00048, but only has a name in Autosar_00050.
    pub fn is_named_in_version(&self, version: AutosarVersion) -> bool {
        self.short_name_version_mask()
            .map_or(false, |ver_mask| version.compatible(ver_mask))
    }

    /// is the ElementType a reference
    pub fn is_ref(&self) -> bool {
        if let Some(idx) = DATATYPES[self.0].character_data {
            idx == REFERENCE_TYPE_IDX
        } else {
            false
        }
    }

    /// get the content mode for this ElementType
    pub fn content_mode(&self) -> ContentMode {
        DATATYPES[self.0].mode
    }

    /// get the character data spec for this ElementType
    pub fn chardata_spec(&self) -> Option<&'static CharacterDataSpec> {
        if let Some(chardata_id) = DATATYPES[self.0].character_data {
            Some(&CHARACTER_DATA[chardata_id as usize])
        } else {
            None
        }
    }

    /// find the spec for a single attribute by name
    pub fn find_attribute_spec(&self, attrname: AttributeName) -> Option<AttributeSpec> {
        let (idx_start, idx_end) = self.get_attributes_idx();
        let attributes = &ATTRIBUTES[idx_start..idx_end];
        if let Some((find_pos, (_, chardata_id, required))) =
            attributes.iter().enumerate().find(|(_, (name, ..))| *name == attrname)
        {
            let idx_ver_start = self.get_attributes_ver();
            let version = VERSION_INFO[idx_ver_start + find_pos];
            Some(AttributeSpec {
                spec: &CHARACTER_DATA[*chardata_id as usize],
                required: *required,
                version,
            })
        } else {
            None
        }
    }

    /// create an iterator over all attribute definitions in the current ElementType
    pub fn attribute_spec_iter(&self) -> AttrDefinitionsIter {
        AttrDefinitionsIter {
            type_id: self.0,
            pos: 0,
        }
    }

    /// create an iterator over all sub elements of the current ElementType
    pub fn sub_element_spec_iter(&self) -> SubelemDefinitionsIter {
        SubelemDefinitionsIter {
            type_id_stack: vec![self.0],
            indices: vec![0],
        }
    }

    /// Is the current ElementType ordered
    ///
    /// It his is true, then the position of the sub elements of this element is semantically meaningful
    /// and they may not be sorted / re-ordered without changing the meaning of the file.
    ///
    /// An example of this is ARGUMENTS in BSW-MODULE-ENTRY. ARGUMENTS is ordered, because each of its
    /// SW-SERVICE-ARG sub elements represents a function argument
    pub fn is_ordered(&self) -> bool {
        DATATYPES[self.0].ordered
    }

    /// Is the current ElementType splittable
    ///
    /// This function returns a bitfield that indicates in which versions (if any) the ElementType is marked as splittable.
    /// A splittable element may be split across multiple arxml files
    pub fn splittable(&self) -> u32 {
        DATATYPES[self.0].splittable
    }

    /// Is the current ElementType splittable in the given version
    ///
    /// A splittable element may be split across multiple arxml files
    pub fn splittable_in(&self, version: AutosarVersion) -> bool {
        (DATATYPES[self.0].splittable & (version as u32)) != 0
    }

    /// find the correct EnumItem to use in the DEST attribute when referring from this element to the other element
    ///
    /// Returns Some(enum_item) it the reference is possible, and None otherwise.
    ///
    /// Example:
    ///
    /// When referring to a `<CAN-TP-CONNECTION><IDENT><SHORT-NAME>foo...`
    /// the referrring `<PHYSICAL-REQUEST-REF [...]>` must set DEST="TP-CONNECTION-IDENT"
    pub fn reference_dest_value(&self, other: &ElementType) -> Option<EnumItem> {
        // this element must be a reference, and the other element must be identifiable, otherwise it is not a valid target
        if self.is_ref() && other.is_named() {
            let dest_spec = self.find_attribute_spec(AttributeName::Dest)?.spec;
            if let CharacterDataSpec::Enum { items } = dest_spec {
                for ref_target_value in DATATYPES[other.0].ref_by {
                    for (enumitem, _) in *items {
                        if ref_target_value == enumitem {
                            return Some(*ref_target_value);
                        }
                    }
                }
            }
        }
        None
    }

    /// ElementType::ROOT is the root ElementType of the Autosar arxml document: this is the ElementType of the AUTOSAR element
    pub const ROOT: Self = ElementType(ROOT_DATATYPE);
}

/// Iterator for attribute definitions
pub struct AttrDefinitionsIter {
    type_id: usize,
    pos: usize,
}

impl Iterator for AttrDefinitionsIter {
    type Item = (AttributeName, &'static CharacterDataSpec, bool);

    fn next(&mut self) -> Option<Self::Item> {
        let (idx_start, idx_end) = ElementType(self.type_id).get_attributes_idx();
        let cur_pos = self.pos;
        self.pos += 1;
        if idx_start + cur_pos < idx_end {
            let (name, chardata_id, required) = ATTRIBUTES[idx_start + cur_pos];
            Some((name, &CHARACTER_DATA[chardata_id as usize], required))
        } else {
            None
        }
    }
}

/// Iterator over sub element definitions
///
/// returns the tuple (name: ElementName, etype: ElementType, version_mask: u32, name_version_mask: u32)
pub struct SubelemDefinitionsIter {
    type_id_stack: Vec<usize>,
    indices: Vec<usize>,
}

impl Iterator for SubelemDefinitionsIter {
    // ElementName, elementType, version_mask, is_named
    type Item = (ElementName, ElementType, u32, u32);

    fn next(&mut self) -> Option<Self::Item> {
        if !self.type_id_stack.is_empty() {
            debug_assert_eq!(self.type_id_stack.len(), self.indices.len());

            let depth = self.indices.len() - 1;
            let current_type = self.type_id_stack[depth];
            let cur_pos = self.indices[depth];
            let (start_idx, end_idx) = ElementType(current_type).get_sub_element_idx();

            if start_idx + cur_pos < end_idx {
                match &SUBELEMENTS[start_idx + cur_pos] {
                    SubElement::Element { name, elemtype, .. } => {
                        // found an element, return it and advance
                        self.indices[depth] += 1;
                        let ver_idx = ElementType(current_type).get_sub_element_ver();
                        let version_mask = VERSION_INFO[ver_idx + cur_pos];
                        let is_named = ElementType(*elemtype).short_name_version_mask().unwrap_or(0);
                        Some((*name, ElementType(*elemtype), version_mask, is_named))
                    }
                    SubElement::Group { groupid } => {
                        // found a group, descend into it
                        self.type_id_stack.push(*groupid);
                        self.indices.push(0);
                        self.next()
                    }
                }
            } else {
                // finished processing this element / group; remove it from the stack
                self.indices.pop();
                self.type_id_stack.pop();
                if !self.indices.is_empty() {
                    self.indices[depth - 1] += 1;
                }
                self.next()
            }
        } else {
            None
        }
    }
}

// manually implement Debug for CharacterDataSpec; deriving it is not possible, because that fails on the check_fn field in ::Pattern.
// The check_fn field is simply omitted here.
impl std::fmt::Debug for CharacterDataSpec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Enum { items } => f.debug_struct("Enum").field("items", items).finish(),
            Self::Pattern { regex, max_length, .. } => f
                .debug_struct("Pattern")
                .field("regex", regex)
                .field("max_length", max_length)
                .finish(),
            Self::String {
                preserve_whitespace,
                max_length,
            } => f
                .debug_struct("String")
                .field("preserve_whitespace", preserve_whitespace)
                .field("max_length", max_length)
                .finish(),
            Self::UnsignedInteger => write!(f, "UnsignedInteger"),
            Self::Double => write!(f, "Double"),
        }
    }
}

/// expand a version mask (u32) to a list of versions in the mask
pub fn expand_version_mask(version_mask: u32) -> Vec<AutosarVersion> {
    let mut versions = vec![];
    for i in 0..u32::BITS {
        let val = 1u32 << i;
        if version_mask & val != 0 {
            if let Some(enum_value) = AutosarVersion::from_val(val) {
                versions.push(enum_value);
            }
        }
    }

    versions
}

pub(crate) fn hashfunc(mut data: &[u8]) -> (u32, u32, u32) {
    const HASHCONST1: u32 = 0x541C69B2; // these 4 constant values are not special, just random values
    const HASHCONST2: u32 = 0x3B17161B;

    let mut f1 = 0x33143C63u32;
    let mut f2 = 0x88B0B21Eu32;
    while data.len() >= 4 {
        let val = u32::from_ne_bytes(data[..4].try_into().unwrap());
        f1 = f1.rotate_left(5).bitxor(val).wrapping_mul(HASHCONST1);
        f2 = f2.rotate_left(6).bitxor(val).wrapping_mul(HASHCONST2);
        data = &data[4..];
    }
    if data.len() >= 2 {
        let val = u16::from_ne_bytes(data[..2].try_into().unwrap()) as u32;
        f1 = f1.rotate_left(5).bitxor(val).wrapping_mul(HASHCONST1);
        f2 = f2.rotate_left(6).bitxor(val).wrapping_mul(HASHCONST2);
        data = &data[2..];
    }
    if !data.is_empty() {
        f1 = f1.rotate_left(5).bitxor(data[0] as u32).wrapping_mul(HASHCONST1);
        f2 = f2.rotate_left(6).bitxor(data[0] as u32).wrapping_mul(HASHCONST2);
    }
    let g = f1.bitxor(f2);
    (g, f1, f2)
}

#[cfg(test)]
mod test {
    use std::{collections::HashSet, str::FromStr};

    use super::*;

    fn get_prm_char_element_type() -> ElementType {
        let (ar_packages_type, _) = ElementType::ROOT
            .find_sub_element(ElementName::ArPackages, u32::MAX)
            .unwrap();
        let (ar_package_type, _) = ar_packages_type
            .find_sub_element(ElementName::ArPackage, u32::MAX)
            .unwrap();
        let (elements_type, _) = ar_package_type
            .find_sub_element(ElementName::Elements, u32::MAX)
            .unwrap();
        let (documentation_type, _) = elements_type
            .find_sub_element(ElementName::Documentation, u32::MAX)
            .unwrap();
        let (documentation_content_type, _) = documentation_type
            .find_sub_element(ElementName::DocumentationContent, u32::MAX)
            .unwrap();
        let (prms_type, _) = documentation_content_type
            .find_sub_element(ElementName::Prms, u32::MAX)
            .unwrap();
        let (prm_type, _) = prms_type.find_sub_element(ElementName::Prm, u32::MAX).unwrap();
        let (prm_char_type, _) = prm_type.find_sub_element(ElementName::PrmChar, u32::MAX).unwrap();

        prm_char_type
    }

    #[test]
    fn find_sub_element() {
        let prm_char_type = get_prm_char_element_type();
        let (_, indices) = prm_char_type.find_sub_element(ElementName::Tol, 0xffffffff).unwrap();
        assert_eq!(indices, vec![1, 0, 0, 0, 1]);
    }

    #[test]
    fn find_sub_element_version_dependent() {
        let (ar_packages_type, _) = ElementType::ROOT
            .find_sub_element(ElementName::ArPackages, u32::MAX)
            .unwrap();
        let (ar_package_type, _) = ar_packages_type
            .find_sub_element(ElementName::ArPackage, u32::MAX)
            .unwrap();
        let (elements_type, _) = ar_package_type
            .find_sub_element(ElementName::Elements, u32::MAX)
            .unwrap();
        let (sw_base_type_type, _) = elements_type
            .find_sub_element(ElementName::SwBaseType, u32::MAX)
            .unwrap();
        let (_, indices) = sw_base_type_type
            .find_sub_element(ElementName::BaseTypeSize, AutosarVersion::Autosar_4_0_1 as u32)
            .unwrap();
        assert_eq!(indices, vec![11, 0]);

        let (_, indices) = sw_base_type_type
            .find_sub_element(ElementName::BaseTypeSize, AutosarVersion::Autosar_4_1_1 as u32)
            .unwrap();
        assert_eq!(indices, vec![13]);
    }

    #[test]
    fn get_sub_element_spec() {
        let prm_char_type = get_prm_char_element_type();
        let (abs_type, indices) = prm_char_type.find_sub_element(ElementName::Abs, u32::MAX).unwrap();
        let sub_elem_spec = prm_char_type.get_sub_element_spec(&indices);
        let (sub_element, _) = sub_elem_spec.unwrap();
        if let SubElement::Element { name, elemtype, .. } = sub_element {
            assert_eq!(*name, ElementName::Abs);
            assert_eq!(ElementType(*elemtype), abs_type);
        }

        // the element_indices passed to get_sub_element_spec may not be empty
        let sub_elem_spec2 = prm_char_type.get_sub_element_spec(&[]);
        assert!(sub_elem_spec2.is_none());
        // element_indices is nonsense
        let sub_elem_spec2 = prm_char_type.get_sub_element_spec(&[0, 0, 0, 0, 0, 0, 0, 0, 0]);
        assert!(sub_elem_spec2.is_none());
    }

    #[test]
    fn get_sub_element_version_mask() {
        let prm_char_type = get_prm_char_element_type();
        let (_, indices) = prm_char_type.find_sub_element(ElementName::Abs, u32::MAX).unwrap();
        let sub_elem_spec = prm_char_type.get_sub_element_spec(&indices).unwrap();
        let version_mask2 = prm_char_type.get_sub_element_version_mask(&indices).unwrap();
        let (_, version_mask) = sub_elem_spec;
        assert_eq!(version_mask, version_mask2);

        let no_result = prm_char_type.get_sub_element_version_mask(&[]);
        assert!(no_result.is_none());
    }

    #[test]
    fn get_sub_element_multiplicity() {
        let prm_char_type = get_prm_char_element_type();
        let (_, indices) = prm_char_type.find_sub_element(ElementName::Abs, u32::MAX).unwrap();
        let sub_elem_spec = prm_char_type.get_sub_element_spec(&indices).unwrap().0;
        let multiplicity2 = prm_char_type.get_sub_element_multiplicity(&indices).unwrap();
        if let SubElement::Element { multiplicity, .. } = sub_elem_spec {
            assert_eq!(*multiplicity, multiplicity2);
        }

        let no_result = prm_char_type.get_sub_element_multiplicity(&[]);
        assert!(no_result.is_none());
    }

    #[test]
    fn get_sub_element_container_mode() {
        let prm_char_type = get_prm_char_element_type();
        let (_, indices) = prm_char_type.find_sub_element(ElementName::Abs, u32::MAX).unwrap();
        let mode = prm_char_type.get_sub_element_container_mode(&indices);
        assert_eq!(mode, ContentMode::Sequence);
    }

    #[test]
    fn find_common_group() {
        let prm_char_type = get_prm_char_element_type();
        let (_, indices_abs) = prm_char_type.find_sub_element(ElementName::Abs, u32::MAX).unwrap();
        let (_, indices_tol) = prm_char_type.find_sub_element(ElementName::Tol, u32::MAX).unwrap();
        let (_, indices_min) = prm_char_type.find_sub_element(ElementName::Min, u32::MAX).unwrap();
        // see the documentation on find_sub_element for the complex structure under PRM-CHAR
        // ABS and TOL share a sequence group (top level)
        let group1 = prm_char_type.find_common_group(&indices_abs, &indices_tol);
        assert_eq!(group1.content_mode(), ContentMode::Sequence);
        // ABS and MIN have the second level choice group in common
        let group2 = prm_char_type.find_common_group(&indices_abs, &indices_min);
        assert_eq!(group2.content_mode(), ContentMode::Choice);
    }

    #[test]
    fn find_attribute_spec() {
        let AttributeSpec {
            spec,
            required,
            version,
        } = ElementType::ROOT.find_attribute_spec(AttributeName::xmlns).unwrap();
        let spec_dbgstr = format!("{:#?}", spec);
        assert!(!spec_dbgstr.is_empty());
        // xmlns in AUTOSAR is required
        assert!(required);
        // must be specified both in the first and latest versions (and every one in between - not tested)
        assert_ne!(version & AutosarVersion::Autosar_00050 as u32, 0);
        assert_ne!(version & AutosarVersion::Autosar_4_0_1 as u32, 0);
    }

    #[test]
    fn subelement_definition_iterator() {
        let (ar_packages_type, _) = ElementType::ROOT
            .find_sub_element(ElementName::ArPackages, u32::MAX)
            .unwrap();
        let (ar_package_type, _) = ar_packages_type
            .find_sub_element(ElementName::ArPackage, u32::MAX)
            .unwrap();
        let (elements_type, _) = ar_package_type
            .find_sub_element(ElementName::Elements, u32::MAX)
            .unwrap();

        let se_iter = elements_type.sub_element_spec_iter();
        assert_eq!(se_iter.count(), 586); // this test breaks when support for new versions is added

        let prm_char_type = get_prm_char_element_type();
        let pc_iter = prm_char_type.sub_element_spec_iter();
        // not all items in the sub element spec are compatible with the latest Autosar version, count only the ones that are compatible
        let compatible_count = pc_iter
            .filter(|(_, _, version_mask, _)| AutosarVersion::Autosar_00050.compatible(*version_mask))
            .count();
        assert_eq!(compatible_count, 9);
    }

    #[test]
    fn autosar_version() {
        // does from_str work correctly?
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-0-1.xsd").unwrap(),
            AutosarVersion::Autosar_4_0_1
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-0-2.xsd").unwrap(),
            AutosarVersion::Autosar_4_0_2
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-0-3.xsd").unwrap(),
            AutosarVersion::Autosar_4_0_3
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-1-1.xsd").unwrap(),
            AutosarVersion::Autosar_4_1_1
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-1-2.xsd").unwrap(),
            AutosarVersion::Autosar_4_1_2
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-1-3.xsd").unwrap(),
            AutosarVersion::Autosar_4_1_3
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-2-1.xsd").unwrap(),
            AutosarVersion::Autosar_4_2_1
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-2-2.xsd").unwrap(),
            AutosarVersion::Autosar_4_2_2
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_4-3-0.xsd").unwrap(),
            AutosarVersion::Autosar_4_3_0
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00042.xsd").unwrap(),
            AutosarVersion::Autosar_00042
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00043.xsd").unwrap(),
            AutosarVersion::Autosar_00043
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00044.xsd").unwrap(),
            AutosarVersion::Autosar_00044
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00045.xsd").unwrap(),
            AutosarVersion::Autosar_00045
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00046.xsd").unwrap(),
            AutosarVersion::Autosar_00046
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00047.xsd").unwrap(),
            AutosarVersion::Autosar_00047
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00048.xsd").unwrap(),
            AutosarVersion::Autosar_00048
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00049.xsd").unwrap(),
            AutosarVersion::Autosar_00049
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00050.xsd").unwrap(),
            AutosarVersion::Autosar_00050
        );
        assert_eq!(
            AutosarVersion::from_str("AUTOSAR_00051.xsd").unwrap(),
            AutosarVersion::Autosar_00051
        );

        // do all the version descriptions exist & make sense?
        assert!(AutosarVersion::Autosar_4_0_1.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_0_2.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_0_3.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_1_1.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_1_2.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_1_3.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_2_1.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_2_2.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_4_3_0.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00042.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00043.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00044.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00045.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00046.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00047.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00048.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00049.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00050.describe().starts_with("AUTOSAR"));
        assert!(AutosarVersion::Autosar_00051.describe().starts_with("AUTOSAR"));

        // do all the xsd file names exist?
        assert!(AutosarVersion::Autosar_4_0_1.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_0_2.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_0_3.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_1_1.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_1_2.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_1_3.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_2_1.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_2_2.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_4_3_0.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00042.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00043.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00044.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00045.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00046.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00047.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00048.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00049.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00050.filename().ends_with(".xsd"));
        assert!(AutosarVersion::Autosar_00051.filename().ends_with(".xsd"));

        // to_string() should give the same result as describe()
        assert_eq!(
            AutosarVersion::Autosar_4_0_1.to_string(),
            AutosarVersion::Autosar_4_0_1.describe()
        );

        // clone impl exists
        let cloned = AutosarVersion::Autosar_00050;
        assert_eq!(cloned, AutosarVersion::Autosar_00050);

        // version parse error
        let error = AutosarVersion::from_str("something else");
        assert_eq!(format!("{:#?}", error.unwrap_err()), "ParseAutosarVersionError");

        //Autosar version implements Hash and can be inserted into HashSet / HashMap
        let mut hashset = HashSet::<AutosarVersion>::new();
        hashset.insert(AutosarVersion::Autosar_00050);
    }

    #[test]
    fn attribute_name_basics() {
        // attribute name round trip: enum -> str -> enum
        assert_eq!(
            AttributeName::Uuid,
            AttributeName::from_str(AttributeName::Uuid.to_str()).unwrap()
        );

        // to_string()
        assert_eq!(AttributeName::Uuid.to_string(), "UUID");

        // clone impl exists
        let cloned = AttributeName::Uuid;
        assert_eq!(cloned, AttributeName::Uuid);

        // attribute parse error
        let error = AttributeName::from_str("unknown attribute name");
        assert_eq!(format!("{:#?}", error.unwrap_err()), "ParseAttributeNameError");

        // attribute names implement Hash and can be inserted into HashSet / HashMap
        let mut hashset = HashSet::<AttributeName>::new();
        hashset.insert(AttributeName::Dest);
    }

    #[test]
    fn element_name_basics() {
        // element name round trip: enum -> str -> enum
        assert_eq!(
            ElementName::Autosar,
            ElementName::from_str(ElementName::Autosar.to_str()).unwrap()
        );

        // to_string()
        assert_eq!(ElementName::Autosar.to_string(), "AUTOSAR");

        // clone impl exists
        let cloned = ElementName::Autosar;
        assert_eq!(cloned, ElementName::Autosar);

        // element name parse error
        let error = ElementName::from_str("unknown element name");
        assert_eq!(format!("{:#?}", error.unwrap_err()), "ParseElementNameError");

        // element names implement Hash and can be inserted into HashSet / HashMap
        let mut hashset = HashSet::<ElementName>::new();
        hashset.insert(ElementName::Autosar);
    }

    #[test]
    fn enum_item_basics() {
        // enum item round trip: enum -> str -> enum
        assert_eq!(
            EnumItem::Default,
            EnumItem::from_str(EnumItem::Default.to_str()).unwrap()
        );

        // to_string()
        assert_eq!(EnumItem::Default.to_string(), "DEFAULT");

        // clone impl exists
        let cloned = EnumItem::Abstract;
        assert_eq!(cloned, EnumItem::Abstract);

        // enum item parse error
        let error = EnumItem::from_str("unknown enum item");
        assert_eq!(format!("{:#?}", error.unwrap_err()), "ParseEnumItemError");

        // enum items implement Hash and can be inserted into HashSet / HashMap
        let mut hashset = HashSet::<EnumItem>::new();
        hashset.insert(EnumItem::Abstract);
    }

    #[test]
    fn ordered() {
        let (ar_packages_type, _) = ElementType::ROOT
            .find_sub_element(ElementName::ArPackages, u32::MAX)
            .unwrap();
        let (ar_package_type, _) = ar_packages_type
            .find_sub_element(ElementName::ArPackage, u32::MAX)
            .unwrap();
        let (elements_type, _) = ar_package_type
            .find_sub_element(ElementName::Elements, u32::MAX)
            .unwrap();
        // BSW-MODULE-ENTRY: This class represents a single API entry (C-function prototype) into the BSW module or cluster.
        let (bsw_module_entry, _) = elements_type
            .find_sub_element(ElementName::BswModuleEntry, u32::MAX)
            .unwrap();
        // ARGUMENTS: Arguments belonging of the BswModuleEntry.
        let (arguments, _) = bsw_module_entry
            .find_sub_element(ElementName::Arguments, u32::MAX)
            .unwrap();

        assert!(!bsw_module_entry.is_ordered());
        assert!(arguments.is_ordered());
    }

    #[test]
    fn splittable() {
        let (ar_packages_type, _) = ElementType::ROOT
            .find_sub_element(ElementName::ArPackages, u32::MAX)
            .unwrap();
        let (ar_package_type, _) = ar_packages_type
            .find_sub_element(ElementName::ArPackage, u32::MAX)
            .unwrap();
        let (elements_type, _) = ar_package_type
            .find_sub_element(ElementName::Elements, u32::MAX)
            .unwrap();

        assert!(!ar_package_type.splittable_in(AutosarVersion::Autosar_00051));
        assert_ne!(ar_packages_type.splittable() & AutosarVersion::Autosar_00051 as u32, 0);
        assert!(ar_packages_type.splittable_in(AutosarVersion::Autosar_00051));
        assert_ne!(elements_type.splittable() & AutosarVersion::Autosar_00051 as u32, 0);
    }

    #[test]
    fn reference_dest() {
        let (ar_packages_type, _) = ElementType::ROOT
            .find_sub_element(ElementName::ArPackages, u32::MAX)
            .unwrap();
        let (ar_package_type, _) = ar_packages_type
            .find_sub_element(ElementName::ArPackage, u32::MAX)
            .unwrap();
        let (elements_type, _) = ar_package_type
            .find_sub_element(ElementName::Elements, u32::MAX)
            .unwrap();
        let (can_tp_config_type, _) = elements_type
            .find_sub_element(ElementName::CanTpConfig, u32::MAX)
            .unwrap();
        let (tp_connections_type, _) = can_tp_config_type
            .find_sub_element(ElementName::TpConnections, u32::MAX)
            .unwrap();
        let (can_tp_connection_type, _) = tp_connections_type
            .find_sub_element(ElementName::CanTpConnection, u32::MAX)
            .unwrap();
        let (ident_type, _) = can_tp_connection_type
            .find_sub_element(ElementName::Ident, u32::MAX)
            .unwrap();

        let (diagnostic_connection_type, _) = elements_type
            .find_sub_element(ElementName::DiagnosticConnection, u32::MAX)
            .unwrap();
        let (physical_request_ref_type, _) = diagnostic_connection_type
            .find_sub_element(ElementName::PhysicalRequestRef, u32::MAX)
            .unwrap();

        let ref_value = physical_request_ref_type.reference_dest_value(&ident_type).unwrap();
        assert_eq!(ref_value, EnumItem::TpConnectionIdent);
        let invalid_ref = physical_request_ref_type.reference_dest_value(&tp_connections_type);
        assert!(invalid_ref.is_none());
    }

    #[test]
    fn traits() {
        // this test is basically nonsense - derived traits should all be ok
        // but there is no way to exclude them from coverage
        // ElementMultiplicity: Debug & Clone
        let mult = ElementMultiplicity::Any;
        let m2 = mult.clone(); // must be .clone(), otherwise the copy impl is tested instead
        assert_eq!(format!("{:#?}", mult), format!("{:#?}", m2));

        // ContentMode: Debug, Clone
        let cm = ContentMode::Sequence;
        let cm2 = cm.clone(); // must be .clone(), otherwise the copy impl is tested instead
        assert_eq!(format!("{:#?}", cm), format!("{:#?}", cm2));

        // ElementType: Debug, Clone, Eq & Hash
        let et = ElementType::ROOT;
        let et2 = et.clone(); // must be .clone(), otherwise the copy impl is tested instead
        assert_eq!(format!("{:#?}", et), format!("{:#?}", et2));
        let mut hashset = HashSet::<ElementType>::new();
        hashset.insert(et);
        let inserted = hashset.insert(et2);
        assert!(!inserted);

        // AutosarVersion: Debug, Clone, Hash
        let ver = AutosarVersion::LATEST;
        let ver2 = ver.clone(); // must be .clone(), otherwise the copy impl is tested instead
        assert_eq!(format!("{ver:#?}"), format!("{ver2:#?}"));
        let mut hashset = HashSet::<AutosarVersion>::new();
        hashset.insert(ver);
        let inserted = hashset.insert(ver2);
        assert!(!inserted);

        // ElementName: Debug, Clone, Hash
        let en = ElementName::Autosar;
        let en2 = en.clone(); // must be .clone(), otherwise the copy impl is tested instead
        assert_eq!(format!("{en:#?}"), format!("{en2:#?}"));
        let mut hashset = HashSet::<ElementName>::new();
        hashset.insert(en);
        let inserted = hashset.insert(en2);
        assert!(!inserted);

        // CharacterDataSpec: Debug
        let cdata_spec = CharacterDataSpec::String {
            preserve_whitespace: true,
            max_length: None,
        };
        let txt = format!("{cdata_spec:#?}");
        assert!(txt.starts_with("String"));
        let cdata_spec = CharacterDataSpec::Pattern {
            check_fn: crate::regex::validate_regex_1,
            regex: r"0x[0-9a-z]*",
            max_length: None,
        };
        let txt = format!("{cdata_spec:#?}");
        assert!(txt.starts_with("Pattern"));
        let cdata_spec = CharacterDataSpec::Enum {
            items: &[(EnumItem::Custom, 0x7e000)],
        };
        let txt = format!("{cdata_spec:#?}");
        assert!(txt.starts_with("Enum"));
        let cdata_spec = CharacterDataSpec::Double;
        let txt = format!("{cdata_spec:#?}");
        assert!(txt.starts_with("Double"));
        let cdata_spec = CharacterDataSpec::UnsignedInteger;
        let txt = format!("{cdata_spec:#?}");
        assert!(txt.starts_with("UnsignedInteger"));
    }

    #[test]
    fn test_expand_version_mask() {
        let (ar_packages_type, _) = ElementType::ROOT
            .find_sub_element(ElementName::ArPackages, u32::MAX)
            .unwrap();
        let (ar_package_type, _) = ar_packages_type
            .find_sub_element(ElementName::ArPackage, u32::MAX)
            .unwrap();
        let (elements_type, _) = ar_package_type
            .find_sub_element(ElementName::Elements, u32::MAX)
            .unwrap();
        let (_, element_indices) = elements_type
            .find_sub_element(ElementName::AdaptiveApplicationSwComponentType, u32::MAX)
            .unwrap();
        let version_mask = elements_type.get_sub_element_version_mask(&element_indices).unwrap();

        assert_eq!(
            &[
                AutosarVersion::Autosar_00042,
                AutosarVersion::Autosar_00043,
                AutosarVersion::Autosar_00044,
                AutosarVersion::Autosar_00045,
                AutosarVersion::Autosar_00046,
                AutosarVersion::Autosar_00047,
                AutosarVersion::Autosar_00048,
                AutosarVersion::Autosar_00049,
                AutosarVersion::Autosar_00050,
                AutosarVersion::Autosar_00051,
            ],
            &*expand_version_mask(version_mask)
        );
    }
}