-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
47 lines (43 loc) · 1.08 KB
/
main.rs
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
use std::io::{self, BufRead, Write};
#[derive(Debug)]
enum State {
Locked,
UnLocked,
}
#[derive(Debug)]
enum Event {
Push,
Coin,
}
fn next_state(state: State, event: Event) -> State {
match state {
State::Locked => match event {
Event::Push => State::Locked,
Event::Coin => State::UnLocked,
},
State::UnLocked => match event {
Event::Push => State::Locked,
Event::Coin => State::UnLocked,
},
}
}
fn main() {
//default state
let mut state = State::Locked;
let stdin = io::stdin();
println!("State: {:?}", state);
print!(">");
for line in stdin.lock().lines() {
match line.unwrap().as_str() {
"coin" => state = next_state(state, Event::Coin),
"push" => state = next_state(state, Event::Push),
"q" => return,
unknown => {
eprintln!("Error:Unknown Event {}", unknown);
}
}
println!("State: {:?}", state);
print!(">");
io::stdout().flush().unwrap();
}
}