Make GameResult parsing slightly more flexible

This commit is contained in:
Savanni D'Gerinel 2023-07-21 10:47:30 -04:00
parent 96c6f2dfbf
commit 4b30bf288a
4 changed files with 47 additions and 16 deletions

View File

@ -133,7 +133,7 @@ pub struct GameInfo {
pub event: Option<String>,
// Games can be played across multiple days, even multiple years. The format specifies
// shortcuts.
pub date_time: Vec<chrono::NaiveDate>,
pub date: Vec<chrono::NaiveDate>,
pub location: Option<String>,
// special rules for the round-number and type
pub round: Option<String>,
@ -141,7 +141,7 @@ pub struct GameInfo {
pub source: Option<String>,
pub time_limits: Option<std::time::Duration>,
pub game_keeper: Option<String>,
pub komi: Option<String>,
pub komi: Option<f32>,
pub game_name: Option<String>,
pub game_comments: Option<String>,
@ -177,15 +177,15 @@ impl TryFrom<&str> for GameResult {
Ok(GameResult::Annulled)
} else {
let parts = s.split("+").collect::<Vec<&str>>();
let res = match parts[0] {
"B" => GameResult::Black,
"W" => GameResult::White,
let res = match parts[0].to_ascii_lowercase().as_str() {
"b" => GameResult::Black,
"w" => GameResult::White,
_ => panic!("unknown result format"),
};
match parts[1] {
"R" | "Resign" => Ok(res(Win::Resignation)),
"T" | "Time" => Ok(res(Win::Time)),
"F" | "Forfeit" => Ok(res(Win::Forfeit)),
match parts[1].to_ascii_lowercase().as_str() {
"r" | "resign" => Ok(res(Win::Resignation)),
"t" | "time" => Ok(res(Win::Time)),
"f" | "forfeit" => Ok(res(Win::Forfeit)),
_ => {
let score = parts[1].parse::<f32>().unwrap();
Ok(res(Win::Score(score)))
@ -360,7 +360,7 @@ mod tests {
Some(std::time::Duration::from_secs(28800))
);
assert_eq!(
tree.info.date_time,
tree.info.date,
vec![
chrono::NaiveDate::from_ymd_opt(1996, 10, 18).unwrap(),
chrono::NaiveDate::from_ymd_opt(1996, 10, 19).unwrap(),

17
kifu/core/Cargo.lock generated
View File

@ -60,6 +60,15 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "cool_asserts"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee9f254e53f61e2688d3677fa2cbe4e9b950afd56f48819c98817417cf6b28ec"
dependencies = [
"indent_write",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
@ -152,6 +161,12 @@ dependencies = [
"cxx-build",
]
[[package]]
name = "indent_write"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cfe9645a18782869361d9c8732246be7b410ad4e919d3609ebabdac00ba12c3"
[[package]]
name = "itoa"
version = "1.0.6"
@ -171,6 +186,8 @@ dependencies = [
name = "kifu-core"
version = "0.1.0"
dependencies = [
"chrono",
"cool_asserts",
"go-sgf",
"grid",
"serde",

View File

@ -6,9 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = { version = "0.4" }
go-sgf = { path = "../../go-sgf" }
grid = { version = "0.9" }
serde_json = { version = "1" }
serde = { version = "1", features = [ "derive" ] }
thiserror = { version = "1" }
typeshare = { version = "1" }
[dev-dependencies]
cool_asserts = { version = "2" }

View File

@ -57,6 +57,7 @@ impl Database {
#[cfg(test)]
mod test {
use super::*;
use cool_asserts::assert_matches;
#[test]
fn it_reads_empty_database() {
@ -73,5 +74,14 @@ mod test {
for game in db.all_games() {
assert_eq!(game.game_type, GameType::Go);
}
assert_matches!(db.all_games().find(|g| g.info.black_player == Some("Steve".to_owned())),
Some(game) => {
assert_eq!(game.info.black_player, Some("Steve".to_owned()));
assert_eq!(game.info.white_player, Some("Savanni".to_owned()));
assert_eq!(game.info.date, vec![chrono::NaiveDate::from_ymd_opt(2023, 4, 19).unwrap()]);
assert_eq!(game.info.komi, Some(6.5));
}
);
}
}