1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use derive_more::{Display, Error};
use super::{Tableau, TableauSequence};
use crate::card::Rank;
use crate::card_sequence::{SequenceError, Sequenced as _};
use crate::piles::{Cards, Pile as _, PileMut as _};
use crate::{action, undo};
#[derive(Debug)]
pub struct Action(pub Cards);
#[derive(Debug)]
pub struct Value {
count: usize,
}
#[derive(Debug, Display, Error)]
pub enum Error {
#[display(fmt = "The cards do not follow the current top card")]
DoesNotFollow,
#[display(fmt = "There are no cards to place")]
Empty,
#[display(fmt = "The top card of the pile is face-down")]
FaceDown,
#[display(fmt = "The cards are not in order for the tableau")]
OutOfSequence(SequenceError),
#[display(fmt = "The pile is empty but the bottom card is not a king")]
RequiresKing,
}
impl action::Action for Action {
type State<'s> = ();
type Value = Value;
type Error = Error;
}
impl action::Target<Action> for Tableau {
fn update(&mut self, Action(pile): Action, _: ()) -> Result<Value, Error> {
let bottom_card = *pile.cards().first().ok_or(Error::Empty)?;
pile.is_sequential::<TableauSequence>()
.map_err(Error::OutOfSequence)?;
if let Some(&pile_top_card) = self.pile.right().cards().last() {
[pile_top_card, bottom_card]
.is_sequential::<TableauSequence>()
.map_err(|_| Error::DoesNotFollow)?;
} else if !self.pile.left().is_empty() {
return Err(Error::FaceDown);
} else if bottom_card.rank != Rank::King {
return Err(Error::RequiresKing);
}
let count = pile.len();
self.pile.place(pile);
Ok(Value { count })
}
}
impl undo::Target<Value> for Tableau {
fn revert(&mut self, undo: Value) {
self.pile.right_mut().take_exactly(undo.count);
}
}