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
#[derive(Clone, Copy)]
pub struct Pin(i32);
impl Pin {
pub fn new(value: i32) -> Pin {
if value < 2 || value > 12 {
panic!("Pin initializer received invalid value: {}", value);
}
Pin(value)
}
pub fn value(&self) -> i32 { self.0 }
}
#[derive(Clone, Copy)]
pub enum Mode { Input, Output }
impl Mode {
pub fn value(&self) -> u8 {
match self { Mode::Input => 0, Mode::Output => 1 }
}
}
#[derive(Clone, Copy)]
pub enum State { Low, High }
impl State {
pub fn value(&self) -> i32 {
match self { State::Low => 0, State::High => 1 }
}
}
pub type Level = u8;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_pin() {
let pin_10 = Pin::new(10);
assert_eq!(pin_10.value(), 10);
}
#[test]
#[should_panic]
fn invalid_pin() {
let _pin_1 = Pin::new(1);
}
#[test]
fn mode_value() {
assert_eq!(Mode::Input.value(), 0);
assert_eq!(Mode::Output.value(), 1);
}
#[test]
fn state_value() {
assert_eq!(State::Low.value(), 0);
assert_eq!(State::High.value(), 1);
}
}