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

use super::Foundation;
use crate::card::Card;
use crate::piles::PileMut as _;
use crate::{action, undo};

#[derive(Debug)]
pub struct Action;

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

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

#[derive(Debug, Display, Error)]
pub enum Error {
    #[display(fmt = "The foundation is empty")]
    Empty,
}

impl action::Action for Action {
    type State<'s> = ();
    type Value = Value;
    type Error = Error;
}

impl action::Target<Action> for Foundation {
    fn update(&mut self, _: Action, _: ()) -> Result<Value, Error> {
        let card = self.pile.take_at_most_one().ok_or(Error::Empty)?;

        Ok(Value { taken: card })
    }
}

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