1use crate::Error;
2
3#[derive(Debug, Default)]
5pub struct Response<'a> {
6 pub payload: &'a [u8],
7 pub trailer: (u8, u8),
8}
9
10impl<'a> Response<'a> {
11 pub fn new() -> Self {
13 Default::default()
14 }
15
16 pub fn is_ok(&self) -> bool {
18 matches!(self.trailer, (0x90, 0x00) | (0x91, 0x00))
19 }
20}
21
22impl<'a> From<&'a [u8]> for Response<'a> {
23 fn from(bytes: &'a [u8]) -> Self {
24 let len = bytes.len();
25 if len < 2 {
26 return Self {
27 payload: bytes,
28 trailer: (0, 0),
29 };
30 }
31
32 let sw2 = bytes[len - 1];
33 let sw1 = bytes[len - 2];
34
35 Self {
36 payload: &bytes[..len - 2],
37 trailer: (sw1, sw2),
38 }
39 }
40}
41
42#[cfg(feature = "std")]
43impl<'a> From<Response<'a>> for Result<&'a [u8], Error<'a>> {
44 fn from(response: Response<'a>) -> Self {
46 let is_ok = response.is_ok();
47
48 match is_ok {
49 true => Ok(response.payload),
50 _ => Err(Error { response }),
51 }
52 }
53}