back-end/front-end: Rust clippy fixes/refactorings

This commit is contained in:
Stephen Seo 2022-04-27 16:51:57 +09:00
parent 694da61bd6
commit 174875b88b
2 changed files with 69 additions and 68 deletions

View file

@ -354,11 +354,11 @@ impl DBHandler {
phrase: Option<String>, phrase: Option<String>,
) -> Result<u32, String> { ) -> Result<u32, String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let row_result: Result<usize, _> = let row_result: Result<usize, _> =
@ -404,11 +404,11 @@ impl DBHandler {
fn pair_up_players(&self, conn: Option<&Connection>) -> Result<(), String> { fn pair_up_players(&self, conn: Option<&Connection>) -> Result<(), String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let mut to_pair: Option<u32> = None; let mut to_pair: Option<u32> = None;
@ -454,11 +454,11 @@ impl DBHandler {
fn create_game(&self, conn: Option<&Connection>, players: &[u32; 2]) -> Result<u32, String> { fn create_game(&self, conn: Option<&Connection>, players: &[u32; 2]) -> Result<u32, String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let mut game_id: u32 = thread_rng().gen(); let mut game_id: u32 = thread_rng().gen();
@ -505,11 +505,11 @@ impl DBHandler {
} }
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let check_player_row = conn.query_row("SELECT games.cyan_player FROM players JOIN games where games.id = players.game_id AND players.id = ?;", [player_id], |row| row.get::<usize, u32>(0)); let check_player_row = conn.query_row("SELECT games.cyan_player FROM players JOIN games where games.id = players.game_id AND players.id = ?;", [player_id], |row| row.get::<usize, u32>(0));
@ -548,11 +548,11 @@ impl DBHandler {
player_id: u32, player_id: u32,
) -> Result<bool, String> { ) -> Result<bool, String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let check_player_row: Result<u32, _> = let check_player_row: Result<u32, _> =
@ -572,11 +572,11 @@ impl DBHandler {
player_id: u32, player_id: u32,
) -> Result<bool, String> { ) -> Result<bool, String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let check_player_game_row: Result<u32, _> = conn.query_row( let check_player_game_row: Result<u32, _> = conn.query_row(
@ -596,11 +596,11 @@ impl DBHandler {
player_id: u32, player_id: u32,
) -> Result<BoardStateType, String> { ) -> Result<BoardStateType, String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
// TODO maybe handle "opponent_disconnected" case // TODO maybe handle "opponent_disconnected" case
@ -671,11 +671,11 @@ impl DBHandler {
fn disconnect_player(&self, conn: Option<&Connection>, player_id: u32) -> Result<(), String> { fn disconnect_player(&self, conn: Option<&Connection>, player_id: u32) -> Result<(), String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let stmt_result = conn.execute("DELETE FROM players WHERE id = ?;", [player_id]); let stmt_result = conn.execute("DELETE FROM players WHERE id = ?;", [player_id]);
@ -688,11 +688,11 @@ impl DBHandler {
fn clear_empty_games(&self, conn: Option<&Connection>) -> Result<(), String> { fn clear_empty_games(&self, conn: Option<&Connection>) -> Result<(), String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
// Only fails if no rows were removed, and that is not an issue // Only fails if no rows were removed, and that is not an issue
@ -712,11 +712,11 @@ impl DBHandler {
pos: usize, pos: usize,
) -> PlaceResultType { ) -> PlaceResultType {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
// check if player exists // check if player exists
@ -960,11 +960,11 @@ impl DBHandler {
} }
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
let is_cyan = status == 0; let is_cyan = status == 0;
@ -1032,11 +1032,11 @@ impl DBHandler {
fn cleanup_stale_games(&self, conn: Option<&Connection>) -> Result<(), String> { fn cleanup_stale_games(&self, conn: Option<&Connection>) -> Result<(), String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
conn.execute( conn.execute(
@ -1050,11 +1050,11 @@ impl DBHandler {
fn cleanup_stale_players(&self, conn: Option<&Connection>) -> Result<(), String> { fn cleanup_stale_players(&self, conn: Option<&Connection>) -> Result<(), String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if let Some(c) = conn {
c
} else {
_conn_result = self.get_conn(DBFirstRun::NotFirstRun); _conn_result = self.get_conn(DBFirstRun::NotFirstRun);
_conn_result.as_ref().unwrap() _conn_result.as_ref().unwrap()
} else {
conn.unwrap()
}; };
conn.execute( conn.execute(

View file

@ -282,17 +282,18 @@ impl Component for Slot {
current_turn, current_turn,
phrase: _, phrase: _,
} => { } => {
if paired && current_side.is_some() { if paired
if current_side.as_ref().unwrap() == &current_turn { && current_side.is_some()
// notify Wrapper with picked slot && current_side.as_ref().unwrap() == &current_turn
if let Some(p) = ctx.link().get_parent() { {
p.clone().downcast::<Wrapper>().send_message( // notify Wrapper with picked slot
WrapperMsg::BackendRequest { if let Some(p) = ctx.link().get_parent() {
place: ctx.props().idx, p.clone()
}, .downcast::<Wrapper>()
); .send_message(WrapperMsg::BackendRequest {
return false; place: ctx.props().idx,
} });
return false;
} }
} }
} }
@ -542,29 +543,29 @@ impl Wrapper {
let board = board_from_string(board_string.clone()); let board = board_from_string(board_string.clone());
for (idx, slot) in board.iter().enumerate() { for (idx, slot) in board.iter().enumerate() {
let was_open = let was_open =
element_has_class(&document, &format!("slot{}", idx), "open").unwrap_or(false); element_has_class(document, &format!("slot{}", idx), "open").unwrap_or(false);
element_remove_class(&document, &format!("slot{}", idx), "open").ok(); element_remove_class(document, &format!("slot{}", idx), "open").ok();
element_remove_class(&document, &format!("slot{}", idx), "placed").ok(); element_remove_class(document, &format!("slot{}", idx), "placed").ok();
element_remove_class(&document, &format!("slot{}", idx), "win").ok(); element_remove_class(document, &format!("slot{}", idx), "win").ok();
element_remove_class(&document, &format!("slot{}", idx), "cyan").ok(); element_remove_class(document, &format!("slot{}", idx), "cyan").ok();
element_remove_class(&document, &format!("slot{}", idx), "magenta").ok(); element_remove_class(document, &format!("slot{}", idx), "magenta").ok();
match slot.get() { match slot.get() {
BoardState::Empty => { BoardState::Empty => {
element_append_class(&document, &format!("slot{}", idx), "open").ok(); element_append_class(document, &format!("slot{}", idx), "open").ok();
} }
BoardState::Cyan => { BoardState::Cyan => {
element_append_class(&document, &format!("slot{}", idx), "cyan").ok(); element_append_class(document, &format!("slot{}", idx), "cyan").ok();
} }
BoardState::CyanWin => { BoardState::CyanWin => {
element_append_class(&document, &format!("slot{}", idx), "cyan").ok(); element_append_class(document, &format!("slot{}", idx), "cyan").ok();
element_append_class(&document, &format!("slot{}", idx), "win").ok(); element_append_class(document, &format!("slot{}", idx), "win").ok();
} }
BoardState::Magenta => { BoardState::Magenta => {
element_append_class(&document, &format!("slot{}", idx), "magenta").ok(); element_append_class(document, &format!("slot{}", idx), "magenta").ok();
} }
BoardState::MagentaWin => { BoardState::MagentaWin => {
element_append_class(&document, &format!("slot{}", idx), "magenta").ok(); element_append_class(document, &format!("slot{}", idx), "magenta").ok();
element_append_class(&document, &format!("slot{}", idx), "win").ok(); element_append_class(document, &format!("slot{}", idx), "win").ok();
} }
} }
let char_at_idx = board_string let char_at_idx = board_string
@ -573,11 +574,11 @@ impl Wrapper {
.expect("idx into board_string should be in range"); .expect("idx into board_string should be in range");
if char_at_idx == 'f' || char_at_idx == 'h' { if char_at_idx == 'f' || char_at_idx == 'h' {
if char_at_idx == 'f' { if char_at_idx == 'f' {
element_append_class(&document, &format!("slot{}", idx), "placed").ok(); element_append_class(document, &format!("slot{}", idx), "placed").ok();
} }
if was_open { if was_open {
append_to_info_text( append_to_info_text(
&document, document,
"info_text0", "info_text0",
&format!("<b class=\"cyan\">CyanPlayer placed at {}</b>", idx), &format!("<b class=\"cyan\">CyanPlayer placed at {}</b>", idx),
INFO_TEXT_MAX_ITEMS, INFO_TEXT_MAX_ITEMS,
@ -586,11 +587,11 @@ impl Wrapper {
} }
} else if char_at_idx == 'g' || char_at_idx == 'i' { } else if char_at_idx == 'g' || char_at_idx == 'i' {
if char_at_idx == 'g' { if char_at_idx == 'g' {
element_append_class(&document, &format!("slot{}", idx), "placed").ok(); element_append_class(document, &format!("slot{}", idx), "placed").ok();
} }
if was_open { if was_open {
append_to_info_text( append_to_info_text(
&document, document,
"info_text0", "info_text0",
&format!("<b class=\"magenta\">MagentaPlayer placed at {}</b>", idx), &format!("<b class=\"magenta\">MagentaPlayer placed at {}</b>", idx),
INFO_TEXT_MAX_ITEMS, INFO_TEXT_MAX_ITEMS,
@ -627,7 +628,7 @@ pub enum WrapperMsg {
} }
impl WrapperMsg { impl WrapperMsg {
fn is_ai_pressed(self) -> bool { fn is_ai_pressed(&self) -> bool {
matches!(self, WrapperMsg::AIPressed(_)) matches!(self, WrapperMsg::AIPressed(_))
} }
} }