From 3ed09bfc32e84609be5ee7e30ca5e25d6cf4ac0d Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Fri, 15 Sep 2023 11:20:00 -0400 Subject: [PATCH] Prevent conflicting instructions in a setup node --- sgf/src/game.rs | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/sgf/src/game.rs b/sgf/src/game.rs index 68754e0..7ba5aed 100644 --- a/sgf/src/game.rs +++ b/sgf/src/game.rs @@ -1,17 +1,10 @@ use crate::{ - parser::{self, Annotation, Evaluation, UnknownProperty}, + parser::{self, Annotation, Evaluation, SetupInstr, UnknownProperty}, Color, }; use std::{collections::HashSet, time::Duration}; use uuid::Uuid; -/* -#[derive(Clone, Debug, PartialEq)] -pub enum PropertyError { - ConflictingProperty, -} - */ - #[derive(Clone, Debug, PartialEq)] pub enum MoveNodeError { IncompatibleProperty, @@ -222,19 +215,6 @@ impl TryFrom<&parser::Node> for MoveNode { } } -/* -fn parse_position(s: &str) -> Result { - if s.len() == 2 { - Ok(Position { - row: s[0], - column: s[1], - }) - } else { - Err(ConversionError::InvalidPositionSyntax) - } -} -*/ - #[derive(Clone, Debug, PartialEq)] pub struct SetupNode { id: Uuid, @@ -245,6 +225,18 @@ pub struct SetupNode { impl SetupNode { pub fn new(positions: Vec) -> Result { + let mut board = HashSet::new(); + for position in positions.iter() { + let point = match position { + SetupInstr::Piece((_, point)) => point, + SetupInstr::Clear(point) => point, + }; + if board.contains(point) { + return Err(SetupNodeError::ConflictingPosition); + } + board.insert(point); + } + Ok(Self { id: Uuid::new_v4(), positions,