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
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::{Chip, Pin, PinType, State};
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default, Debug)]
pub struct Socket {
    chip: Option<Box<dyn Chip>>,
}

impl Socket {
    pub fn new() -> Socket {
        Socket {
            chip: None
        }
    }

    pub fn with(chip: Box<dyn Chip>) -> Socket {
        Socket {
            chip: Some(chip)
        }
    }

    pub fn plug(&mut self, chip: Box<dyn Chip>) {
        self.chip = Some(chip);
    }

    pub fn has_chip(&self) -> bool {
        self.chip.is_some()
    }

    pub fn get_chip(&mut self) -> &mut Option<Box<dyn Chip>> {
        &mut self.chip
    }

    pub fn get_pin_type(&mut self, pin: u8) -> PinType {
        if self.chip.is_some() {
            self.chip.as_mut().unwrap().get_pin(pin).unwrap().borrow().pin_type.clone()
        } else {
            PinType::Undefined
        }
    }

    pub fn set_pin_type(&mut self, pin: u8, pin_type: &PinType) {
        if self.chip.is_some() {
            self.chip.as_mut().unwrap().get_pin(pin).unwrap().borrow_mut().pin_type = pin_type.clone();
        }
    }
}

impl Chip for Socket {
    fn get_pin_qty(&self) -> u8 {
        if self.chip.is_some() {
            self.chip.as_ref().unwrap().get_pin_qty()
        } else {
            0
        }
    }

    fn get_pin(&mut self, pin: u8) -> Result<Rc<RefCell<Pin>>, &str> {
        if self.chip.is_some() {
            self.chip.as_mut().unwrap().get_pin(pin)
        } else {
            Err("No chip connected")
        }
    }

    fn get_pin_state(&mut self, pin: u8) -> State {
        if self.chip.is_some() {
            self.chip.as_mut().unwrap().get_pin(pin).unwrap().borrow().state.clone()
        } else {
            State::Undefined
        }
    }

    fn set_pin_state(&mut self, pin: u8, state: &State) {
        if self.chip.is_some() {
            self.chip.as_mut().unwrap().get_pin(pin).unwrap().borrow_mut().state = state.clone();
        }
    }

    fn run(&mut self, elapsed_time: std::time::Duration) {
        if self.chip.is_some() {
            self.chip.as_mut().unwrap().run(elapsed_time)
        }
    }
}