use super::{Event, VoidResponse};
use kas::geom::Rect;
#[derive(Clone, Debug)]
#[must_use]
pub enum Response<M> {
None,
Unhandled(Event),
Focus(Rect),
Msg(M),
}
impl<M> Response<M> {
#[inline]
pub fn is_none(&self) -> bool {
match self {
&Response::None => true,
_ => false,
}
}
#[inline]
pub fn is_unhandled(&self) -> bool {
match self {
&Response::Unhandled(_) => true,
_ => false,
}
}
#[inline]
pub fn is_msg(&self) -> bool {
match self {
&Response::Msg(_) => true,
_ => false,
}
}
#[inline]
pub fn from<N>(r: Response<N>) -> Self
where
M: From<N>,
{
r.try_into()
.unwrap_or_else(|msg| Response::Msg(M::from(msg)))
}
#[inline]
pub fn into<N>(self) -> Response<N>
where
N: From<M>,
{
Response::from(self)
}
#[inline]
pub fn try_from<N>(r: Response<N>) -> Result<Self, N> {
use Response::*;
match r {
None => Ok(None),
Unhandled(e) => Ok(Unhandled(e)),
Focus(rect) => Ok(Focus(rect)),
Msg(m) => Err(m),
}
}
#[inline]
pub fn try_into<N>(self) -> Result<Response<N>, M> {
Response::try_from(self)
}
}
impl VoidResponse {
pub fn void_into<M>(self) -> Response<M> {
self.try_into().unwrap_or(Response::None)
}
}
impl<M> From<M> for Response<M> {
fn from(msg: M) -> Self {
Response::Msg(msg)
}
}