autosar_data_specification/
autosarversion.rs

1use num_traits::cast::FromPrimitive;
2
3#[derive(Debug)]
4/// Error type returned when `from_str()` / `parse()` for `AutosarVersion` fails
5pub struct ParseAutosarVersionError;
6
7#[allow(non_camel_case_types)]
8#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash)]
9#[repr(u32)]
10#[non_exhaustive]
11/// Enum of all Autosar versions
12pub enum AutosarVersion {
13    /// AUTOSAR 4.0.1 - xsd file name: `AUTOSAR_4-0-1.xsd`
14    Autosar_4_0_1 = 0x1,
15    /// AUTOSAR 4.0.2 - xsd file name: `AUTOSAR_4-0-2.xsd`
16    Autosar_4_0_2 = 0x2,
17    /// AUTOSAR 4.0.3 - xsd file name: `AUTOSAR_4-0-3.xsd`
18    Autosar_4_0_3 = 0x4,
19    /// AUTOSAR 4.1.1 - xsd file name: `AUTOSAR_4-1-1.xsd`
20    Autosar_4_1_1 = 0x8,
21    /// AUTOSAR 4.1.2 - xsd file name: `AUTOSAR_4-1-2.xsd`
22    Autosar_4_1_2 = 0x10,
23    /// AUTOSAR 4.1.3 - xsd file name: `AUTOSAR_4-1-3.xsd`
24    Autosar_4_1_3 = 0x20,
25    /// AUTOSAR 4.2.1 - xsd file name: `AUTOSAR_4-2-1.xsd`
26    Autosar_4_2_1 = 0x40,
27    /// AUTOSAR 4.2.2 - xsd file name: `AUTOSAR_4-2-2.xsd`
28    Autosar_4_2_2 = 0x80,
29    /// AUTOSAR 4.3.0 - xsd file name: `AUTOSAR_4-3-0.xsd`
30    Autosar_4_3_0 = 0x100,
31    /// AUTOSAR Adaptive 17-03 - xsd file name: `AUTOSAR_00042.xsd`
32    Autosar_00042 = 0x200,
33    /// AUTOSAR Adaptive 17-10 - xsd file name: `AUTOSAR_00043.xsd`
34    Autosar_00043 = 0x400,
35    /// AUTOSAR Classic 4.3.1 - xsd file name: `AUTOSAR_00044.xsd`
36    Autosar_00044 = 0x800,
37    /// AUTOSAR Adaptive 18-03 - xsd file name: `AUTOSAR_00045.xsd`
38    Autosar_00045 = 0x1000,
39    /// AUTOSAR Classic 4.4.0 / Adaptive 18-10 - xsd file name: `AUTOSAR_00046.xsd`
40    Autosar_00046 = 0x2000,
41    /// AUTOSAR Adaptive 19-03 - xsd file name: `AUTOSAR_00047.xsd`
42    Autosar_00047 = 0x4000,
43    /// AUTOSAR 4.5.0 - xsd file name: `AUTOSAR_00048.xsd`
44    Autosar_00048 = 0x8000,
45    /// AUTOSAR R20-11 - xsd file name: `AUTOSAR_00049.xsd`
46    Autosar_00049 = 0x10000,
47    /// AUTOSAR R21-11 - xsd file name: `AUTOSAR_00050.xsd`
48    Autosar_00050 = 0x20000,
49    /// AUTOSAR R22-11 - xsd file name: `AUTOSAR_00051.xsd`
50    Autosar_00051 = 0x40000,
51    /// AUTOSAR R23-11 - xsd file name: `AUTOSAR_00052.xsd`
52    Autosar_00052 = 0x80000,
53    /// AUTOSAR R24-11 - xsd file name: `AUTOSAR_00053.xsd`
54    Autosar_00053 = 0x100000,
55}
56
57impl AutosarVersion {
58    /// get the name of the xsd file matching the Autosar version
59    #[must_use]
60    pub fn filename(&self) -> &'static str {
61        match self {
62            Self::Autosar_4_0_1 => "AUTOSAR_4-0-1.xsd",
63            Self::Autosar_4_0_2 => "AUTOSAR_4-0-2.xsd",
64            Self::Autosar_4_0_3 => "AUTOSAR_4-0-3.xsd",
65            Self::Autosar_4_1_1 => "AUTOSAR_4-1-1.xsd",
66            Self::Autosar_4_1_2 => "AUTOSAR_4-1-2.xsd",
67            Self::Autosar_4_1_3 => "AUTOSAR_4-1-3.xsd",
68            Self::Autosar_4_2_1 => "AUTOSAR_4-2-1.xsd",
69            Self::Autosar_4_2_2 => "AUTOSAR_4-2-2.xsd",
70            Self::Autosar_4_3_0 => "AUTOSAR_4-3-0.xsd",
71            Self::Autosar_00042 => "AUTOSAR_00042.xsd",
72            Self::Autosar_00043 => "AUTOSAR_00043.xsd",
73            Self::Autosar_00044 => "AUTOSAR_00044.xsd",
74            Self::Autosar_00045 => "AUTOSAR_00045.xsd",
75            Self::Autosar_00046 => "AUTOSAR_00046.xsd",
76            Self::Autosar_00047 => "AUTOSAR_00047.xsd",
77            Self::Autosar_00048 => "AUTOSAR_00048.xsd",
78            Self::Autosar_00049 => "AUTOSAR_00049.xsd",
79            Self::Autosar_00050 => "AUTOSAR_00050.xsd",
80            Self::Autosar_00051 => "AUTOSAR_00051.xsd",
81            Self::Autosar_00052 => "AUTOSAR_00052.xsd",
82            Self::Autosar_00053 => "AUTOSAR_00053.xsd",
83        }
84    }
85
86    /// Human readable description of the Autosar version
87    ///
88    /// This is particularly useful for the later versions, where the xsd files are just sequentially numbered.
89    /// For example `Autosar_00050` -> "AUTOSAR R21-11"
90    #[must_use]
91    pub fn describe(&self) -> &'static str {
92        match self {
93            Self::Autosar_4_0_1 => "AUTOSAR 4.0.1",
94            Self::Autosar_4_0_2 => "AUTOSAR 4.0.2",
95            Self::Autosar_4_0_3 => "AUTOSAR 4.0.3",
96            Self::Autosar_4_1_1 => "AUTOSAR 4.1.1",
97            Self::Autosar_4_1_2 => "AUTOSAR 4.1.2",
98            Self::Autosar_4_1_3 => "AUTOSAR 4.1.3",
99            Self::Autosar_4_2_1 => "AUTOSAR 4.2.1",
100            Self::Autosar_4_2_2 => "AUTOSAR 4.2.2",
101            Self::Autosar_4_3_0 => "AUTOSAR 4.3.0",
102            Self::Autosar_00042 => "AUTOSAR Adaptive 17-03",
103            Self::Autosar_00043 => "AUTOSAR Adaptive 17-10",
104            Self::Autosar_00044 => "AUTOSAR Classic 4.3.1",
105            Self::Autosar_00045 => "AUTOSAR Adaptive 18-03",
106            Self::Autosar_00046 => "AUTOSAR Classic 4.4.0 / Adaptive 18-10",
107            Self::Autosar_00047 => "AUTOSAR Adaptive 19-03",
108            Self::Autosar_00048 => "AUTOSAR 4.5.0",
109            Self::Autosar_00049 => "AUTOSAR R20-11",
110            Self::Autosar_00050 => "AUTOSAR R21-11",
111            Self::Autosar_00051 => "AUTOSAR R22-11",
112            Self::Autosar_00052 => "AUTOSAR R23-11",
113            Self::Autosar_00053 => "AUTOSAR R24-11",
114        }
115    }
116
117    /// make an `AutosarVersion` from a u32 value
118    ///
119    /// All `AutosarVersion`s are associated with a power of two u32 value, for example `Autosar_4_3_0` == 0x100
120    /// If the given value is a valid constant of `AutosarVersion`, the enum value will be returnd
121    ///
122    /// This is useful in order to decode version masks
123    #[must_use]
124    pub fn from_val(n: u32) -> Option<Self> {
125        Self::from_u32(n)
126    }
127
128    /// `AutosarVersion::LATEST` is an alias of which ever is the latest version
129    pub const LATEST: AutosarVersion = AutosarVersion::Autosar_00053;
130}
131
132impl core::str::FromStr for AutosarVersion {
133    type Err = ParseAutosarVersionError;
134    fn from_str(input: &str) -> Result<Self, Self::Err> {
135        match input {
136            "AUTOSAR_4-0-1.xsd" => Ok(Self::Autosar_4_0_1),
137            "AUTOSAR_4-0-2.xsd" => Ok(Self::Autosar_4_0_2),
138            "AUTOSAR_4-0-3.xsd" => Ok(Self::Autosar_4_0_3),
139            "AUTOSAR_4-1-1.xsd" => Ok(Self::Autosar_4_1_1),
140            "AUTOSAR_4-1-2.xsd" => Ok(Self::Autosar_4_1_2),
141            "AUTOSAR_4-1-3.xsd" => Ok(Self::Autosar_4_1_3),
142            "AUTOSAR_4-2-1.xsd" => Ok(Self::Autosar_4_2_1),
143            "AUTOSAR_4-2-2.xsd" => Ok(Self::Autosar_4_2_2),
144            "AUTOSAR_4-3-0.xsd" => Ok(Self::Autosar_4_3_0),
145            "AUTOSAR_00042.xsd" => Ok(Self::Autosar_00042),
146            "AUTOSAR_00043.xsd" => Ok(Self::Autosar_00043),
147            "AUTOSAR_00044.xsd" => Ok(Self::Autosar_00044),
148            "AUTOSAR_00045.xsd" => Ok(Self::Autosar_00045),
149            "AUTOSAR_00046.xsd" => Ok(Self::Autosar_00046),
150            "AUTOSAR_00047.xsd" => Ok(Self::Autosar_00047),
151            "AUTOSAR_00048.xsd" => Ok(Self::Autosar_00048),
152            "AUTOSAR_00049.xsd" => Ok(Self::Autosar_00049),
153            "AUTOSAR_00050.xsd" => Ok(Self::Autosar_00050),
154            "AUTOSAR_00051.xsd" => Ok(Self::Autosar_00051),
155            "AUTOSAR_00052.xsd" => Ok(Self::Autosar_00052),
156            "AUTOSAR_00053.xsd" => Ok(Self::Autosar_00053),
157            _ => Err(ParseAutosarVersionError),
158        }
159    }
160}
161
162impl core::fmt::Display for AutosarVersion {
163    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
164        f.write_str(self.describe())
165    }
166}
167
168impl FromPrimitive for AutosarVersion {
169    #[inline]
170    fn from_i64(n: i64) -> Option<Self> {
171        if n < 0 {
172            return None;
173        }
174        Self::from_u64(n as u64)
175    }
176
177    #[inline]
178    fn from_u64(n: u64) -> Option<Self> {
179        match n {
180            0x1 => Some(Self::Autosar_4_0_1),
181            0x2 => Some(Self::Autosar_4_0_2),
182            0x4 => Some(Self::Autosar_4_0_3),
183            0x8 => Some(Self::Autosar_4_1_1),
184            0x10 => Some(Self::Autosar_4_1_2),
185            0x20 => Some(Self::Autosar_4_1_3),
186            0x40 => Some(Self::Autosar_4_2_1),
187            0x80 => Some(Self::Autosar_4_2_2),
188            0x100 => Some(Self::Autosar_4_3_0),
189            0x200 => Some(Self::Autosar_00042),
190            0x400 => Some(Self::Autosar_00043),
191            0x800 => Some(Self::Autosar_00044),
192            0x1000 => Some(Self::Autosar_00045),
193            0x2000 => Some(Self::Autosar_00046),
194            0x4000 => Some(Self::Autosar_00047),
195            0x8000 => Some(Self::Autosar_00048),
196            0x10000 => Some(Self::Autosar_00049),
197            0x20000 => Some(Self::Autosar_00050),
198            0x40000 => Some(Self::Autosar_00051),
199            0x80000 => Some(Self::Autosar_00052),
200            0x100000 => Some(Self::Autosar_00053),
201            _ => None,
202        }
203    }
204}