#![allow(clippy::use_self)] use crate::actions::Action;
use crate::metadata::AsAny;
use crate::observers::Observers;
use crate::requests::Request;
use crate::responses::Response;
use crate::state::SharedState;
use crate::std_ext::tuple::Named;
use crate::ProcessorsList;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub use self::request::RequestProcessor;
pub use self::response::ResponseProcessor;
pub use self::statistics::StatisticsProcessor;
mod request;
mod response;
mod statistics;
use dyn_clone::DynClone;
#[derive(Copy, Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Ordering {
PreSend,
PostSend,
#[default]
PreAndPostSend,
}
pub trait Processor: DynClone + AsAny + Named {}
pub trait ProcessorHooks<O, R>: Processor + DynClone + AsAny + Sync + Send
where
O: Observers<R>,
R: Response,
{
fn pre_send_hook(
&mut self,
_state: &SharedState,
_request: &mut Request,
_action: Option<&Action>,
) {
}
fn post_send_hook(&mut self, _state: &SharedState, _observers: &O, _action: Option<&Action>) {}
}
impl Clone for Box<dyn Processor> {
fn clone(&self) -> Self {
dyn_clone::clone_box(&**self)
}
}
impl<O, R> Clone for Box<dyn ProcessorHooks<O, R>>
where
O: Observers<R>,
R: Response,
{
fn clone(&self) -> Self {
dyn_clone::clone_box(&**self)
}
}
pub trait Processors<O, R>
where
O: Observers<R>,
R: Response,
{
fn call_pre_send_hooks(
&mut self,
_state: &SharedState,
_request: &mut Request,
_action: Option<&Action>,
) {
}
fn call_post_send_hooks(
&mut self,
_state: &SharedState,
_observers: &O,
_action: Option<&Action>,
) {
}
}
impl<O, R> Processors<O, R> for ()
where
O: Observers<R>,
R: Response,
{
}
impl<Head, Tail, O, R> Processors<O, R> for (Head, Tail)
where
Head: ProcessorHooks<O, R>,
Tail: Processors<O, R> + ProcessorsList,
O: Observers<R>,
R: Response,
{
fn call_pre_send_hooks(
&mut self,
state: &SharedState,
request: &mut Request,
action: Option<&Action>,
) {
self.0.pre_send_hook(state, request, action);
self.1.call_pre_send_hooks(state, request, action);
}
fn call_post_send_hooks(
&mut self,
state: &SharedState,
observers: &O,
action: Option<&Action>,
) {
self.0.post_send_hook(state, observers, action);
self.1.call_post_send_hooks(state, observers, action);
}
}