soliterm-model 0.1.0

Shared model types for soliterm
Documentation
use super::Waste;
use crate::piles::{Cards, Pile as _, PileMut as _};
use crate::{action, undo};

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

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

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

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

        self.pile.place(pile);
        self.pile.set_right_length(count);

        Ok(Value {
            count,
            previous_stack_len,
        })
    }
}

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