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
use super::Waste;
use crate::piles::{Cards, Pile as _, PileMut as _};
use crate::{action, undo};

#[derive(Debug)]
pub struct Action;

#[derive(Debug)]
pub struct Value {
    emptied: Cards,
    previous_stack_len: usize,
}

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

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

impl action::Target<Action> for Waste {
    fn update(&mut self, _: Action, _: ()) -> Result<Value, !> {
        let previous_stack_len = self.pile.left().len();
        let cards = self.pile.take_all().flipped();

        Ok(Value {
            emptied: cards,
            previous_stack_len,
        })
    }
}

impl undo::Target<Value> for Waste {
    fn revert(&mut self, undo: Value) {
        let cards = undo.emptied.flipped();

        self.pile.place(cards);
        self.pile.set_left_length(undo.previous_stack_len);
    }
}