dash7_alp/spec/v1_2/action/
indirect_forward.rs1use crate::{
2 codec::{Codec, StdError, WithOffset, WithSize},
3 spec::v1_2::operand,
4};
5
6#[derive(Clone, Debug, PartialEq)]
7pub struct IndirectForward {
8 pub resp: bool,
10 pub interface: operand::IndirectInterface,
11}
12impl std::fmt::Display for IndirectForward {
13 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
14 write!(
15 f,
16 "[{}]{}",
17 if self.resp { "R" } else { "-" },
18 self.interface
19 )
20 }
21}
22impl Codec for IndirectForward {
23 type Error = StdError;
24 fn encoded_size(&self) -> usize {
25 1 + self.interface.encoded_size()
26 }
27 unsafe fn encode_in(&self, out: &mut [u8]) -> usize {
28 let overload = match self.interface {
29 operand::IndirectInterface::Overloaded(_) => true,
30 operand::IndirectInterface::NonOverloaded(_) => false,
31 };
32 out[0] |= ((overload as u8) << 7) | ((self.resp as u8) << 6);
33 1 + super::serialize_all!(&mut out[1..], &self.interface)
34 }
35 fn decode(out: &[u8]) -> Result<WithSize<Self>, WithOffset<Self::Error>> {
36 if out.is_empty() {
37 Err(WithOffset::new_head(Self::Error::MissingBytes(1)))
38 } else {
39 let mut offset = 0;
40 let WithSize {
41 value: op1,
42 size: op1_size,
43 } = operand::IndirectInterface::decode(out)?;
44 offset += op1_size;
45 Ok(WithSize {
46 value: Self {
47 resp: out[0] & 0x40 != 0,
48 interface: op1,
49 },
50 size: offset,
51 })
52 }
53 }
54}