use super::{Decider, DeciderHooks};
use crate::actions::Action;
use crate::metadata::AsAny;
use crate::observers::{Observers, ResponseObserver};
use crate::responses::Response;
use crate::state::SharedState;
use crate::std_ext::tuple::Named;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(clippy::derive_partial_eq_without_eq)] pub struct StatusCodeDecider<F>
where
F: Fn(u16, u16, &SharedState) -> Action,
{
comparator: F,
status_code: u16,
}
impl<F> StatusCodeDecider<F>
where
F: Fn(u16, u16, &SharedState) -> Action + 'static,
{
pub const fn new(status_code: u16, comparator: F) -> Self {
Self {
comparator,
status_code,
}
}
}
impl<O, R, F> DeciderHooks<O, R> for StatusCodeDecider<F>
where
O: Observers<R>,
R: Response,
F: Fn(u16, u16, &SharedState) -> Action + Sync + Send + Clone + 'static,
{
}
impl<O, R, F> Decider<O, R> for StatusCodeDecider<F>
where
O: Observers<R>,
R: Response,
F: Fn(u16, u16, &SharedState) -> Action + Clone + 'static,
{
fn decide_with_observers(&mut self, state: &SharedState, observers: &O) -> Option<Action> {
if let Some(observer) = observers.match_name::<ResponseObserver<R>>("ResponseObserver") {
let observed_status = observer.status_code();
return Some((self.comparator)(self.status_code, observed_status, state));
}
None
}
}
impl<F> AsAny for StatusCodeDecider<F>
where
F: Fn(u16, u16, &SharedState) -> Action + 'static,
{
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
impl<F> Named for StatusCodeDecider<F>
where
F: Fn(u16, u16, &SharedState) -> Action,
{
fn name(&self) -> &'static str {
"StatusCodeDecider"
}
}