From 99bb36dbcdea0bc2a10ee07ddddcd1b679d71978 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 18 Sep 2023 10:57:37 -0400 Subject: [PATCH] Convert the root game node --- sgf/src/game.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++--- sgf/src/parser.rs | 37 +++++++++++++++++++++ 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/sgf/src/game.rs b/sgf/src/game.rs index dd9018a..1891a5f 100644 --- a/sgf/src/game.rs +++ b/sgf/src/game.rs @@ -20,6 +20,7 @@ pub enum SetupNodeError { #[derive(Clone, Debug, PartialEq)] pub enum GameError { InvalidGame, + RequiredPropertiesMissing, } #[derive(Clone, Debug, PartialEq)] @@ -58,7 +59,7 @@ pub struct Game { source: Option, time_limit: Option, overtime: Option, - data_entry: Option, + transcriber: Option, children: Vec, } @@ -91,7 +92,7 @@ impl Game { source: None, time_limit: None, overtime: None, - data_entry: None, + transcriber: None, children: vec![], } @@ -113,7 +114,81 @@ impl TryFrom<&parser::Tree> for Game { type Error = GameError; fn try_from(tree: &parser::Tree) -> Result { - unimplemented!() + let mut ty = None; + let mut size = None; + let mut black_player = Player { + name: None, + rank: None, + team: None, + }; + let mut white_player = Player { + name: None, + rank: None, + team: None, + }; + + for prop in tree.root.properties.iter() { + match prop { + parser::Property::GameType(ty_) => ty = Some(ty_.clone()), + parser::Property::BoardSize(size_) => size = Some(size_.clone()), + parser::Property::BlackPlayer(name) => { + black_player.name = Some(name.clone()); + } + parser::Property::WhitePlayer(name) => { + white_player.name = Some(name.clone()); + } + parser::Property::BlackRank(rank) => { + black_player.rank = Some(rank.clone()); + } + parser::Property::WhiteRank(rank) => { + white_player.rank = Some(rank.clone()); + } + parser::Property::BlackTeam(team) => { + black_player.team = Some(team.clone()); + } + parser::Property::WhiteTeam(team) => { + white_player.team = Some(team.clone()); + } + _ => {} + } + } + + let mut s = match (ty, size) { + (Some(ty), Some(size)) => Ok(Self::new(ty, size, black_player, white_player)), + _ => Err(Self::Error::RequiredPropertiesMissing), + }?; + + for prop in tree.root.properties.iter() { + match prop { + parser::Property::GameType(_) + | parser::Property::BoardSize(_) + | parser::Property::BlackPlayer(_) + | parser::Property::WhitePlayer(_) + | parser::Property::BlackRank(_) + | parser::Property::WhiteRank(_) + | parser::Property::BlackTeam(_) + | parser::Property::WhiteTeam(_) => {} + parser::Property::Application(v) => s.app = Some(v.clone()), + parser::Property::Annotator(v) => s.annotator = Some(v.clone()), + parser::Property::Copyright(v) => s.copyright = Some(v.clone()), + parser::Property::EventDates(v) => s.dates = v.clone(), + parser::Property::EventName(v) => s.event = Some(v.clone()), + parser::Property::GameName(v) => s.game_name = Some(v.clone()), + parser::Property::ExtraGameInformation(v) => s.extra_info = Some(v.clone()), + parser::Property::GameOpening(v) => s.opening_info = Some(v.clone()), + parser::Property::GameLocation(v) => s.location = Some(v.clone()), + parser::Property::Result(v) => s.result = v.clone(), + parser::Property::Round(v) => s.round = Some(v.clone()), + parser::Property::Ruleset(v) => s.rules = Some(v.clone()), + parser::Property::Source(v) => s.source = Some(v.clone()), + parser::Property::TimeLimit(v) => s.time_limit = Some(v.clone()), + parser::Property::Overtime(v) => s.overtime = Some(v.clone()), + // parser::Property::Data(v) => s.transcriber = Some(v.clone()), + _ => {} + } + } + + Ok(s) } } @@ -606,7 +681,7 @@ mod file_test { assert_eq!(game.source, None); assert_eq!(game.time_limit, Some(Duration::from_secs(1800))); assert_eq!(game.overtime, Some("5x30 byo-yomi".to_owned())); - assert_eq!(game.data_entry, None); + assert_eq!(game.transcriber, None); /* Property { diff --git a/sgf/src/parser.rs b/sgf/src/parser.rs index 5d2fd2c..1005517 100644 --- a/sgf/src/parser.rs +++ b/sgf/src/parser.rs @@ -482,6 +482,9 @@ pub enum Property { // BL, WL TimeLeft((Color, Duration)), + // TW, TB + Territory(Color, Vec), + Unknown(UnknownProperty), } @@ -557,6 +560,24 @@ impl ToString for Property { Property::User(value) => format!("US[{}]", value), Property::WhiteRank(value) => format!("WR[{}]", value), Property::WhiteTeam(value) => format!("WT[{}]", value), + Property::Territory(Color::White, positions) => { + format!( + "TW{}", + positions + .iter() + .map(|Position(p)| format!("[{}]", p)) + .collect::() + ) + } + Property::Territory(Color::Black, positions) => { + format!( + "TB{}", + positions + .iter() + .map(|Position(p)| format!("[{}]", p)) + .collect::() + ) + } Property::Unknown(UnknownProperty { ident, value }) => { format!("{}[{}]", ident, value) } @@ -671,6 +692,12 @@ fn parse_property<'a, E: nom::error::ParseError<&'a str>>( "US" => parse_propval(parse_simple_text().map(Property::User))(input)?, "WR" => parse_propval(parse_simple_text().map(Property::WhiteRank))(input)?, "WT" => parse_propval(parse_simple_text().map(Property::WhiteTeam))(input)?, + "TW" => parse_territory() + .map(|p| Property::Territory(Color::White, p)) + .parse(input)?, + "TB" => parse_territory() + .map(|p| Property::Territory(Color::Black, p)) + .parse(input)?, _ => parse_propval(parse_simple_text().map(|value| { Property::Unknown(UnknownProperty { ident: ident.to_owned(), @@ -748,6 +775,16 @@ fn parse_game_result<'a, E: nom::error::ParseError<&'a str>>() -> impl Parser<&' alt((parse_draw_result(), parse_void_result(), parse_win_result())) } +fn parse_territory<'a, E: nom::error::ParseError<&'a str>>( +) -> impl Parser<&'a str, Vec, E> { + many1(|input| { + let (input, _) = tag("[")(input)?; + let (input, position) = parse_simple_text().map(|v| Position(v)).parse(input)?; + let (input, _) = tag("]")(input)?; + Ok((input, position)) + }) +} + fn parse_propval<'a, E: nom::error::ParseError<&'a str>>( mut parser: impl Parser<&'a str, Property, E>, ) -> impl FnMut(&'a str) -> IResult<&'a str, Property, E> {