1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::{
    codec::{Codec, StdError, WithOffset, WithSize},
    spec::v1_2 as spec,
    wizzilab::v5_3::operand,
};

#[derive(Clone, Debug, PartialEq)]
pub struct IndirectForward {
    // ALP_SPEC Ask for response ?
    pub resp: bool,
    pub interface: operand::IndirectInterface,
}
impl std::fmt::Display for IndirectForward {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "[{}]{}",
            if self.resp { "R" } else { "-" },
            self.interface
        )
    }
}
impl Codec for IndirectForward {
    type Error = StdError;
    fn encoded_size(&self) -> usize {
        1 + self.interface.encoded_size()
    }
    unsafe fn encode_in(&self, out: &mut [u8]) -> usize {
        let overload = match self.interface {
            operand::IndirectInterface::Overloaded(_) => true,
            operand::IndirectInterface::NonOverloaded(_) => false,
        };
        out[0] |= ((overload as u8) << 7) | ((self.resp as u8) << 6);
        1 + spec::action::serialize_all!(&mut out[1..], &self.interface)
    }
    fn decode(out: &[u8]) -> Result<WithSize<Self>, WithOffset<Self::Error>> {
        if out.is_empty() {
            Err(WithOffset::new_head(Self::Error::MissingBytes(1)))
        } else {
            let mut offset = 0;
            let WithSize {
                value: op1,
                size: op1_size,
            } = operand::IndirectInterface::decode(out)?;
            offset += op1_size;
            Ok(WithSize {
                value: Self {
                    resp: out[0] & 0x40 != 0,
                    interface: op1,
                },
                size: offset,
            })
        }
    }
}

impl From<spec::action::IndirectForward> for IndirectForward {
    fn from(action: spec::action::IndirectForward) -> Self {
        Self {
            resp: action.resp,
            interface: action.interface.into(),
        }
    }
}

impl From<IndirectForward> for spec::action::IndirectForward {
    fn from(action: IndirectForward) -> Self {
        Self {
            resp: action.resp,
            interface: action.interface.into(),
        }
    }
}