Skip to content

Commit 802bdb2

Browse files
committed
refactor: simplify examples/modes.rs
1 parent e4e057b commit 802bdb2

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

examples/modes.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,25 @@ fn main() -> std::io::Result<()> {
4747

4848
println!("mode: {mode}\r");
4949

50-
run(move |key| match mode {
51-
"home" => {
52-
let Some(Actions::Home(config)) = modes.get(mode) else {
53-
return false;
54-
};
55-
56-
match config.get(&key) {
57-
Some(action) => match action {
58-
HomeAction::Quit => quit("quit!"),
59-
HomeAction::Edit => {
60-
mode = "edit";
61-
print("enter edit mode!")
62-
}
63-
},
64-
None => print(&format!("{}", key.code)),
65-
}
66-
}
67-
"edit" => {
68-
let Some(Actions::Edit(config)) = modes.get(mode) else {
69-
return false;
70-
};
71-
72-
match config.get(&key) {
73-
Some(action) => match action {
74-
EditAction::Exit => {
75-
mode = "home";
76-
print("exit edit mode!")
77-
}
78-
},
79-
None => print(&format!("{}", key.code)),
80-
}
81-
}
82-
_ => unreachable!(),
50+
run(move |key| match modes.get(mode).unwrap() {
51+
Actions::Home(config) => match config.get(&key) {
52+
Some(action) => match action {
53+
HomeAction::Quit => quit("quit!"),
54+
HomeAction::Edit => {
55+
mode = "edit";
56+
print("enter edit mode!")
57+
}
58+
},
59+
None => print(&format!("{}", key.code)),
60+
},
61+
Actions::Edit(config) => match config.get(&key) {
62+
Some(action) => match action {
63+
EditAction::Exit => {
64+
mode = "home";
65+
print("exit edit mode!")
66+
}
67+
},
68+
None => print(&format!("{}", key.code)),
69+
},
8370
})
8471
}

0 commit comments

Comments
 (0)