Skip to main content

doip_definitions/doip_header/
version.rs

1use crate::{
2    definitions::{
3        DEFAULT_VALUE, ISO13400_2010, ISO13400_2012, ISO13400_2019, ISO13400_2019_AMD1,
4        RESERVED_VER,
5    },
6    error::Error,
7};
8
9/// Avaiable version of the `DoIP` protocol as per ISO-13400.
10///
11/// Maps to `u8` values for avaiable `DoIP` protocols which are supported by this
12/// crates and ISO-13400.
13#[cfg_attr(feature = "python-bindings", pyo3::pyclass(eq, eq_int))]
14#[derive(Debug, Clone, Copy, PartialEq)]
15#[repr(u8)]
16pub enum ProtocolVersion {
17    /// Reserved Version
18    ReservedVer = RESERVED_VER,
19
20    /// `DoIP` Payload Version: ISO-13400 2010 Version
21    Iso13400_2010 = ISO13400_2010,
22
23    /// `DoIP` Payload Version: ISO-13400 2012 Version
24    Iso13400_2012 = ISO13400_2012,
25
26    /// `DoIP` Payload Version: ISO-13400 2019 Version
27    Iso13400_2019 = ISO13400_2019,
28
29    /// `DoIP` Payload Version: ISO-13400 `2019_AMD1` Version
30    Iso13400_2019Amd1 = ISO13400_2019_AMD1,
31
32    /// `DoIP` Payload Version: Default Version
33    DefaultValue = DEFAULT_VALUE,
34}
35
36impl TryFrom<&u8> for ProtocolVersion {
37    type Error = Error;
38
39    fn try_from(value: &u8) -> Result<Self, Self::Error> {
40        let val = *value;
41
42        match val {
43            v if v == ProtocolVersion::ReservedVer as u8 => Ok(ProtocolVersion::ReservedVer),
44            v if v == ProtocolVersion::Iso13400_2010 as u8 => Ok(ProtocolVersion::Iso13400_2010),
45            v if v == ProtocolVersion::Iso13400_2012 as u8 => Ok(ProtocolVersion::Iso13400_2012),
46            v if v == ProtocolVersion::Iso13400_2019 as u8 => Ok(ProtocolVersion::Iso13400_2019),
47            v if v == ProtocolVersion::Iso13400_2019Amd1 as u8 => {
48                Ok(ProtocolVersion::Iso13400_2019Amd1)
49            }
50            v if v == ProtocolVersion::DefaultValue as u8 => Ok(ProtocolVersion::DefaultValue),
51            v => Err(Error::InvalidProtocolVersion { value: v }),
52        }
53    }
54}
55
56impl From<ProtocolVersion> for u8 {
57    fn from(value: ProtocolVersion) -> Self {
58        value as u8
59    }
60}