Skip to content

Commit

Permalink
attempt to improve readability of certain pattern matches in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
brehen committed Oct 4, 2023
1 parent 37e6c3e commit 730c229
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
26 changes: 15 additions & 11 deletions src/game_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ impl GameWindow {
component.del();
return Action::QUIT;
}
x => match component.handle_input(x) {
Err(n) => return n,
_ => {}
},
x => {
if let Err(n) = component.handle_input(x) {
return n;
}
}
}
}
}
Expand All @@ -108,10 +109,11 @@ impl GameWindow {
component.del();
return Action::QUIT;
}
x => match component.handle_input(x) {
Err(n) => return n,
_ => {}
},
x => {
if let Err(n) = component.handle_input(x) {
return n;
}
}
}
}
}
Expand All @@ -134,10 +136,12 @@ impl GameWindow {
ERR => {}
10 | 27 => break,
c => {
if c == 263 && name.len() > 0 {
if c == 263 && !name.is_empty() {
name.truncate(name.len() - 1);
}
else if ( c >= 48 && c <= 57) || ( c >= 65 && c <= 90 ) || ( c >= 97 && c <= 122 ){
} else if (48..=57).contains(&c)
|| (65..=90).contains(&c)
|| (97..=122).contains(&c)
{
let ch = char::from_u32(c as u32).unwrap();
name = format!("{}{}", name, ch);
}
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ fn main() {

let mut window = GameWindow::new();
'new_game: loop {
match window.start_menu(6, 30 ){
Action::QUIT => break 'new_game,
_ => {}
if let Action::QUIT = window.start_menu(6, 30) {
break 'new_game;
}
let name: String = window.get_name(6, 30);
let mut game: Game = Game::new(name, window.window_width, window.window_height);
Expand Down
13 changes: 6 additions & 7 deletions src/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ impl SnakeBodyPart {
}
}

let ch: String;
match self.part {
Part::HEAD => ch = texture.0.clone(),
Part::BODY => ch = texture.1.clone(),
Part::TAIL => ch = texture.2.clone(),
}
fn display(&self, texture: (String, String, String)) {
let ch: String = match self.part {
Part::HEAD => texture.0.clone(),
Part::BODY => texture.1.clone(),
Part::TAIL => texture.2.clone(),
};
mvprintw(self.pos.posy, self.pos.posx, ch.as_str());
}
}


impl Snake {
pub fn new(position: Position, texture: (String, String, String)) -> Self {
let p1 = SnakeBodyPart::new(position, Part::HEAD);
Expand Down Expand Up @@ -75,6 +73,7 @@ impl Snake {
}
self.snake.push_front(p);
}

Ok(())
}

Expand Down
7 changes: 2 additions & 5 deletions src/window_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,8 @@ impl Component {
}
}
KEY_ENTER | 10 => {
match self.option_handler(self.options[self.option_selected].handler){
Err(n) => return Err(n),
_ => {}
};
},
self.option_handler(self.options[self.option_selected].handler)?;
}
_ => {}
}
Ok(())
Expand Down

0 comments on commit 730c229

Please sign in to comment.