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

use super::{Foundation, FoundationSequence};
use crate::card::{Card, Rank};
use crate::card_sequence::Sequenced as _;
use crate::piles::{Pile as _, PileMut as _};
use crate::{action, undo};

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

#[derive(Debug)]
pub struct Value;

#[derive(Debug, Display, Error)]
pub enum Error {
    #[display(fmt = "The card does not follow the top card of the foundation")]
    DoesNotFollow,

    #[display(fmt = "The foundation is empty but the card is not an ace")]
    RequiresAce,
}

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(card): Action, _: ()) -> Result<Value, Error> {
        if let Some(foundation_top_card) = self.pile.cards().last().copied() {
            // There's at least one foundation card, so ensure the sequence.
            [foundation_top_card, card]
                .is_sequential::<FoundationSequence>()
                .map_err(|_| Error::DoesNotFollow)?;
        } else if card.rank != Rank::Ace {
            // The foundation is empty.
            return Err(Error::RequiresAce);
        }

        self.pile.place_one(card);

        Ok(Value)
    }
}

impl undo::Target<Value> for Foundation {
    fn revert(&mut self, _: Value) {
        // Take the card and let it drop. Whoever gave us the card tracked it in their own Undo.
        self.pile.take_exactly_one();
    }
}