use {
crate::{Middleware, Request, Response, Result},
http_types::Body,
std::{borrow::Cow, collections::HashMap, sync::Arc},
};
#[allow(missing_debug_implementations)]
pub struct Context<'x, Ex> {
pub req: &'x Request,
pub resp: &'x mut Response,
pub ex: &'x mut Ex,
pub(crate) body: &'x mut Option<Body>,
pub(crate) remain_path: &'x str,
pub(crate) router_matches: &'x mut HashMap<Cow<'static, str>, String>,
pub(crate) tail: &'x [Arc<dyn Middleware<Ex>>],
}
impl<'x, Ex> Context<'x, Ex>
where
Ex: Send + Sync + 'static,
{
pub async fn next(&mut self) -> Result {
if let Some((current, tail)) = self.tail.split_first() {
self.tail = tail;
let next_ctx = Context {
req: self.req,
body: self.body,
resp: self.resp,
ex: self.ex,
remain_path: self.remain_path,
router_matches: self.router_matches,
tail,
};
current.handle(next_ctx).await
} else {
Ok(())
}
}
pub fn body(&mut self) -> Option<Body> {
self.body.take()
}
#[must_use]
pub fn path(&self) -> &str {
self.remain_path
}
pub fn arg<K: AsRef<str>>(&self, name: K) -> Option<&str> {
self.router_matches.get(name.as_ref()).map(String::as_str)
}
}