Skip to main content

doip_definitions/doip_payload/
generic_nack.rs

1use crate::{
2    error::{Error, Result},
3    payload::NackCode,
4};
5
6/// The generic negative acknowledgement of a bad request.
7///
8/// This is found usually when a critical error occurs due to a bad `DoIP` packet
9/// or an entity issue.
10#[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
11#[derive(Copy, Clone, Debug, PartialEq)]
12pub struct GenericNack {
13    /// Available negative acknowledgement codes
14    pub nack_code: NackCode,
15}
16
17impl From<GenericNack> for [u8; 1] {
18    fn from(value: GenericNack) -> Self {
19        let nack_code: u8 = value.nack_code.into();
20        [nack_code]
21    }
22}
23
24impl TryFrom<&[u8]> for GenericNack {
25    type Error = Error;
26
27    fn try_from(value: &[u8]) -> Result<Self> {
28        let nack_code_slice = value.first().ok_or(Error::OutOfBounds {
29            source: "GenericNack",
30            variable: "Nack Code",
31        })?;
32
33        let nack_code = NackCode::try_from(nack_code_slice)?;
34
35        Ok(GenericNack { nack_code })
36    }
37}