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
use derive_more::{Display, Error};

use super::Tableau;
use crate::piles::{Cards, PileMut as _};
use crate::{action, undo};

#[derive(Debug)]
pub struct Action(pub usize);

#[derive(Debug)]
pub struct Value {
    taken: Cards,
}

impl Value {
    pub fn taken(&self) -> &Cards {
        &self.taken
    }
}

#[derive(Debug, Display, Error)]
pub enum Error {
    #[display(fmt = "There are not enough face-up cards to take")]
    InsufficientCards,
}

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(count): Action, _: ()) -> Result<Value, Error> {
        let cards = self
            .pile
            .right_mut()
            .take_or_none(count)
            .ok_or(Error::InsufficientCards)?;

        Ok(Value { taken: cards })
    }
}

impl undo::Target<Value> for Tableau {
    fn revert(&mut self, value: Value) {
        self.pile.place(value.taken);
    }
}