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