soliterm-model 0.1.0

Shared model types for soliterm
Documentation
use derive_more::{Display, Error};

use super::Tableau;
use crate::piles::Pile as _;
use crate::{action, undo};

#[derive(Debug)]
pub struct Action;

#[derive(Debug)]
pub struct Value;

#[derive(Debug, Display, Error)]
pub enum Error {
    #[display(fmt = "The pile is empty")]
    Empty,

    #[display(fmt = "The top card is already face-up")]
    FaceUp,
}

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

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

        if self.pile.left().is_empty() {
            return Err(Error::Empty);
        }

        self.pile.set_right_length(1);

        Ok(Value)
    }
}

impl undo::Target<Value> for Tableau {
    fn revert(&mut self, _: Value) {
        self.pile.set_right_length(0);
    }
}