checke_rs/turn.rs
1use crate::position::{Move, NotationError};
2
3/// Construct representing many moves at once. Necessary due to checkers allowing multiple
4/// jumps per turn.
5pub struct Turn {
6 moves: Vec<Move>,
7}
8
9impl Turn {
10 /// Creates a [Turn] instance from the given move vector.
11 pub fn new(moves: Vec<Move>) -> Self {
12 Turn { moves }
13 }
14
15 /// Attempts tp create a [Turn] instance using checkers notation.
16 /// ```rust
17 /// use checke_rs::turn::Turn;
18 ///
19 /// let turn = Turn::try_from("9x18,18x25").unwrap();
20 ///
21 /// assert_eq!(turn.moves().len(), 2)
22 /// ```
23 pub fn from_notation(text: &str) -> Result<Self, NotationError> {
24 let parse_result: Result<Vec<Move>, NotationError> = text.split(',')
25 .map(Move::try_from)
26 .collect();
27
28 parse_result.map(|moves| Turn { moves })
29 }
30
31 pub fn moves(&self) -> Vec<Move> {
32 self.moves.to_vec()
33 }
34}
35
36impl TryFrom<&str> for Turn {
37 type Error = NotationError;
38
39 /// Attempts to convert string slice into a [Turn] instance using checkers notation.
40 /// ```rust
41 /// use checke_rs::turn::Turn;
42 ///
43 /// let turn = Turn::try_from("9x18,18x25").unwrap();
44 ///
45 /// assert_eq!(turn.moves().len(), 2)
46 /// ```
47 fn try_from(text: &str) -> Result<Self, Self::Error> {
48 Turn::from_notation(text)
49 }
50}