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
use crate::codec::{Codec, StdError, WithOffset, WithSize};

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Logic {
    Or = 0,
    Xor = 1,
    Nor = 2,
    Nand = 3,
}
impl Logic {
    fn from(n: u8) -> Self {
        match n {
            0 => Logic::Or,
            1 => Logic::Xor,
            2 => Logic::Nor,
            3 => Logic::Nand,
            x => panic!("Impossible logic op {}", x),
        }
    }
}
impl std::fmt::Display for Logic {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Logic::Or => write!(f, "[OR]"),
            Logic::Xor => write!(f, "[XOR]"),
            Logic::Nor => write!(f, "[NOR]"),
            Logic::Nand => write!(f, "[NAND]"),
        }
    }
}
impl Codec for Logic {
    type Error = StdError;
    fn encoded_size(&self) -> usize {
        1
    }
    unsafe fn encode_in(&self, out: &mut [u8]) -> usize {
        out[0] = crate::spec::v1_2::action::OpCode::Logic as u8 + ((*self as u8) << 6);
        1
    }
    fn decode(out: &[u8]) -> Result<WithSize<Self>, WithOffset<Self::Error>> {
        if out.is_empty() {
            return Err(WithOffset::new_head(Self::Error::MissingBytes(1)));
        }
        Ok(WithSize {
            value: Self::from(out[0] >> 6),
            size: 1,
        })
    }
}