autosar_data_abstraction/ecu_configuration/definition/
mod.rs

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
use crate::{abstraction_element, AbstractionElement, ArPackage, AutosarAbstractionError};
use autosar_data::{Element, ElementName, EnumItem};

mod container;
mod parameter;
mod reference;

pub use container::*;
pub use parameter::*;
pub use reference::*;

//#########################################################

/// EcucCommonAttributes provides methods to modify attributes that are shared by all parameters and references
pub trait EcucCommonAttributes: EcucDefinitionElement {
    /// set the multiplicity config classes of the parameter definition.
    /// If an empty list is provided, the multiplicity config classes are removed.
    ///
    /// This setting is required if the containing EcucModuleDef has the category VENDOR_SPECIFIC_MODULE_DEFINITION.
    fn set_multiplicity_config_classes(
        &self,
        config: &[(EcucConfigurationClassEnum, EcucConfigurationVariantEnum)],
    ) -> Result<(), AutosarAbstractionError> {
        set_config_classes(
            self.element(),
            ElementName::MultiplicityConfigClasses,
            ElementName::EcucMultiplicityConfigurationClass,
            config,
        )?;
        Ok(())
    }

    /// get the multiplicity config classes of the parameter definition
    #[must_use]
    fn multiplicity_config_classes(&self) -> Vec<(EcucConfigurationClassEnum, EcucConfigurationVariantEnum)> {
        get_config_classes(self.element(), ElementName::MultiplicityConfigClasses)
    }

    /// set the origin of the parameter definition
    ///
    /// The origin is a string that describes if the parameter was defined in the AUTOSAR standard or by a vendor.
    /// Standardized parameters use the origin "AUTOSAR_ECUC", while vendors are supposed to use string like "VendorXyz_v1.3"
    fn set_origin(&self, origin: &str) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::Origin)?
            .set_character_data(origin)?;
        Ok(())
    }

    /// get the origin of the parameter definition
    ///
    /// The origin is a string that describes if the parameter was defined in the AUTOSAR standard or by a vendor.
    /// Standardized parameters use the origin "AUTOSAR_ECUC", while vendors are supposed to use string like "VendorXyz_v1.3"
    #[must_use]
    fn origin(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::Origin)?
            .character_data()?
            .string_value()
    }

    /// set or remove the postBuildVariantMultiplicity attribute
    ///
    /// If postBuildVariantMultiplicity is true, then the parameter or reference
    /// may have a different number of instances in different post-build variants.
    fn set_post_build_variant_multiplicity(
        &self,
        post_build_variant_multiplicity: Option<bool>,
    ) -> Result<(), AutosarAbstractionError> {
        if let Some(post_build_variant_multiplicity) = post_build_variant_multiplicity {
            self.element()
                .get_or_create_sub_element(ElementName::PostBuildVariantMultiplicity)?
                .set_character_data(post_build_variant_multiplicity)?;
        } else {
            self.element()
                .remove_sub_element_kind(ElementName::PostBuildVariantMultiplicity)?;
        }

        Ok(())
    }

    /// get the postBuildVariantMultiplicity attribute
    ///
    /// If postBuildVariantMultiplicity is true, then the parameter or reference
    /// may have a different number of instances in different post-build variants.
    #[must_use]
    fn post_build_variant_multiplicity(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::PostBuildVariantMultiplicity)?
            .character_data()?
            .parse_bool()
    }

    /// set or remove the postBuildVariantValue attribute
    ///
    /// If postBuildVariantValue is true, then the parameter or reference
    /// may have different values in different post-build variants.
    fn set_post_build_variant_value(
        &self,
        post_build_variant_value: Option<bool>,
    ) -> Result<(), AutosarAbstractionError> {
        if let Some(post_build_variant_value) = post_build_variant_value {
            self.element()
                .get_or_create_sub_element(ElementName::PostBuildVariantValue)?
                .set_character_data(post_build_variant_value)?;
        } else {
            self.element()
                .remove_sub_element_kind(ElementName::PostBuildVariantValue)?;
        }

        Ok(())
    }

    /// get the postBuildVariantValue attribute
    ///
    /// If postBuildVariantValue is true, then the parameter or reference
    /// may have different values in different post-build variants.
    #[must_use]
    fn post_build_variant_value(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::PostBuildVariantValue)?
            .character_data()?
            .parse_bool()
    }

    /// set or remove the requiresIndex attribute
    fn set_requires_index(&self, requires_index: Option<bool>) -> Result<(), AutosarAbstractionError> {
        if let Some(requires_index) = requires_index {
            self.element()
                .get_or_create_sub_element(ElementName::RequiresIndex)?
                .set_character_data(requires_index)?;
        } else {
            self.element().remove_sub_element_kind(ElementName::RequiresIndex)?;
        }

        Ok(())
    }

    /// get the requiresIndex attribute
    #[must_use]
    fn requires_index(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::RequiresIndex)?
            .character_data()?
            .parse_bool()
    }

    /// set the value config classes of the parameter definition.
    ///
    /// If an empty list is provided, the value config classes are removed.
    /// According to the specification setting is required if the containing EcucModuleDef
    /// has the category VENDOR_SPECIFIC_MODULE_DEFINITION, but in practice it is rarely used.
    fn set_value_config_classes(
        &self,
        config: &[(EcucConfigurationClassEnum, EcucConfigurationVariantEnum)],
    ) -> Result<(), AutosarAbstractionError> {
        set_config_classes(
            self.element(),
            ElementName::ValueConfigClasses,
            ElementName::EcucValueConfigurationClass,
            config,
        )?;
        Ok(())
    }

    /// get the value config classes of the parameter definition
    ///
    /// According to the specification setting is required if the containing EcucModuleDef
    /// has the category VENDOR_SPECIFIC_MODULE_DEFINITION, but in practice it is rarely used.
    #[must_use]
    fn value_config_classes(&self) -> Vec<(EcucConfigurationClassEnum, EcucConfigurationVariantEnum)> {
        get_config_classes(self.element(), ElementName::ValueConfigClasses)
    }

    /// set or remove the withAuto attribute
    ///
    /// If withAuto is true, then the parameter or reference is allowed to set its isAutoValue attribute to true.
    fn set_with_auto(&self, with_auto: Option<bool>) -> Result<(), AutosarAbstractionError> {
        if let Some(with_auto) = with_auto {
            self.element()
                .get_or_create_sub_element(ElementName::WithAuto)?
                .set_character_data(with_auto)?;
        } else {
            self.element().remove_sub_element_kind(ElementName::WithAuto)?;
        }

        Ok(())
    }

    /// get the withAuto attribute
    fn with_auto(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::WithAuto)?
            .character_data()?
            .parse_bool()
    }
}

/// EcucDefinitionElement provides methods to modify attributes that are shared by all elements of the ecuc definition
pub trait EcucDefinitionElement: AbstractionElement {
    /// set or remove the lower multiplicity attribute
    fn set_lower_multiplicity(&self, lower_multiplicity: Option<u32>) -> Result<(), AutosarAbstractionError> {
        if let Some(lower_multiplicity) = lower_multiplicity {
            self.element()
                .get_or_create_sub_element(ElementName::LowerMultiplicity)?
                .set_character_data(lower_multiplicity as u64)?;
        } else {
            self.element().remove_sub_element_kind(ElementName::LowerMultiplicity)?;
        }

        Ok(())
    }

    /// get the lower multiplicity attribute
    #[must_use]
    fn lower_multiplicity(&self) -> Option<u32> {
        self.element()
            .get_sub_element(ElementName::LowerMultiplicity)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_integer())
    }

    /// set or remove the upper multiplicity attribute
    fn set_upper_multiplicity(&self, upper_multiplicity: Option<u32>) -> Result<(), AutosarAbstractionError> {
        if let Some(upper_multiplicity) = upper_multiplicity {
            self.element()
                .get_or_create_sub_element(ElementName::UpperMultiplicity)?
                .set_character_data(upper_multiplicity as u64)?;
        } else {
            self.element().remove_sub_element_kind(ElementName::UpperMultiplicity)?;
        }

        Ok(())
    }

    /// get the upper multiplicity attribute
    #[must_use]
    fn upper_multiplicity(&self) -> Option<u32> {
        self.element()
            .get_sub_element(ElementName::UpperMultiplicity)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_integer())
    }

    /// set or remove the upper multiplicity infinite attribute
    ///
    /// if this attribute is set to true, the upper multiplicity is infinite
    /// (i.e. the module definition can be used an arbitrary number of times)
    /// When this attribute is true, the upper multiplicity attribute may not be used.
    fn set_upper_multiplicity_infinite(&self, infinite: Option<bool>) -> Result<(), AutosarAbstractionError> {
        if let Some(infinite) = infinite {
            self.element()
                .get_or_create_sub_element(ElementName::UpperMultiplicityInfinite)?
                .set_character_data(infinite)?;
        } else {
            self.element()
                .remove_sub_element_kind(ElementName::UpperMultiplicityInfinite)?;
        }

        Ok(())
    }

    /// get the upper multiplicity infinite attribute
    #[must_use]
    fn upper_multiplicity_infinite(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::UpperMultiplicityInfinite)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_bool())
    }
}

//#########################################################

/// The `EcucDefinitionCollection` is a container for all module definitions in the ECU configuration
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucDefinitionCollection(Element);
abstraction_element!(EcucDefinitionCollection, EcucDefinitionCollection);

impl EcucDefinitionCollection {
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let ecuc_definition_collection_elem =
            elements.create_named_sub_element(ElementName::EcucDefinitionCollection, name)?;

        Ok(Self(ecuc_definition_collection_elem))
    }

    /// add a reference to a module definition to the collection
    pub fn add_module_def(&self, module_def: &EcucModuleDef) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::ModuleRefs)?
            .create_sub_element(ElementName::ModuleRef)?
            .set_reference_target(module_def.element())?;
        Ok(())
    }

    /// iterate over all module definitions in the collection
    pub fn module_defs(&self) -> impl Iterator<Item = EcucModuleDef> {
        self.element()
            .get_sub_element(ElementName::ModuleRefs)
            .into_iter()
            .flat_map(|module_refs_elem| module_refs_elem.sub_elements())
            .filter_map(|module_ref_elem| module_ref_elem.get_reference_target().ok())
            .filter_map(|module_def_elem| EcucModuleDef::try_from(module_def_elem).ok())
    }
}

//#########################################################

/// The `EcucModuleDef` is a container for the definition of a single base software module
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucModuleDef(Element);
abstraction_element!(EcucModuleDef, EcucModuleDef);
impl EcucDefinitionElement for EcucModuleDef {}

impl EcucModuleDef {
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let ecuc_module_def_elem = elements.create_named_sub_element(ElementName::EcucModuleDef, name)?;

        Ok(Self(ecuc_module_def_elem))
    }

    /// create a new EcucChoiceContainerDef in the module
    pub fn create_choice_container_def(&self, name: &str) -> Result<EcucChoiceContainerDef, AutosarAbstractionError> {
        let containers_elem = self.element().get_or_create_sub_element(ElementName::Containers)?;
        EcucChoiceContainerDef::new(name, &containers_elem)
    }

    /// create a new EcucParamConfContainerDef in the module
    pub fn create_param_conf_container_def(
        &self,
        name: &str,
    ) -> Result<EcucParamConfContainerDef, AutosarAbstractionError> {
        let containers_elem = self.element().get_or_create_sub_element(ElementName::Containers)?;
        EcucParamConfContainerDef::new(name, &containers_elem)
    }

    /// iterate over all containers in the module
    pub fn containers(&self) -> impl Iterator<Item = EcucContainerDef> {
        self.element()
            .get_sub_element(ElementName::Containers)
            .into_iter()
            .flat_map(|containers_elem| containers_elem.sub_elements())
            .filter_map(|container_elem| EcucContainerDef::try_from(container_elem).ok())
    }

    /// set or remove the apiServicePrefix for the module
    ///
    /// for CDD modules the short name of the module is always "CDD", so
    /// this attribute is needed to define the prefix for the API services
    pub fn set_api_service_prefix(&self, prefix: Option<&str>) -> Result<(), AutosarAbstractionError> {
        if let Some(prefix) = prefix {
            self.element()
                .get_or_create_sub_element(ElementName::ApiServicePrefix)?
                .set_character_data(prefix)?;
        } else {
            self.element().remove_sub_element_kind(ElementName::ApiServicePrefix)?;
        }

        Ok(())
    }

    /// get the apiServicePrefix for the module
    ///
    /// for CDD modules the short name of the module is always "CDD", so
    /// this attribute is needed to define the prefix for the API services
    #[must_use]
    pub fn api_service_prefix(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::ApiServicePrefix)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.string_value())
    }

    /// set the supported configuration variants for the module
    pub fn set_supported_config_variants(
        &self,
        variants: &[EcucConfigurationVariantEnum],
    ) -> Result<(), AutosarAbstractionError> {
        // remove the old supported configuration variants list
        let _ = self
            .element()
            .remove_sub_element_kind(ElementName::SupportedConfigVariants);

        // create the new supported configuration variants list
        let supported_config_variants_elem = self
            .element()
            .create_sub_element(ElementName::SupportedConfigVariants)?;
        for variant in variants {
            let variant_elem =
                supported_config_variants_elem.create_sub_element(ElementName::SupportedConfigVariant)?;
            variant_elem.set_character_data::<EnumItem>((*variant).into())?;
        }

        Ok(())
    }

    /// get the supported configuration variants for the module
    #[must_use]
    pub fn supported_config_variants(&self) -> Vec<EcucConfigurationVariantEnum> {
        self.element()
            .get_sub_element(ElementName::SupportedConfigVariants)
            .map(|elem| {
                elem.sub_elements()
                    .filter_map(|variant_elem| {
                        variant_elem
                            .character_data()
                            .and_then(|cdata| cdata.enum_value())
                            .and_then(|enum_item| EcucConfigurationVariantEnum::try_from(enum_item).ok())
                    })
                    .collect()
            })
            .unwrap_or_default()
    }

    /// set or remove the post build variant support attribute
    pub fn set_post_build_variant_support(&self, support: Option<bool>) -> Result<(), AutosarAbstractionError> {
        if let Some(support) = support {
            self.element()
                .get_or_create_sub_element(ElementName::PostBuildVariantSupport)?
                .set_character_data(support)?;
        } else {
            self.element()
                .remove_sub_element_kind(ElementName::PostBuildVariantSupport)?;
        }

        Ok(())
    }

    /// get the post build variant support attribute
    #[must_use]
    pub fn post_build_variant_support(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::PostBuildVariantSupport)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_bool())
    }

    /// set or remove the category of the module definition
    pub fn set_category(&self, category: Option<EcucModuleDefCategory>) -> Result<(), AutosarAbstractionError> {
        if let Some(category) = category {
            self.element()
                .get_or_create_sub_element(ElementName::Category)?
                .set_character_data(category.to_string())?;
        } else {
            self.element().remove_sub_element_kind(ElementName::Category)?;
        }

        Ok(())
    }

    /// get the category of the module definition
    #[must_use]
    pub fn category(&self) -> Option<EcucModuleDefCategory> {
        self.element()
            .get_sub_element(ElementName::Category)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.string_value())
            .and_then(|value| EcucModuleDefCategory::try_from(value.as_str()).ok())
    }

    /// set or remove the reference to a refined standard module
    ///
    /// This reference is only used if the category is `VendorSpecificModuleDefinition`
    pub fn set_refined_module_def(
        &self,
        refined_module_def: Option<&EcucModuleDef>,
    ) -> Result<(), AutosarAbstractionError> {
        if let Some(refined_module_def) = refined_module_def {
            self.element()
                .get_or_create_sub_element(ElementName::RefinedModuleDefRef)?
                .set_reference_target(refined_module_def.element())?;
        } else {
            self.element()
                .remove_sub_element_kind(ElementName::RefinedModuleDefRef)?;
        }

        Ok(())
    }

    /// get the reference to a refined standard module
    ///
    /// This reference is only used if the category is `VendorSpecificModuleDefinition`
    pub fn refined_module_def(&self) -> Option<EcucModuleDef> {
        self.element()
            .get_sub_element(ElementName::RefinedModuleDefRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|target| EcucModuleDef::try_from(target).ok())
    }
}

//#########################################################

/// `EcucConfigurationVariant` provides the different configuration variants that
/// can be used by the module definition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EcucConfigurationVariantEnum {
    /// Preconfigured (i.e. fixed) configuration which cannot be changed.
    PreconfiguredConfiguration,
    /// Recommended configuration
    RecommendedConfiguration,
    /// the BSW Module implementation may use PreCompileTime and LinkTime configuration parameters
    VariantLinkTime,
    /// the BSW Module implementation may use PreCompileTime, LinkTime and PostBuild configuration parameters
    VariantPostBuild,
    /// the BSW Module implementation may use PreCompileTime configuration parameters
    VariantPreCompile,
    /// deprecated in Autosar 4.2.1 - the BSW Module implementation may use PreCompileTime, LinkTime and PostBuild loadable configuration parameters
    VariantPostBuildLoadable,
    /// deprecated in Autosar 4.2.1 - the BSW Module implementation may use PreCompileTime, LinkTime and PostBuild selectable configuration parameters
    VariantPostBuildSelectable,
}

impl From<EcucConfigurationVariantEnum> for EnumItem {
    fn from(value: EcucConfigurationVariantEnum) -> Self {
        match value {
            EcucConfigurationVariantEnum::PreconfiguredConfiguration => Self::PreconfiguredConfiguration,
            EcucConfigurationVariantEnum::RecommendedConfiguration => Self::RecommendedConfiguration,
            EcucConfigurationVariantEnum::VariantLinkTime => Self::VariantLinkTime,
            EcucConfigurationVariantEnum::VariantPostBuild => Self::VariantPostBuild,
            EcucConfigurationVariantEnum::VariantPreCompile => Self::VariantPreCompile,
            EcucConfigurationVariantEnum::VariantPostBuildLoadable => Self::VariantPostBuildLoadable,
            EcucConfigurationVariantEnum::VariantPostBuildSelectable => Self::VariantPostBuildSelectable,
        }
    }
}

impl TryFrom<EnumItem> for EcucConfigurationVariantEnum {
    type Error = AutosarAbstractionError;

    fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
        match value {
            EnumItem::PreconfiguredConfiguration => Ok(Self::PreconfiguredConfiguration),
            EnumItem::RecommendedConfiguration => Ok(Self::RecommendedConfiguration),
            EnumItem::VariantLinkTime => Ok(Self::VariantLinkTime),
            EnumItem::VariantPostBuild => Ok(Self::VariantPostBuild),
            EnumItem::VariantPreCompile => Ok(Self::VariantPreCompile),
            EnumItem::VariantPostBuildLoadable => Ok(Self::VariantPostBuildLoadable),
            EnumItem::VariantPostBuildSelectable => Ok(Self::VariantPostBuildSelectable),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: value.to_string(),
                dest: "EcucConfigurationVariant".to_string(),
            }),
        }
    }
}

//#########################################################

/// `EcucConfigurationClassEnum` provides the different configuration classes for Autosar configuration parameters
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EcucConfigurationClassEnum {
    /// Link Time: parts of configuration are delivered from another object code file
    Link,
    /// PostBuild: a configuration parameter can be changed after compilation
    PostBuild,
    /// PreCompile: a configuration parameter can not be changed after compilation
    PreCompile,
    /// PublishedInformation is used to specify the fact that certain information is fixed even before the pre-compile stage.
    PublishedInformation,
}

impl From<EcucConfigurationClassEnum> for EnumItem {
    fn from(value: EcucConfigurationClassEnum) -> Self {
        match value {
            EcucConfigurationClassEnum::Link => Self::Link,
            EcucConfigurationClassEnum::PostBuild => Self::PostBuild,
            EcucConfigurationClassEnum::PreCompile => Self::PreCompile,
            EcucConfigurationClassEnum::PublishedInformation => Self::PublishedInformation,
        }
    }
}

impl TryFrom<EnumItem> for EcucConfigurationClassEnum {
    type Error = AutosarAbstractionError;

    fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
        match value {
            EnumItem::Link => Ok(Self::Link),
            EnumItem::PostBuild => Ok(Self::PostBuild),
            EnumItem::PreCompile => Ok(Self::PreCompile),
            EnumItem::PublishedInformation => Ok(Self::PublishedInformation),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: value.to_string(),
                dest: "EcucConfigurationClass".to_string(),
            }),
        }
    }
}

//#########################################################

/// The `EcucModuleDefCategory` represents the possible category values for a module definition
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EcucModuleDefCategory {
    /// The module definition is a standardized module (StMD)
    StandardizedModuleDefinition,
    /// The module definition is a vendor specific module (VSMD)
    VendorSpecificModuleDefinition,
}

impl std::fmt::Display for EcucModuleDefCategory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EcucModuleDefCategory::StandardizedModuleDefinition => write!(f, "STANDARDIZED_MODULE_DEFINITION"),
            EcucModuleDefCategory::VendorSpecificModuleDefinition => write!(f, "VENDOR_SPECIFIC_MODULE_DEFINITION"),
        }
    }
}

impl TryFrom<&str> for EcucModuleDefCategory {
    type Error = AutosarAbstractionError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "STANDARDIZED_MODULE_DEFINITION" => Ok(Self::StandardizedModuleDefinition),
            "VENDOR_SPECIFIC_MODULE_DEFINITION" => Ok(Self::VendorSpecificModuleDefinition),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: value.to_string(),
                dest: "EcucModuleDefCategory".to_string(),
            }),
        }
    }
}

//#########################################################

// helper for setting the multiplicity config classes and the value config classes
fn set_config_classes(
    base: &Element,
    element_name_l1: ElementName,
    element_name_l2: ElementName,
    config: &[(EcucConfigurationClassEnum, EcucConfigurationVariantEnum)],
) -> Result<(), AutosarAbstractionError> {
    // remove the existing multiplicity config classes, since we configure
    // the entire list instead of updating the existing one
    let _ = base.remove_sub_element_kind(element_name_l1);

    if !config.is_empty() {
        // create the new multiplicity config classes
        let config_classes = base.create_sub_element(element_name_l1)?;
        for (config_class, variant) in config {
            let ecuc_config_class_elem = config_classes.create_sub_element(element_name_l2)?;
            ecuc_config_class_elem
                .create_sub_element(ElementName::ConfigClass)?
                .set_character_data::<EnumItem>((*config_class).into())?;
            ecuc_config_class_elem
                .create_sub_element(ElementName::ConfigVariant)?
                .set_character_data::<EnumItem>((*variant).into())?;
        }
    }

    Ok(())
}

// helper for getting the multiplicity config classes and the value config classes
fn get_config_classes(
    base: &Element,
    element_name_l1: ElementName,
) -> Vec<(EcucConfigurationClassEnum, EcucConfigurationVariantEnum)> {
    base.get_sub_element(element_name_l1)
        .into_iter()
        .flat_map(|config_classes| config_classes.sub_elements())
        .filter_map(|config_class| {
            let class = config_class
                .get_sub_element(ElementName::ConfigClass)?
                .character_data()?
                .enum_value()?;
            let variant = config_class
                .get_sub_element(ElementName::ConfigVariant)?
                .character_data()?
                .enum_value()?;
            Some((
                EcucConfigurationClassEnum::try_from(class).ok()?,
                EcucConfigurationVariantEnum::try_from(variant).ok()?,
            ))
        })
        .collect()
}

//#########################################################

/// A `EcucDestinationUriDefSet` contains a list of `EcucDestinationUriDef`s
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucDestinationUriDefSet(Element);
abstraction_element!(EcucDestinationUriDefSet, EcucDestinationUriDefSet);

impl EcucDestinationUriDefSet {
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let ecuc_destination_uri_def_set_elem =
            elements.create_named_sub_element(ElementName::EcucDestinationUriDefSet, name)?;

        Ok(Self(ecuc_destination_uri_def_set_elem))
    }

    /// create a new `EcucDestinationUriDef`
    pub fn create_destination_uri_def(
        &self,
        name: &str,
        contract: EcucDestinationUriNestingContract,
    ) -> Result<EcucDestinationUriDef, AutosarAbstractionError> {
        let defs = self
            .element()
            .get_or_create_sub_element(ElementName::DestinationUriDefs)?;
        EcucDestinationUriDef::new(name, &defs, contract)
    }

    /// iterate over all destination uri definitions in the set
    pub fn destination_uri_defs(&self) -> impl Iterator<Item = EcucDestinationUriDef> {
        self.element()
            .get_sub_element(ElementName::DestinationUriDefs)
            .into_iter()
            .flat_map(|defs_elem| defs_elem.sub_elements())
            .filter_map(|def_elem| EcucDestinationUriDef::try_from(def_elem).ok())
    }
}

//#########################################################

/// A `EcucDestinationUriDef` defines a target for an `EcucUriReferenceDef`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcucDestinationUriDef(Element);
abstraction_element!(EcucDestinationUriDef, EcucDestinationUriDef);

impl EcucDestinationUriDef {
    /// create a new `EcucDestinationUriDef`
    pub(crate) fn new(
        name: &str,
        parent: &Element,
        contract: EcucDestinationUriNestingContract,
    ) -> Result<Self, AutosarAbstractionError> {
        let ecuc_destination_uri_def_elem =
            parent.create_named_sub_element(ElementName::EcucDestinationUriDef, name)?;

        let ecuc_destination_uri_def = Self(ecuc_destination_uri_def_elem);
        ecuc_destination_uri_def.set_nesting_contract(contract)?;

        Ok(ecuc_destination_uri_def)
    }

    /// set the nesting contract for the destination uri
    pub fn set_nesting_contract(
        &self,
        contract: EcucDestinationUriNestingContract,
    ) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::DestinationUriPolicy)?
            .get_or_create_sub_element(ElementName::DestinationUriNestingContract)?
            .set_character_data::<EnumItem>(contract.into())?;
        Ok(())
    }

    /// get the nesting contract for the destination uri
    #[must_use]
    pub fn nesting_contract(&self) -> Option<EcucDestinationUriNestingContract> {
        self.element()
            .get_sub_element(ElementName::DestinationUriPolicy)?
            .get_sub_element(ElementName::DestinationUriNestingContract)?
            .character_data()?
            .enum_value()
            .and_then(|enum_item| EcucDestinationUriNestingContract::try_from(enum_item).ok())
    }

    /// create an `EcucParamConfContainerDef` in the destination uri policy
    pub fn create_param_conf_container_def(
        &self,
        name: &str,
    ) -> Result<EcucParamConfContainerDef, AutosarAbstractionError> {
        let containers = self
            .element()
            .get_or_create_sub_element(ElementName::DestinationUriPolicy)?
            .get_or_create_sub_element(ElementName::Containers)?;
        EcucParamConfContainerDef::new(name, &containers)
    }

    /// create an `EcucChoiceContainerDef` in the destination uri policy
    pub fn create_choice_container_def(&self, name: &str) -> Result<EcucChoiceContainerDef, AutosarAbstractionError> {
        let containers = self
            .element()
            .get_or_create_sub_element(ElementName::DestinationUriPolicy)?
            .get_or_create_sub_element(ElementName::Containers)?;
        EcucChoiceContainerDef::new(name, &containers)
    }

    /// iterate over all containers in the destination uri policy
    pub fn containers(&self) -> impl Iterator<Item = EcucContainerDef> {
        self.element()
            .get_sub_element(ElementName::DestinationUriPolicy)
            .and_then(|dup_elem| dup_elem.get_sub_element(ElementName::Containers))
            .into_iter()
            .flat_map(|policy_elem| policy_elem.sub_elements())
            .filter_map(|container_elem| EcucContainerDef::try_from(container_elem).ok())
    }

    // theoretically, the destination uri def could also contain parameters or references
    // it looks like nobody uses these, so we don't implement them here
}

//#########################################################

/// `EcucDestinationUriNestingContract` provides the different nesting contracts for destination URIs
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EcucDestinationUriNestingContract {
    /// EcucDestinationUriPolicy describes elements (subContainers, Parameters, References) that are directly owned by the target container.
    LeafOfTargetContainer,
    /// EcucDestinationUriPolicy describes the target container of EcucUriReferenceDef.
    TargetContainer,
    /// EcucDestinationUriPolicy describes elements (subContainers, Parameters, References) that are owned by the target container or its subContainers.
    VertexOfTargetContainer,
}

impl From<EcucDestinationUriNestingContract> for EnumItem {
    fn from(value: EcucDestinationUriNestingContract) -> Self {
        match value {
            EcucDestinationUriNestingContract::LeafOfTargetContainer => Self::LeafOfTargetContainer,
            EcucDestinationUriNestingContract::TargetContainer => Self::TargetContainer,
            EcucDestinationUriNestingContract::VertexOfTargetContainer => Self::VertexOfTargetContainer,
        }
    }
}

impl TryFrom<EnumItem> for EcucDestinationUriNestingContract {
    type Error = AutosarAbstractionError;

    fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
        match value {
            EnumItem::LeafOfTargetContainer => Ok(Self::LeafOfTargetContainer),
            EnumItem::TargetContainer => Ok(Self::TargetContainer),
            EnumItem::VertexOfTargetContainer => Ok(Self::VertexOfTargetContainer),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: value.to_string(),
                dest: "EcucDestinationUriNestingContract".to_string(),
            }),
        }
    }
}

//#########################################################

#[cfg(test)]
mod test {
    use super::*;
    use autosar_data::{AutosarModel, AutosarVersion};

    #[test]
    fn ecuc_module_def() {
        let model = AutosarModel::new();
        let _file = model.create_file("file.arxml", AutosarVersion::LATEST).unwrap();
        let package = ArPackage::get_or_create(&model, "/package").unwrap();

        let ecuc_definition_collection = package.create_ecuc_definition_collection("collection").unwrap();
        assert_eq!(ecuc_definition_collection.module_defs().count(), 0);

        let ecuc_module_def = package.create_ecuc_module_def("module_def").unwrap();
        ecuc_definition_collection.add_module_def(&ecuc_module_def).unwrap();
        assert_eq!(ecuc_definition_collection.module_defs().count(), 1);

        assert_eq!(ecuc_module_def.api_service_prefix(), None);
        assert_eq!(
            ecuc_module_def.supported_config_variants(),
            Vec::<EcucConfigurationVariantEnum>::new()
        );
        assert_eq!(ecuc_module_def.post_build_variant_support(), None);
        assert_eq!(ecuc_module_def.category(), None);
        assert_eq!(ecuc_module_def.refined_module_def(), None);
        assert_eq!(ecuc_module_def.lower_multiplicity(), None);
        assert_eq!(ecuc_module_def.upper_multiplicity(), None);
        assert_eq!(ecuc_module_def.upper_multiplicity_infinite(), None);

        let base_ecuc_module_def = package.create_ecuc_module_def("base_module_def").unwrap();
        ecuc_module_def
            .set_refined_module_def(Some(&base_ecuc_module_def))
            .unwrap();
        assert_eq!(ecuc_module_def.refined_module_def(), Some(base_ecuc_module_def.clone()));
        ecuc_module_def.set_api_service_prefix(Some("prefix")).unwrap();
        assert_eq!(ecuc_module_def.api_service_prefix(), Some("prefix".to_string()));
        ecuc_module_def
            .set_supported_config_variants(&[
                EcucConfigurationVariantEnum::PreconfiguredConfiguration,
                EcucConfigurationVariantEnum::VariantLinkTime,
            ])
            .unwrap();
        assert_eq!(
            ecuc_module_def.supported_config_variants(),
            vec![
                EcucConfigurationVariantEnum::PreconfiguredConfiguration,
                EcucConfigurationVariantEnum::VariantLinkTime
            ]
        );
        ecuc_module_def.set_post_build_variant_support(Some(true)).unwrap();
        assert_eq!(ecuc_module_def.post_build_variant_support(), Some(true));
        ecuc_module_def
            .set_category(Some(EcucModuleDefCategory::VendorSpecificModuleDefinition))
            .unwrap();
        assert_eq!(
            ecuc_module_def.category(),
            Some(EcucModuleDefCategory::VendorSpecificModuleDefinition)
        );
        ecuc_module_def.set_lower_multiplicity(Some(1)).unwrap();
        assert_eq!(ecuc_module_def.lower_multiplicity(), Some(1));
        ecuc_module_def.set_upper_multiplicity(Some(2)).unwrap();
        assert_eq!(ecuc_module_def.upper_multiplicity(), Some(2));
        ecuc_module_def.set_upper_multiplicity_infinite(Some(true)).unwrap();
        assert_eq!(ecuc_module_def.upper_multiplicity_infinite(), Some(true));
    }

    #[test]
    fn ecuc_configuration_variant_enum_conversion() {
        let variants = [
            EcucConfigurationVariantEnum::PreconfiguredConfiguration,
            EcucConfigurationVariantEnum::RecommendedConfiguration,
            EcucConfigurationVariantEnum::VariantLinkTime,
            EcucConfigurationVariantEnum::VariantPostBuild,
            EcucConfigurationVariantEnum::VariantPreCompile,
            EcucConfigurationVariantEnum::VariantPostBuildLoadable,
            EcucConfigurationVariantEnum::VariantPostBuildSelectable,
        ];

        for variant in &variants {
            let enum_item: EnumItem = (*variant).into();
            let converted_variant = EcucConfigurationVariantEnum::try_from(enum_item).unwrap();
            assert_eq!(*variant, converted_variant);
        }
    }

    #[test]
    fn destionation_uri_defs() {
        let model = AutosarModel::new();
        let _file = model.create_file("file.arxml", AutosarVersion::LATEST).unwrap();
        let package = ArPackage::get_or_create(&model, "/package").unwrap();

        let ecuc_destination_uri_def_set = package
            .create_ecuc_destination_uri_def_set("destination_uri_def_set")
            .unwrap();
        assert_eq!(ecuc_destination_uri_def_set.destination_uri_defs().count(), 0);

        let ecuc_destination_uri_def = ecuc_destination_uri_def_set
            .create_destination_uri_def(
                "destination_uri_def",
                EcucDestinationUriNestingContract::LeafOfTargetContainer,
            )
            .unwrap();
        assert_eq!(ecuc_destination_uri_def_set.destination_uri_defs().count(), 1);

        assert_eq!(
            ecuc_destination_uri_def.nesting_contract(),
            Some(EcucDestinationUriNestingContract::LeafOfTargetContainer)
        );
        assert_eq!(ecuc_destination_uri_def.containers().count(), 0);

        let _param_conf_container_def = ecuc_destination_uri_def
            .create_param_conf_container_def("param_conf_container")
            .unwrap();
        assert_eq!(ecuc_destination_uri_def.containers().count(), 1);

        let _choice_container_def = ecuc_destination_uri_def
            .create_choice_container_def("choice_container")
            .unwrap();
        assert_eq!(ecuc_destination_uri_def.containers().count(), 2);
    }
}