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
mod board;
pub mod chip;
mod save;
mod socket;
mod trace;
pub use board::Board;
pub use chip::{Chip, Pin, PinType};
use serde::{Deserialize, Serialize};
pub use socket::Socket;
pub use trace::Trace;

/// Current's State
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum State {
    Undefined,
    High,
    Low,
}

impl From<bool> for State {
    fn from(bit: bool) -> Self {
        if bit {
            State::High
        } else {
            State::Low
        }
    }
}
impl State {
    pub fn from_u8(data: u8, position: usize) -> Self {
        let bit = (data >> position) & 1;
        if bit == 1 {
            State::High
        } else {
            State::Low
        }
    }
    pub fn from_u16(data: u16, position: usize) -> Self {
        let bit = (data >> position) & 1;
        if bit == 1 {
            State::High
        } else {
            State::Low
        }
    }
    pub fn from_u32(data: u32, position: usize) -> Self {
        let bit = (data >> position) & 1;
        if bit == 1 {
            State::High
        } else {
            State::Low
        }
    }

    pub fn as_bool(&self) -> bool {
        match self {
            State::High => true,
            _ => false,
        }
    }

    pub fn as_u8(&self) -> u8 {
        match self {
            State::High => 1,
            _ => 0,
        }
    }
}