Set up a per-game interpretation layer atop the SGF, and bind all games together in a Game data structure #49

Merged
savanni merged 6 commits from feature/sgf-interpretation-layer into main 2023-07-27 22:20:24 +00:00
2 changed files with 12 additions and 17 deletions
Showing only changes of commit 5a471a3dbf - Show all commits

View File

@ -78,23 +78,16 @@ use typeshare::typeshare;
#[derive(Clone, Debug)]
pub struct Game {
pub file_format: i8,
pub app_name: Option<String>,
pub board_size: Size,
pub info: GameInfo,
pub tree: Tree,
}
impl TryFrom<Tree> for Game {
type Error = Error;
fn try_from(tree: Tree) -> Result<Self, Self::Error> {
let file_format = match tree.sequence[0].find_prop("FF") {
Some(prop) => prop.values[0].parse::<i8>().unwrap(),
None => 4,
};
let app_name = tree.sequence[0]
.find_prop("AP")
.map(|prop| prop.values[0].clone());
let board_size = match tree.sequence[0].find_prop("SZ") {
Some(prop) => Size::try_from(prop.values[0].as_str())?,
None => Size {
@ -103,6 +96,9 @@ impl TryFrom<Tree> for Game {
},
};
let mut info = GameInfo::default();
info.app_name = tree.sequence[0]
.find_prop("AP")
.map(|prop| prop.values[0].clone());
info.black_player = tree.sequence[0]
.find_prop("PB")
.map(|prop| prop.values.join(", "));
@ -165,10 +161,10 @@ impl TryFrom<Tree> for Game {
.map(|prop| prop.values.join(", "));
Ok(Game {
file_format,
app_name,
board_size,
info,
tree,
})
}
}
@ -203,6 +199,7 @@ impl ToString for Rank {
#[derive(Clone, Debug, Default)]
pub struct GameInfo {
pub app_name: Option<String>,
pub annotator: Option<String>,
pub copyright: Option<String>,
pub event: Option<String>,
@ -335,8 +332,7 @@ mod tests {
with_text(EXAMPLE, |trees| {
assert_eq!(trees.len(), 1);
let tree = &trees[0];
assert_eq!(tree.file_format, 4);
assert_eq!(tree.app_name, None);
assert_eq!(tree.info.app_name, None);
assert_eq!(
tree.board_size,
Size {
@ -350,8 +346,7 @@ mod tests {
with_file(std::path::Path::new("test_data/print1.sgf"), |trees| {
assert_eq!(trees.len(), 1);
let tree = &trees[0];
assert_eq!(tree.file_format, 4);
assert_eq!(tree.app_name, None);
assert_eq!(tree.info.app_name, None);
assert_eq!(
tree.board_size,
Size {

View File

@ -52,7 +52,7 @@ impl TryFrom<&str> for Size {
}
}
#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Tree {
pub sequence: Vec<Node>,
pub sub_sequences: Vec<Tree>,
@ -74,7 +74,7 @@ impl ToString for Tree {
}
}
#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Node {
pub properties: Vec<Property>,
}