use std::pin::Pin;
#[derive(Default, Debug)]
pub struct ScopeEndpointBuilder(Vec<Pin<Box<dyn crate::Middleware>>>);
impl ScopeEndpointBuilder {
pub fn with<M: crate::Middleware>(&mut self, middleware: M) -> &mut Self {
self.0.push(Box::pin(middleware));
self
}
pub fn then<E: crate::Endpoint>(&mut self, endpoint: E) -> ScopeEndpoint {
let endpoint = Box::pin(endpoint);
let middleware = std::mem::take(&mut self.0);
ScopeEndpoint {
middleware,
endpoint,
}
}
}
#[derive(Debug)]
pub struct ScopeEndpoint {
middleware: Vec<Pin<Box<dyn crate::Middleware>>>,
endpoint: Pin<Box<dyn crate::Endpoint>>,
}
#[async_trait]
impl crate::Endpoint for ScopeEndpoint {
async fn apply(
self: Pin<&Self>,
request: crate::Request,
) -> Result<crate::Response, anyhow::Error> {
let next = crate::middleware::Next::new(&self.middleware[..], self.endpoint.as_ref());
next.apply(request).await
}
}