Skip to content

Commit

Permalink
yay error handling instead of panics everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
brian committed Oct 12, 2021
1 parent 4af3944 commit 791a231
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 deletions.
29 changes: 20 additions & 9 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use rand::random;

use crate::control::ProgramState;
use crate::movement::Movement;
use crate::movement::CONVERSION_FAILED_MOVEMENT_FROM_U8;

use std::convert::TryFrom;

mod player;
use crate::game::player::{Player, SPAWN_DELAY};
Expand Down Expand Up @@ -539,15 +542,17 @@ impl Game {
// this is flipped because singleplayer and multiplayer will be different someday; TODO
if self.bh.attempt_rotate_board(Movement::RotateCcw) {
self.gravity_direction =
Movement::from(((self.gravity_direction as u8) + 3) % 4);
Movement::try_from(((self.gravity_direction as u8) + 3) % 4)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_U8);
}
}

if player.input.keydown_board_ccw.1 {
// this is flipped because singleplayer and multiplayer will be different someday; TODO
if self.bh.attempt_rotate_board(Movement::RotateCw) {
self.gravity_direction =
Movement::from(((self.gravity_direction as u8) + 1) % 4);
Movement::try_from(((self.gravity_direction as u8) + 1) % 4)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_U8);
}
}
// rotatris specific end
Expand All @@ -559,9 +564,10 @@ impl Game {
player.waiting_to_shift = !self
.bh
.attempt_piece_movement(
Movement::from(
Movement::try_from(
(Movement::Left as u8 + self.gravity_direction as u8) % 4,
),
)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_U8),
player.player_num,
)
.0;
Expand All @@ -572,9 +578,10 @@ impl Game {
player.waiting_to_shift = !self
.bh
.attempt_piece_movement(
Movement::from(
Movement::try_from(
(Movement::Right as u8 + self.gravity_direction as u8) % 4,
),
)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_U8),
player.player_num,
)
.0;
Expand All @@ -593,7 +600,10 @@ impl Game {
if self
.bh
.attempt_piece_movement(
Movement::from((movement as u8 + self.gravity_direction as u8) % 4),
Movement::try_from(
(movement as u8 + self.gravity_direction as u8) % 4,
)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_U8),
player.player_num,
)
.0
Expand Down Expand Up @@ -625,9 +635,10 @@ impl Game {
{
let (moved_flag, caused_full_line_flag): (bool, bool) =
self.bh.attempt_piece_movement(
Movement::from(
Movement::try_from(
(Movement::Down as u8 + self.gravity_direction as u8) % 4,
),
)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_U8),
player.player_num,
);
// if the piece got locked, piece.shape gets set to Shapes::None, so set the spawn piece flag
Expand Down
4 changes: 2 additions & 2 deletions src/game/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ pub enum Gravity {
}

impl From<u8> for Gravity {
fn from(value: u8) -> Gravity {
fn from(value: u8) -> Self {
match value {
0 => Gravity::Down,
1 => Gravity::Left,
2 => Gravity::Up,
3 => Gravity::Right,
_ => panic!("[!] Unknown Gravity value: {}", value),
_ => Gravity::Invalid,
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/menu/inputconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use ggez::Context;
use crate::game::GameMode;
use crate::inputs::{Input, KeyboardControlScheme};
use crate::movement::Movement;
use crate::movement::CONVERSION_FAILED_MOVEMENT_FROM_MENUITEMTRIGGER;

use crate::menu::menuhelpers::GAME_MODE_UNEXPECTEDLY_NONE;
use crate::menu::menuhelpers::{MenuGameOptions, MenuItem, MenuItemTrigger, MenuItemValueType};
use crate::menu::menuhelpers::{DARK_GRAY, HELP_RED, LIGHT_GRAY};
use crate::menu::menuhelpers::{SUB_TEXT_SCALE_DOWN, TEXT_SCALE_DOWN};

use std::convert::TryFrom;

const MAX_NON_START_INPUTS_PER_PLAYER: usize = 8;

static KEY_UNEXPECTEDLY_NONE: &str =
Expand Down Expand Up @@ -353,9 +356,10 @@ impl InputConfigMenu {
let key: KeyCode = self.most_recently_pressed_key.expect(KEY_UNEXPECTEDLY_NONE);
(game_options.arr_controls[self.player_num as usize].0).add_pair(
key,
Movement::from(
Movement::try_from(
self.vec_menu_items_keycode[self.sub_selection_keyboard].trigger,
),
)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_MENUITEMTRIGGER),
);
self.vec_menu_items_keycode[self.sub_selection_keyboard].set_keycode(Some(key));
self.vec_used_keycode.push(key);
Expand Down Expand Up @@ -402,7 +406,8 @@ impl InputConfigMenu {

fn update_all_sub_text_strings(&mut self, game_options: &MenuGameOptions) {
for item in self.vec_menu_items_keycode.iter_mut() {
let desired_movement = Movement::from(item.trigger);
let desired_movement = Movement::try_from(item.trigger)
.expect(CONVERSION_FAILED_MOVEMENT_FROM_MENUITEMTRIGGER);
item.set_keycode(None);
for kmp in (game_options.arr_controls[self.player_num as usize].0)
.vec_keycode_movement_pair
Expand Down
60 changes: 34 additions & 26 deletions src/movement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::menu::menuhelpers::MenuItemTrigger;

use std::convert::TryFrom;

#[repr(u8)]
#[derive(PartialEq, Eq, Copy, Clone)]
pub enum Movement {
Expand All @@ -15,38 +17,44 @@ pub enum Movement {
None,
}

impl From<u8> for Movement {
fn from(value: u8) -> Movement {
pub static CONVERSION_FAILED_MOVEMENT_FROM_U8: &str = "[!] Failed to get Movement value from u8";

impl TryFrom<u8> for Movement {
type Error = &'static str;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Movement::Down,
1 => Movement::Left,
2 => Movement::Up,
3 => Movement::Right,
4 => Movement::RotateCw,
5 => Movement::RotateCcw,
6 => Movement::DoubleRotate,
7 => Movement::BoardCw,
8 => Movement::BoardCcw,
9 => Movement::None,
_ => panic!("[!] Unknown Movement value: {}", value),
0 => Ok(Movement::Down),
1 => Ok(Movement::Left),
2 => Ok(Movement::Up),
3 => Ok(Movement::Right),
4 => Ok(Movement::RotateCw),
5 => Ok(Movement::RotateCcw),
6 => Ok(Movement::DoubleRotate),
7 => Ok(Movement::BoardCw),
8 => Ok(Movement::BoardCcw),
9 => Ok(Movement::None),
_ => Err("Invalid u8 value"),
}
}
}

impl From<MenuItemTrigger> for Movement {
fn from(value: MenuItemTrigger) -> Movement {
pub static CONVERSION_FAILED_MOVEMENT_FROM_MENUITEMTRIGGER: &str =
"[!] Failed to get Movement value from MenuItemTrigger";

impl TryFrom<MenuItemTrigger> for Movement {
type Error = &'static str;

fn try_from(value: MenuItemTrigger) -> Result<Self, Self::Error> {
match value {
MenuItemTrigger::KeyLeft => Movement::Left,
MenuItemTrigger::KeyRight => Movement::Right,
MenuItemTrigger::KeyDown => Movement::Down,
MenuItemTrigger::KeyRotateCw => Movement::RotateCw,
MenuItemTrigger::KeyRotateCcw => Movement::RotateCcw,
MenuItemTrigger::KeyBoardCw => Movement::BoardCw,
MenuItemTrigger::KeyBoardCcw => Movement::BoardCcw,
_ => panic!(
"[!] Unexpected value converting MenuItemTrigger to Movement: {:?}",
value
),
MenuItemTrigger::KeyLeft => Ok(Movement::Left),
MenuItemTrigger::KeyRight => Ok(Movement::Right),
MenuItemTrigger::KeyDown => Ok(Movement::Down),
MenuItemTrigger::KeyRotateCw => Ok(Movement::RotateCw),
MenuItemTrigger::KeyRotateCcw => Ok(Movement::RotateCcw),
MenuItemTrigger::KeyBoardCw => Ok(Movement::BoardCw),
MenuItemTrigger::KeyBoardCcw => Ok(Movement::BoardCcw),
_ => Err("Invalid MenuItemTrigger value"),
}
}
}

0 comments on commit 791a231

Please sign in to comment.