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

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

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

#[derive(Debug)]
pub struct Value;

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

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

impl action::Target<Action> for Stock {
    fn update(&mut self, Action(pile): Action, _state: ()) -> Result<Value, Error> {
        if !self.pile.is_empty() {
            return Err(Error::NotEmpty);
        }

        self.pile.place(pile);

        Ok(Value)
    }
}

impl undo::Target<Value> for Stock {
    fn revert(&mut self, _undo: Value) {
        // Take them and let them drop. Whoever gave us those cards tracked it in their own Undo.
        self.pile.take_all();
    }
}