iso14229_1/common/
transfer_data.rs

1//! Commons of Service 36
2
3
4use crate::{Configuration, Error, Placeholder, RequestData, ResponseData, utils};
5
6#[derive(Debug, Clone)]
7pub struct TransferData {
8    pub sequence: u8,
9    pub data: Vec<u8>,
10}
11
12impl<'a> TryFrom<&'a [u8]> for TransferData {
13    type Error = Error;
14    #[inline]
15    fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
16        utils::data_length_check(data.len(), 1, false)?;
17
18        let mut offset = 0;
19        let sequence = data[offset];
20        offset += 1;
21
22        Ok(Self { sequence, data: data[offset..].to_vec() })
23    }
24}
25
26impl Into<Vec<u8>> for TransferData {
27    fn into(mut self) -> Vec<u8> {
28        let mut result = vec![self.sequence];
29        result.append(&mut self.data);
30        result
31    }
32}
33
34impl RequestData for TransferData {
35    type SubFunc = Placeholder;
36    #[inline]
37    fn try_parse(data: &[u8], _: Option<Self::SubFunc>, _: &Configuration) -> Result<Self, Error> {
38        Self::try_from(data)
39    }
40    #[inline]
41    fn to_vec(self, _: &Configuration) -> Vec<u8> {
42        self.into()
43    }
44}
45
46impl ResponseData for TransferData {
47    type SubFunc = Placeholder;
48    #[inline]
49    fn try_parse(data: &[u8], _: Option<Self::SubFunc>, _: &Configuration) -> Result<Self, Error> {
50        Self::try_from(data)
51    }
52    #[inline]
53    fn to_vec(self, _: &Configuration) -> Vec<u8> {
54        self.into()
55    }
56}