use tower::{Layer, ServiceExt};
use crate::{layer::FullUriMatch, Actionable, ActiveAction, BoxedAction};
#[derive(Clone)]
pub struct Dispatchable<State> {
pattern: String,
action: BoxedAction<State>,
}
impl<State> Dispatchable<State> {
pub fn new(pattern: impl AsRef<str>, action: impl Actionable<State>) -> Self {
Self {
pattern: pattern.as_ref().into(),
action: Box::new(action),
}
}
}
impl<State> Actionable<State> for Dispatchable<State>
where
State: Clone + Send + 'static,
{
fn into_actionable(self: Box<Self>, state: State) -> ActiveAction {
let layer = FullUriMatch::layer(self.pattern.clone());
let service = layer.layer(self.action.into_actionable(state));
service.boxed_clone().into()
}
}