Skip to main content

doip_definitions/doip_payload/
diagnostic_nack.rs

1use crate::error::{Error, Result};
2
3/// Available negative acknowledgement codes for `DiagnosticMessageAck`.
4///
5/// Negative acknowledgement codes from the result of a sent `DiagnosticMessage`.
6#[cfg_attr(feature = "python-bindings", pyo3::pyclass(eq, eq_int))]
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub enum DiagnosticNackCode {
9    /// Reserved By ISO-13400 for bytes value `00`
10    ReservedByIso13400_00 = 0x00,
11
12    /// Reserved By ISO-13400 for bytes value `01`
13    ReservedByIso13400_01 = 0x01,
14
15    /// Invalid Source Address
16    InvalidSourceAddress = 0x02,
17
18    /// Unknown Target Address
19    UnknownTargetAddress = 0x03,
20
21    /// Diagnostic Message Too Large
22    DiagnosticMessageTooLarge = 0x04,
23
24    /// Out Of Memory
25    OutOfMemory = 0x05,
26
27    /// Target Unreachable
28    TargetUnreachable = 0x06,
29
30    /// Unknown Network
31    UnknownNetwork = 0x07,
32
33    /// Transport Protocol Error
34    TransportProtocolError = 0x08,
35}
36
37impl From<DiagnosticNackCode> for u8 {
38    fn from(value: DiagnosticNackCode) -> Self {
39        value as u8
40    }
41}
42
43impl TryFrom<&u8> for DiagnosticNackCode {
44    type Error = Error;
45
46    fn try_from(value: &u8) -> Result<Self> {
47        let val = *value;
48
49        match val {
50            v if v == DiagnosticNackCode::ReservedByIso13400_00 as u8 => {
51                Ok(DiagnosticNackCode::ReservedByIso13400_00)
52            }
53            v if v == DiagnosticNackCode::ReservedByIso13400_01 as u8 => {
54                Ok(DiagnosticNackCode::ReservedByIso13400_01)
55            }
56            v if v == DiagnosticNackCode::InvalidSourceAddress as u8 => {
57                Ok(DiagnosticNackCode::InvalidSourceAddress)
58            }
59            v if v == DiagnosticNackCode::UnknownTargetAddress as u8 => {
60                Ok(DiagnosticNackCode::UnknownTargetAddress)
61            }
62            v if v == DiagnosticNackCode::DiagnosticMessageTooLarge as u8 => {
63                Ok(DiagnosticNackCode::DiagnosticMessageTooLarge)
64            }
65            v if v == DiagnosticNackCode::OutOfMemory as u8 => Ok(DiagnosticNackCode::OutOfMemory),
66            v if v == DiagnosticNackCode::TargetUnreachable as u8 => {
67                Ok(DiagnosticNackCode::TargetUnreachable)
68            }
69            v if v == DiagnosticNackCode::UnknownNetwork as u8 => {
70                Ok(DiagnosticNackCode::UnknownNetwork)
71            }
72            v if v == DiagnosticNackCode::TransportProtocolError as u8 => {
73                Ok(DiagnosticNackCode::TransportProtocolError)
74            }
75            v => Err(Error::InvalidDiagnosticNackCode { value: v }),
76        }
77    }
78}