Skip to content

Commit

Permalink
Fix most clippy lints
Browse files Browse the repository at this point in the history
This was really educational XD
  • Loading branch information
brianch committed Feb 8, 2024
1 parent 8eeb79f commit 65ee0d1
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 170 deletions.
62 changes: 30 additions & 32 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,74 +118,72 @@ pub fn coord_to_san(board: &Board, coords: String, lang: &lang::Language) -> Opt
san_str.push_str(&coords[0..1]);
san_localized.push_str(&coords[0..1]);
} Piece::Bishop => {
san_str.push_str("B");
san_str.push('B');
san_localized.push_str(&lang::tr(lang, "bishop"));
} Piece::Knight => {
san_str.push_str("N");
san_str.push('N');
san_localized.push_str(&lang::tr(lang, "knight"));
} Piece::Rook => {
san_str.push_str("R");
san_str.push('R');
san_localized.push_str(&lang::tr(lang, "rook"));
} Piece::Queen => {
san_str.push_str("Q");
san_str.push('Q');
san_localized.push_str(&lang::tr(lang, "queen"));
} Piece::King => {
san_str.push_str("K");
san_str.push('K');
san_localized.push_str(&lang::tr(lang, "king"));
}
}
// Checking fist the cases of capture
if is_en_passant {
san_localized.push_str(&(String::from("x") + &coords[2..4] + " e.p."));
} else if is_normal_capture {
let simple_capture = san_str.clone() + &"x" + &coords[2..];
let try_move = ChessMove::from_san(&board, &simple_capture);
if let Ok(_) = try_move {
let simple_capture = san_str.clone() + "x" + &coords[2..];
let try_move = ChessMove::from_san(board, &simple_capture);
if try_move.is_ok() {
san_str.push_str(&(String::from("x") + &coords[2..]));
san_localized.push_str(&(String::from("x") + &coords[2..]));
} else {
//the simple notation can only fail because of ambiguity, so we try to specify
//either the file or the rank
let capture_with_file = san_str.clone() + &coords[0..1] + &"x" + &coords[2..];
let try_move_file = ChessMove::from_san(&board, &capture_with_file);
if let Ok(_) = try_move_file {
san_localized.push_str(&(String::from(&coords[0..1]) + &"x" + &coords[2..]));
let capture_with_file = san_str.clone() + &coords[0..1] + "x" + &coords[2..];
let try_move_file = ChessMove::from_san(board, &capture_with_file);
if try_move_file.is_ok() {
san_localized.push_str(&(String::from(&coords[0..1]) + "x" + &coords[2..]));
} else {
san_localized.push_str(&(String::from(&coords[1..2]) + &"x" + &coords[2..]));
san_localized.push_str(&(String::from(&coords[1..2]) + "x" + &coords[2..]));
}
}
// And now the regular moves
} else if piece==Piece::Pawn {
san_localized = String::from(&coords[2..]);
} else {
if piece==Piece::Pawn {
san_localized = String::from(&coords[2..]);
let move_with_regular_notation = san_str.clone() + &coords[2..];
let move_to_try = ChessMove::from_san(board, &move_with_regular_notation);
if move_to_try.is_ok() {
san_str.push_str(&coords[2..]);
san_localized.push_str(&coords[2..]);
} else {
let move_with_regular_notation = san_str.clone() + &coords[2..];
let move_to_try = ChessMove::from_san(&board, &move_with_regular_notation);
if let Ok(_) = move_to_try {
san_str.push_str(&coords[2..]);
san_localized.push_str(&coords[2..]);
//the simple notation can only fail because of ambiguity, so we try to specify
//either the file or the rank
let move_notation_with_file = san_str.clone() + &coords[0..1] + &coords[2..];
let try_move_file = ChessMove::from_san(board, &move_notation_with_file);
if try_move_file.is_ok() {
san_localized.push_str(&(String::from(&coords[0..1]) + &coords[2..]));
} else {
//the simple notation can only fail because of ambiguity, so we try to specify
//either the file or the rank
let move_notation_with_file = san_str.clone() + &coords[0..1] + &coords[2..];
let try_move_file = ChessMove::from_san(&board, &move_notation_with_file);
if let Ok(_) = try_move_file {
san_localized.push_str(&(String::from(&coords[0..1]) + &coords[2..]));
} else {
san_localized.push_str(&(String::from(&coords[1..2]) + &coords[2..]));
}
san_localized.push_str(&(String::from(&coords[1..2]) + &coords[2..]));
}
}
}
let chess_move = ChessMove::from_san(&board, &san_localized);
let chess_move = ChessMove::from_san(board, &san_localized);
// Note: It can indeed return Err for a moment when using the engine (and quickly taking
// back moves), I guess for a sec the engine & board may get desynced, so we can't just unwrap it.
if let Ok(chess_move) = chess_move {
let current_board = board.make_move_new(chess_move);
if current_board.status() == chess::BoardStatus::Checkmate {
san_localized.push_str("#");
san_localized.push('#');
} else if current_board.checkers().popcnt() != 0 {
san_localized.push_str("+");
san_localized.push('+');
}
}
san = Some(san_localized);
Expand Down
8 changes: 3 additions & 5 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn get_favorites(min_rating: i32, max_rating: i32, theme: TacticalThemes, op
} else {
opening.get_field_name()
};
let opening_filter = opening_tags.like(String::from("%") + &opening_tag + "%");
let opening_filter = opening_tags.like(String::from("%") + opening_tag + "%");
let side = match op_side {
None => OpeningSide::Any,
Some(x) => x
Expand Down Expand Up @@ -74,10 +74,8 @@ pub fn is_favorite(id: &str) -> bool {
let results = favs
.filter(puzzle_id.eq(id))
.first::<Puzzle>(&mut conn);
if results.is_ok() {
return true;
}
return false;

results.is_ok()
}

pub fn toggle_favorite(puzzle: Puzzle) {
Expand Down
86 changes: 40 additions & 46 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Engine {
let mut child = cmd.spawn().expect("Error calling engine");

let pos = String::from("position fen ") + &engine.position + &String::from("\n");
let limit = String::from("go ") + &engine.search_up_to + &"\n";
let limit = String::from("go ") + &engine.search_up_to + "\n";
let mut uciok = false;
let mut readyok = false;

Expand All @@ -76,13 +76,11 @@ impl Engine {
let uciok_timeout = timeout(Duration::from_millis(7000),
reader.read_line(&mut buf_str)
).await;
if let Err(_) = uciok_timeout {
if uciok_timeout.is_err() {
break;
} else if buf_str.contains("uciok") {
uciok = true;
break;
} else {
if buf_str.contains("uciok") {
uciok = true;
break;
}
}
}
if uciok {
Expand All @@ -93,13 +91,11 @@ impl Engine {
let readyok_timeout = timeout(Duration::from_millis(7000),
reader.read_line(&mut buf_str)
).await;
if let Err(_) = readyok_timeout {
if readyok_timeout.is_err() {
break;
} else if buf_str.contains("readyok") {
readyok = true;
break;
} else {
if buf_str.contains("readyok") {
readyok = true;
break;
}
}
}
if readyok {
Expand All @@ -118,8 +114,9 @@ impl Engine {
let terminate_timeout = timeout(Duration::from_millis(1000),
child.wait()
).await;
if let Err(_) = terminate_timeout {
eprintln!("Engine didn't quit, killing the process now...");
if let Err(e) = terminate_timeout {
eprintln!("Error: {e}");
eprintln!("Engine didn't quit, killing the process now... ");
let kill_result = timeout(Duration::from_millis(500),
child.kill()
).await;
Expand All @@ -132,27 +129,28 @@ impl Engine {
} EngineState::Thinking(child, search_up_to, receiver) => {
let msg = receiver.try_recv();
if let Ok(msg) = msg {
if &msg == STOP_COMMAND || &msg == EXIT_APP_COMMAND {
if msg == STOP_COMMAND || msg == EXIT_APP_COMMAND {
child.stdin.as_mut().unwrap().write_all(b"stop\n").await.expect("Error communicating with engine");
child.stdin.as_mut().unwrap().write_all(b"quit\n").await.expect("Error communicating with engine");
let terminate_timeout = timeout(Duration::from_millis(1000),
child.wait()
).await;
if let Err(_) = terminate_timeout {
eprintln!("Engine didn't quit, killing the process now...");
if let Err(e) = terminate_timeout {
eprintln!("Error: {e}");
eprintln!("Engine didn't quit, killing the process now... ");
let kill_result = timeout(Duration::from_millis(500),
child.kill()
).await;
if let Err(e) = kill_result {
eprintln!("Error killing the engine process: {e}");
}
}
output.send(Message::EngineStopped(&msg == EXIT_APP_COMMAND)).await.expect("Error on the mpsc channel in the engine subscription");
output.send(Message::EngineStopped(msg == EXIT_APP_COMMAND)).await.expect("Error on the mpsc channel in the engine subscription");
state = EngineState::TurnedOff;
continue;
} else {
let pos = String::from("position fen ") + &msg + &String::from("\n");
let limit = String::from("go ") + &search_up_to + &"\n";
let limit = String::from("go ") + &search_up_to + "\n";
child.stdin.as_mut().unwrap().write_all(b"stop\n").await.expect("Error communicating with engine");
//child.stdin.as_mut().unwrap().write_all(b"setoption name UCI_AnalyseMode value true\n").await.expect("Error communicating with engine");
//child.stdin.as_mut().unwrap().write_all(b"ucinewgame\n").await.expect("Error communicating with engine");
Expand All @@ -170,38 +168,34 @@ impl Engine {
let read_timeout = timeout(Duration::from_millis(50),
reader.read_line(&mut buf_str)
).await;
if let Ok(timeout) = read_timeout {
if let Ok(read_result) = timeout {
if read_result == 0 {
break;
}
let vector: Vec<&str> = buf_str.split_whitespace().collect::<Vec<&str>>();
if let Some(index) = vector.iter().position(|&x| x == "score") {
let eval_num = vector.get(index+2).unwrap().parse::<f32>().ok();
if let Some(e) = eval_num {
if vector.get(index+1).unwrap() == &"mate" {
eval = Some(String::from("Mate in ") + &e.to_string());
} else {
eval = Some(format!("{:.2}",(e / 100.)));
}
if let Ok(Ok(read_result)) = read_timeout {
if read_result == 0 {
break;
}
let vector: Vec<&str> = buf_str.split_whitespace().collect::<Vec<&str>>();
if let Some(index) = vector.iter().position(|&x| x == "score") {
let eval_num = vector.get(index+2).unwrap().parse::<f32>().ok();
if let Some(e) = eval_num {
if vector.get(index+1).unwrap() == &"mate" {
eval = Some(String::from("Mate in ") + &e.to_string());
} else {
eval = Some(format!("{:.2}",(e / 100.)));
}
for i in (index + 3)..vector.len() {
if let Some(token) = vector.get(i) {
if token == &"pv" {
// I thought we could just unwrap, but at least Koivisto sometimes
// returns lines with nothing in the pv
if let Some(best) = vector.get(i+1) {
best_move = Some(best.to_string());
break;
}
}
for i in (index + 3)..vector.len() {
if let Some(token) = vector.get(i) {
if token == &"pv" {
// I thought we could just unwrap, but at least Koivisto sometimes
// returns lines with nothing in the pv
if let Some(best) = vector.get(i+1) {
best_move = Some(best.to_string());
break;
}
}
}
}
buf_str.clear();
} else {
break;
}
buf_str.clear();
} else {
break;
}
Expand Down
Loading

0 comments on commit 65ee0d1

Please sign in to comment.