Skip to main content

doip_definitions/doip_payload/
power_information_response.rs

1use crate::{
2    error::{Error, Result},
3    payload::PowerMode,
4};
5
6/// Expected reponse from `PowerInformationRequest`.
7///
8/// Containing details of the target of the `PowerInformationRequest`, the
9/// `EntityStatusResponse` provides the program with details pertaining to the
10/// active power mode status of the entity.
11#[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
12#[derive(Copy, Clone, Debug, PartialEq)]
13pub struct PowerInformationResponse {
14    /// Possible power modes available
15    pub power_mode: PowerMode,
16}
17
18impl From<PowerInformationResponse> for [u8; 1] {
19    fn from(value: PowerInformationResponse) -> Self {
20        let power_mode: u8 = value.power_mode.into();
21        [power_mode]
22    }
23}
24
25impl TryFrom<&[u8]> for PowerInformationResponse {
26    type Error = Error;
27
28    fn try_from(value: &[u8]) -> Result<Self> {
29        let power_mode_slice = value.first().ok_or(Error::OutOfBounds {
30            source: "PowerInformationResponse",
31            variable: "Power Mode",
32        })?;
33
34        let power_mode = PowerMode::try_from(power_mode_slice)?;
35
36        Ok(PowerInformationResponse { power_mode })
37    }
38}