Prevent conflicting instructions in a setup node

This commit is contained in:
Savanni D'Gerinel 2023-09-15 11:20:00 -04:00
parent 7fc1530245
commit 3ed09bfc32
1 changed files with 13 additions and 21 deletions

View File

@ -1,17 +1,10 @@
use crate::{ use crate::{
parser::{self, Annotation, Evaluation, UnknownProperty}, parser::{self, Annotation, Evaluation, SetupInstr, UnknownProperty},
Color, Color,
}; };
use std::{collections::HashSet, time::Duration}; use std::{collections::HashSet, time::Duration};
use uuid::Uuid; use uuid::Uuid;
/*
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyError {
ConflictingProperty,
}
*/
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum MoveNodeError { pub enum MoveNodeError {
IncompatibleProperty, IncompatibleProperty,
@ -222,19 +215,6 @@ impl TryFrom<&parser::Node> for MoveNode {
} }
} }
/*
fn parse_position(s: &str) -> Result<Position, ConversionError> {
if s.len() == 2 {
Ok(Position {
row: s[0],
column: s[1],
})
} else {
Err(ConversionError::InvalidPositionSyntax)
}
}
*/
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct SetupNode { pub struct SetupNode {
id: Uuid, id: Uuid,
@ -245,6 +225,18 @@ pub struct SetupNode {
impl SetupNode { impl SetupNode {
pub fn new(positions: Vec<parser::SetupInstr>) -> Result<Self, SetupNodeError> { pub fn new(positions: Vec<parser::SetupInstr>) -> Result<Self, SetupNodeError> {
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 { Ok(Self {
id: Uuid::new_v4(), id: Uuid::new_v4(),
positions, positions,