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
use enum_map::Enum;

use super::Zone;
use crate::card::{Card, Rank, Suit};
use crate::card_sequence::Sequenced as _;
use crate::piles::{Cards, Pile as _};

pub mod place;
pub mod take;

mod sequence;

use self::sequence::FoundationSequence;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Enum)]
pub struct Index(pub Suit);

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Foundation {
    pile: Cards,
}

#[derive(Debug, Clone, Copy)]
pub struct View<'a> {
    pub stack: &'a [Card],
}

impl Foundation {
    pub const fn empty() -> Self {
        Self {
            pile: Cards::empty(),
        }
    }
}

impl From<Cards> for Foundation {
    fn from(pile: Cards) -> Self {
        if let Some(card) = pile.cards().first() {
            assert!(card.rank == Rank::Ace);
        }

        pile.is_sequential::<FoundationSequence>()
            .expect("Pile must be sequential");

        Self { pile }
    }
}

impl FromIterator<Card> for Foundation {
    fn from_iter<I>(cards: I) -> Self
    where
        I: IntoIterator<Item = Card>,
    {
        cards.into_iter().collect::<Cards>().into()
    }
}

impl Zone for Foundation {
    type View<'a> = View<'a>
    where
        Self: 'a;

    fn as_view(&self) -> Self::View<'_> {
        View {
            stack: self.pile.cards(),
        }
    }
}