use crate::{
After, AfterFn, AfterHandler, Before, BeforeFn, DispatchError, DispatchStage, Handler,
IdentityResponse, NoAfter, NoBefore, ResponseFn, ResponseHandler, ResponseStage, StageError,
};
pub type CommandHandler<C, R, O, E> = fn(&mut C, &mut R) -> Result<O, StageError<E>>;
pub struct CommandEntry<K, C, R, O, E> {
key: K,
handler: CommandHandler<C, R, O, E>,
}
impl<K, C, R, O, E> CommandEntry<K, C, R, O, E> {
pub const fn new(key: K, handler: CommandHandler<C, R, O, E>) -> Self {
Self { key, handler }
}
pub const fn key(&self) -> &K {
&self.key
}
pub fn invoke(&self, context: &mut C, resources: &mut R) -> Result<O, StageError<E>> {
(self.handler)(context, resources)
}
}
pub trait Matcher<K, C: ?Sized> {
fn matches(&self, key: &K, context: &C) -> bool;
}
impl<K, C, F> Matcher<K, C> for F
where
C: ?Sized,
F: Fn(&K, &C) -> bool,
{
fn matches(&self, key: &K, context: &C) -> bool {
self(key, context)
}
}
pub trait CommandKey<K> {
fn command_key(&self) -> &K;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct KeyMatcher;
impl<K, C> Matcher<K, C> for KeyMatcher
where
K: PartialEq,
C: CommandKey<K>,
{
fn matches(&self, key: &K, context: &C) -> bool {
key == context.command_key()
}
}
pub struct Dispatcher<'table, K, C, R, O, E, M, B = NoBefore, A = NoAfter, S = IdentityResponse> {
table: &'table [CommandEntry<K, C, R, O, E>],
matcher: M,
before: B,
after: A,
response: S,
}
pub type ResponseDispatcher<'table, K, C, R, O, E, M, B, A, F, Params, T> =
Dispatcher<'table, K, C, R, O, E, M, B, A, ResponseFn<F, Params, T>>;
impl<'table, K, C, R, O, E, M>
Dispatcher<'table, K, C, R, O, E, M, NoBefore, NoAfter, IdentityResponse>
{
pub const fn new(table: &'table [CommandEntry<K, C, R, O, E>], matcher: M) -> Self {
Self {
table,
matcher,
before: NoBefore,
after: NoAfter,
response: IdentityResponse,
}
}
}
impl<'table, K, C, R, O, E, M, B, A, S> Dispatcher<'table, K, C, R, O, E, M, B, A, S> {
pub fn with_before<F, Params>(
self,
function: F,
) -> Dispatcher<'table, K, C, R, O, E, M, BeforeFn<F, Params>, A, S>
where
F: Handler<C, R, (), E, Params>,
{
Dispatcher {
table: self.table,
matcher: self.matcher,
before: BeforeFn::new(function),
after: self.after,
response: self.response,
}
}
pub fn with_after<F, Params>(
self,
function: F,
) -> Dispatcher<'table, K, C, R, O, E, M, B, AfterFn<F, Params>, S>
where
F: AfterHandler<C, R, O, E, Params>,
{
Dispatcher {
table: self.table,
matcher: self.matcher,
before: self.before,
after: AfterFn::new(function),
response: self.response,
}
}
pub fn with_response<F, Params, T>(
self,
function: F,
) -> ResponseDispatcher<'table, K, C, R, O, E, M, B, A, F, Params, T>
where
F: ResponseHandler<C, R, O, T, E, Params>,
{
Dispatcher {
table: self.table,
matcher: self.matcher,
before: self.before,
after: self.after,
response: ResponseFn::new(function),
}
}
pub fn select(&self, context: &C) -> Option<&'table CommandEntry<K, C, R, O, E>>
where
M: Matcher<K, C>,
{
self.table
.iter()
.find(|command| self.matcher.matches(command.key(), context))
}
pub fn dispatch(
&self,
context: &mut C,
resources: &mut R,
) -> Result<S::Output, DispatchError<E>>
where
M: Matcher<K, C>,
B: Before<C, R, E>,
A: After<C, R, O, E>,
S: ResponseStage<C, R, O, E>,
{
let command = self.select(context).ok_or(DispatchError::NotFound)?;
self.before
.run_before(context, resources)
.map_err(|error| DispatchError::at(DispatchStage::Before, error))?;
let mut output = command
.invoke(context, resources)
.map_err(|error| DispatchError::at(DispatchStage::Command, error))?;
self.after
.run_after(context, &mut output, resources)
.map_err(|error| DispatchError::at(DispatchStage::After, error))?;
self.response
.run_response(context, output, resources)
.map_err(|error| DispatchError::at(DispatchStage::Response, error))
}
pub const fn table(&self) -> &'table [CommandEntry<K, C, R, O, E>] {
self.table
}
pub const fn matcher(&self) -> &M {
&self.matcher
}
pub fn matcher_mut(&mut self) -> &mut M {
&mut self.matcher
}
pub const fn before(&self) -> &B {
&self.before
}
pub fn before_mut(&mut self) -> &mut B {
&mut self.before
}
pub const fn after(&self) -> &A {
&self.after
}
pub fn after_mut(&mut self) -> &mut A {
&mut self.after
}
pub const fn response(&self) -> &S {
&self.response
}
pub fn response_mut(&mut self) -> &mut S {
&mut self.response
}
}
pub fn dispatch<K, C, R, O, E>(
table: &[CommandEntry<K, C, R, O, E>],
context: &mut C,
resources: &mut R,
) -> Result<O, DispatchError<E>>
where
K: PartialEq,
C: CommandKey<K>,
{
Dispatcher::new(table, KeyMatcher).dispatch(context, resources)
}