use core::cell::Cell;
use std::fmt;
use std::rc::Rc;
use crate::extract::parent::ParentHandle;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Param {
pub(crate) name: String,
pub(crate) value: Option<String>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct Deps {
pub(crate) path: bool,
pub(crate) query: bool,
pub(crate) params: bool,
pub(crate) parent: bool,
}
#[derive(Clone)]
pub struct Request {
path: String,
query: Option<String>,
params: Vec<Param>,
route: String,
pub(crate) deps: Rc<Cell<Deps>>,
pub(crate) parent: Option<ParentHandle>,
}
impl Request {
pub(crate) fn new(url: &str) -> Self {
let (path, query) = match url.split_once('?') {
Some((path, query)) => (path, Some(query.to_owned())),
None => (url, None),
};
Self {
path: crate::router::normalize(path),
query: query.filter(|q| !q.is_empty()),
params: Vec::new(),
route: String::new(),
deps: Rc::default(),
parent: None,
}
}
pub(crate) fn with_route(mut self, route: &str, params: Vec<Param>) -> Self {
self.route = route.to_owned();
self.params = params;
self
}
pub(crate) fn for_handler(&self, params: usize, parent: Option<ParentHandle>) -> Self {
Self {
params: self.params[..params.min(self.params.len())].to_vec(),
deps: Rc::default(),
parent,
..self.clone()
}
}
fn track(&self, update: impl FnOnce(&mut Deps)) {
let mut deps = self.deps.get();
update(&mut deps);
self.deps.set(deps);
}
pub fn path(&self) -> &str {
self.track(|deps| deps.path = true);
&self.path
}
pub fn query(&self) -> Option<&str> {
self.track(|deps| deps.query = true);
self.query.as_deref()
}
pub fn url(&self) -> String {
self.track(|deps| {
deps.path = true;
deps.query = true;
});
self.untracked_url()
}
pub fn route(&self) -> &str {
&self.route
}
pub fn param(&self, name: &str) -> Option<&str> {
self.track(|deps| deps.params = true);
self.params
.iter()
.find(|param| param.name == name)
.and_then(|param| param.value.as_deref())
}
pub(crate) fn params(&self) -> &[Param] {
self.track(|deps| deps.params = true);
&self.params
}
pub(crate) fn untracked_path(&self) -> &str {
&self.path
}
pub(crate) fn untracked_url(&self) -> String {
match &self.query {
Some(query) => format!("{}?{query}", self.path),
None => self.path.clone(),
}
}
pub(crate) fn same_for(&self, deps: Deps, other: &Self) -> bool {
(!deps.path || self.path == other.path)
&& (!deps.query || self.query == other.query)
&& (!deps.params || self.params == other.params)
}
}
impl fmt::Debug for Request {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Request")
.field("path", &self.path)
.field("query", &self.query)
.field("params", &self.params)
.field("route", &self.route)
.finish_non_exhaustive()
}
}