lumecs 0.0.1

Experimental GUI backed by Bevy ECS + usual suspects
use std::error::Error;
use std::fmt;

use crossbeam_channel::SendError;
use crossbeam_channel::Sender;

pub enum SubscriptionError<M> {
    SendError(SendError<M>),
}

impl<M> fmt::Debug for SubscriptionError<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SubscriptionError::SendError(err) => {
                write!(f, "SubscriptionError::SendError({:?})", err)
            }
        }
    }
}

impl<M> std::fmt::Display for SubscriptionError<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SubscriptionError::SendError(err) => write!(f, "Failed to send message: {:?}", err),
        }
    }
}

impl<M> Error for SubscriptionError<M> where M: std::fmt::Debug {}

pub struct Subscription<M> {
    sender: Sender<M>,
}

impl<M> Subscription<M> {
    pub(crate) fn new(sender: Sender<M>) -> Self {
        Self { sender }
    }

    pub fn send(&self, message: M) -> Result<(), SubscriptionError<M>> {
        self.sender
            .send(message)
            .map_err(SubscriptionError::SendError)
    }
}