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;

#[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);
    }
}