spru 0.1.0

Reusable components for the spru strategy and digital board game framework.
Documentation
use std::{any, fmt, ops};

use crate::{
    action,
    common::error::{AnyError, PsuedoError},
    item, player,
};

/// An error encountered during a [player::Init].
/// [std::ops::Deref] to use as a [std::error::Error].
#[derive(Debug)]
pub struct Error {
    kind: Kind,
    context: Option<Context>,
}

impl ops::Deref for Error {
    type Target = dyn std::error::Error;

    fn deref(&self) -> &Self::Target {
        self.as_error()
    }
}

impl Error {
    pub(crate) fn new(kind: Kind) -> Self {
        Self {
            kind,
            context: None,
        }
    }

    pub(crate) fn with_context<PlayerInit: player::Init>(
        mut self,
        player_init: &PlayerInit,
    ) -> Self {
        self.context = Some(Context::new(player_init));
        self
    }

    /// The contained inner error
    pub fn kind(&self) -> &Kind {
        &self.kind
    }
}

impl From<action::Error> for Error {
    fn from(value: action::Error) -> Self {
        Self::new(Kind::Action(value))
    }
}

impl From<item::storage::Error> for Error {
    fn from(value: item::storage::Error) -> Self {
        Self::new(Kind::Action(value.into()))
    }
}

impl<E: std::error::Error + Send + Sync + 'static> From<E> for Error {
    fn from(value: E) -> Self {
        Self::new(Kind::Init(AnyError::new(value)))
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { kind, context } = self;

        if let Some(context) = context {
            write!(f, "{context}")?;
        } else {
            write!(f, "Game Initializer")?;
        }
        write!(f, " failed: {kind}")?;

        Ok(())
    }
}

impl PsuedoError for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            Kind::Action(e) => std::error::Error::source(e.as_error()),
            Kind::Init(e) => std::error::Error::source(e.as_error()),
        }
    }
}

/// The inner error contained in the [Error]
#[derive(Debug)]
pub enum Kind {
    /// An [action::Error]
    Action(action::Error),
    /// A user-provided error
    Init(AnyError),
}

impl fmt::Display for Kind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Kind::Action(e) => fmt::Display::fmt(e, f),
            Kind::Init(e) => fmt::Display::fmt(e, f),
        }
    }
}

#[derive(Debug)]
struct Context {
    player_init_name: &'static str,
}

impl Context {
    fn new<PlayerInit: player::Init>(_game_init: &PlayerInit) -> Self {
        Self {
            player_init_name: any::type_name::<PlayerInit>(),
        }
    }
}

impl fmt::Display for Context {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { player_init_name } = self;

        write!(f, "Player Initializer '{player_init_name}'")?;

        Ok(())
    }
}